Console signal implementation using PHPs Process control functions.
There are 2 implementations.
- Background command. Allows a process to be interrupted using the signal handler.
- Daemon command. Builds on the Background command to allow forking (detaching) the process.
Via Composer
$ composer require phlib/console-process
The Background Command works in the same way as you're used to with the normal Symfony Command.
<?php
use Phlib\Console\Command\BackgroundCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyProcessCommand extends BackgroundCommand
{
protected function configure()
{
$this->setName('my:process')
->setDescription('My background process.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Doing important work!');
}
}
The background command has one additional method, to the standard Symfony command methods, which gets called
when the process finishes. The method is onShutdown
which takes a InputInterface
and
an OutputInterface
. This can be used for any final cleanup for example.
class MyProcessCommand extends BackgroundCommand
{
// ...
protected function onShutdown(InputInterface $input, OutputInterface $output)
{
$output->writeln('onShutdown method called.');
}
}
Apart from extending a different class, the Daemon Command looks and works in a similar way to the Background Command. The PID file argument is optional. If it is not specified the process generates it's own PID file based on the command name.
<?php
use Phlib\Console\Command\DaemonCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyProcessCommand extends DaemonCommand
{
protected function configure()
{
$this->setName('my:process')
->setDescription('My background process.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Doing important work!');
}
}
Once a daemon process is detached, the original output is also lost. The daemon command class provides a
protected method to specify a new output. If this method isn't overriden, then by default, the output is a
NullOutput
Symfony object. The following example demonstrates overriding the method.
use Symfony\Component\Console\Output\StreamOutput;
class MyProcessCommand extends DaemonCommand
{
// ...
protected function createChildOutput()
{
return new StreamOutput(fopen(getcwd() . '/my-daemon.log', 'a'));
}
}
The daemon command has additional methods, to the standard Symfony command methods and the background command, which gets called during the process lifecycle. The following example demonstrates overriding the methods.
class MyProcessCommand extends DaemonCommand
{
// ...
protected function onBeforeDaemonize(InputInterface $input, OutputInterface $output)
{
$output->writeln('onBeforeDaemonize method called.');
}
protected function onAfterDaemonizeChild(InputInterface $input, OutputInterface $output)
{
$output->writeln('onAfterDaemonizeChild method called.');
}
protected function onAfterDaemonizeParent(InputInterface $input, OutputInterface $output)
{
$output->writeln('onAfterDaemonizeParent method called.');
}
}
# path/to/my/process start -d
# path/to/my/process status
# path/to/my/process stop
Name | Short | Type | Required | Default | Description |
---|---|---|---|---|---|
action | Argument | yes | start, stop, status | ||
daemonize | d | Option | no | no | Detaches the process |
pid-file | p | Option | no | auto | Name of the PID file to use. Not used if daemonize is not set. |
This package is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see http://www.gnu.org/licenses/.