<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Installs a signal handler</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pcntl-signal-dispatch.html">pcntl_signal_dispatch</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pcntl-sigprocmask.html">pcntl_sigprocmask</a></div> <div class="up"><a href="ref.pcntl.html">PCNTL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.pcntl-signal" class="refentry"> <div class="refnamediv"> <h1 class="refname">pcntl_signal</h1> <p class="verinfo">(PHP 4 >= 4.1.0, PHP 5)</p><p class="refpurpose"><span class="refname">pcntl_signal</span> — <span class="dc-title">Installs a signal handler</span></p> </div> <div class="refsect1 description" id="refsect1-function.pcntl-signal-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>pcntl_signal</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$signo</code></span> , <span class="methodparam"><span class="type"><span class="type callable|int">callable|int</span></span> <code class="parameter">$handler</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$restart_syscalls</code><span class="initializer"> = true</span></span> ] )</div> <p class="para rdfs-comment"> The <span class="function"><strong>pcntl_signal()</strong></span> function installs a new signal handler or replaces the current signal handler for the signal indicated by <em><code class="parameter">signo</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.pcntl-signal-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">signo</code></em></span> <dd> <p class="para"> The signal number. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">handler</code></em></span> <dd> <p class="para"> The signal handler. This may be either a <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span>, which will be invoked to handle the signal, or either of the two global constants <strong><code>SIG_IGN</code></strong> or <strong><code>SIG_DFL</code></strong>, which will ignore the signal or restore the default signal handler respectively. </p> <p class="para"> If a <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> is given, it must implement the following signature: </p> <p class="para"> <div class="methodsynopsis dc-description"> <span class="type"><span class="type void">void</span></span> <span class="methodname"><span class="replaceable">handler</span></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$signo</code></span> )</div> <dl> <dt> <span class="term"><em><code class="parameter">signo</code></em></span> <dd> <span class="simpara"> The signal being handled. </span> </dd> </dt> </dl> </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Note that when you set a handler to an object method, that object's reference count is increased which makes it persist until you either change the handler to something else, or your script ends. </p> </p></blockquote> </dd> </dt> <dt> <span class="term"><em><code class="parameter">restart_syscalls</code></em></span> <dd> <p class="para"> Specifies whether system call restarting should be used when this signal arrives. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.pcntl-signal-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 changelog" id="refsect1-function.pcntl-signal-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.3.0</td> <td> The <em><code class="parameter">restart_syscalls</code></em> parameter was added. </td> </tr> <tr> <td>4.3.0</td> <td> The ability to use an object method as a callback became available. </td> </tr> <tr> <td>4.3.0</td> <td> As of PHP 4.3.0 PCNTL uses ticks as the signal handle callback mechanism, which is much faster than the previous mechanism. This change follows the same semantics as using "<a href="control-structures.declare.html#control-structures.declare.ticks" class="link">user ticks</a>". You must use the <a href="control-structures.declare.html" class="link">declare()</a> statement to specify the locations in your program where callbacks are allowed to occur for the signal handler to function properly (as used in the above example). </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.pcntl-signal-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3645"> <p><strong>Example #1 <span class="function"><strong>pcntl_signal()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// tick use required as of PHP 4.3.0<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">ticks </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// signal handler function<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">sig_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">$signo</span><span style="color: #007700">)<br />{<br /><br /> switch (</span><span style="color: #0000BB">$signo</span><span style="color: #007700">) {<br /> case </span><span style="color: #0000BB">SIGTERM</span><span style="color: #007700">:<br /> </span><span style="color: #FF8000">// handle shutdown tasks<br /> </span><span style="color: #007700">exit;<br /> break;<br /> case </span><span style="color: #0000BB">SIGHUP</span><span style="color: #007700">:<br /> </span><span style="color: #FF8000">// handle restart tasks<br /> </span><span style="color: #007700">break;<br /> case </span><span style="color: #0000BB">SIGUSR1</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">"Caught SIGUSR1...\n"</span><span style="color: #007700">;<br /> break;<br /> default:<br /> </span><span style="color: #FF8000">// handle all other signals<br /> </span><span style="color: #007700">}<br /><br />}<br /><br />echo </span><span style="color: #DD0000">"Installing signal handler...\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// setup signal handlers<br /></span><span style="color: #0000BB">pcntl_signal</span><span style="color: #007700">(</span><span style="color: #0000BB">SIGTERM</span><span style="color: #007700">, </span><span style="color: #DD0000">"sig_handler"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">pcntl_signal</span><span style="color: #007700">(</span><span style="color: #0000BB">SIGHUP</span><span style="color: #007700">, </span><span style="color: #DD0000">"sig_handler"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">pcntl_signal</span><span style="color: #007700">(</span><span style="color: #0000BB">SIGUSR1</span><span style="color: #007700">, </span><span style="color: #DD0000">"sig_handler"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// or use an object, available as of PHP 4.3.0<br />// pcntl_signal(SIGUSR1, array($obj, "do_something"));<br /><br /></span><span style="color: #007700">echo</span><span style="color: #DD0000">"Generating signal SIGTERM to self...\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// send SIGUSR1 to current process id<br /></span><span style="color: #0000BB">posix_kill</span><span style="color: #007700">(</span><span style="color: #0000BB">posix_getpid</span><span style="color: #007700">(), </span><span style="color: #0000BB">SIGUSR1</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Done\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.pcntl-signal-notes"> <h3 class="title">Notes</h3> <p class="para"> <span class="function"><strong>pcntl_signal()</strong></span> doesn't stack the signal handlers, but replaces them. </p> </div> <div class="refsect1 seealso" id="refsect1-function.pcntl-signal-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.pcntl-fork.html" class="function" rel="rdfs-seeAlso">pcntl_fork()</a> - Forks the currently running process</span></li> <li class="member"> <span class="function"><a href="function.pcntl-waitpid.html" class="function" rel="rdfs-seeAlso">pcntl_waitpid()</a> - Waits on or returns the status of a forked child</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pcntl-signal-dispatch.html">pcntl_signal_dispatch</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pcntl-sigprocmask.html">pcntl_sigprocmask</a></div> <div class="up"><a href="ref.pcntl.html">PCNTL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>