Skip to content

POSIX signal handling in a php zend framework 2 console controller action call

I played around with php console applications for a while now. The projects are getting bigger and sometimes, the load on the system or the runtime itself increases a lot.
At night, it made "click" and I asked myself "Are you nuts or why aren't you simple using POSIX signals for your php console applications (like normal people are doing it)?". Thanks to the book Signaling PHP, I could "kickstart" into the topic and created a simple example script.

While solving this problem and migrating some own scripts, I wanted to implement the signal handling in a bigger zend framework 2 project.
Quickly done, I copy/pasted/adapted my example code and thought "this is it". To bad, I need to take one pitfall. I could not debug it right know, but zf2 needs to have the "signal handler method" public. The following code is the final outcome of this adaptation session.

class MyController extends AbstractCliActionController
    //AbstractCliActionController extends zend AbstractActionController
    //...usefull controller code
     * @param AbstractCliActionController $object
     * @param string $method
    protected function attachSignalHandler(AbstractCliActionController $object, $method = 'defaultSignalHandler')
        declare(ticks = 10);

        pcntl_signal(SIGHUP,    array($object, $method));
        pcntl_signal(SIGINT,    array($object, $method));
        pcntl_signal(SIGUSR1,   array($object, $method));
        pcntl_signal(SIGUSR2,   array($object, $method));
        pcntl_signal(SIGQUIT,   array($object, $method));
        pcntl_signal(SIGILL,    array($object, $method));
        pcntl_signal(SIGABRT,   array($object, $method));
        pcntl_signal(SIGFPE,    array($object, $method));
        pcntl_signal(SIGSEGV,   array($object, $method));
        pcntl_signal(SIGPIPE,   array($object, $method));
        pcntl_signal(SIGALRM,   array($object, $method));
        pcntl_signal(SIGCONT,   array($object, $method));
        pcntl_signal(SIGTSTP,   array($object, $method));
        pcntl_signal(SIGTTIN,   array($object, $method));
        pcntl_signal(SIGTTOU,   array($object, $method));

     * @param $signal
    public function defaultSignalHandler($signal)
        echo 'caught signal "' . $signal . '"' . PHP_EOL;

You can easily extend the method "defaultSignalHandler" to whatever you need (push something to a log, flip a flag to stop execution etc) and thats it. Injoy your work :-).

Thanks to the nice irc freenode channel #zftalk, I can provide a second implementation to prove my example to be right.

Translate to de es fr it pt ja


No Trackbacks


Display comments as Linear | Threaded

No comments

The author does not allow comments to this entry

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.

Markdown format allowed
Form options