<!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>Basic usage</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pcntl.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.pcntl.html">PCNTL Functions</a></div> <div class="up"><a href="pcntl.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="pcntl.example" class="section"> <h2 class="title">Basic usage</h2> <p class="para"> This example forks off a daemon process with a signal handler. </p> <div class="example" id="example-3642"> <p><strong>Example #1 Process Control 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: #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: #0000BB">$pid </span><span style="color: #007700">= </span><span style="color: #0000BB">pcntl_fork</span><span style="color: #007700">();<br />if (</span><span style="color: #0000BB">$pid </span><span style="color: #007700">== -</span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /> die(</span><span style="color: #DD0000">"could not fork"</span><span style="color: #007700">); <br />} else if (</span><span style="color: #0000BB">$pid</span><span style="color: #007700">) {<br /> exit(); </span><span style="color: #FF8000">// we are the parent <br /></span><span style="color: #007700">} else {<br /> </span><span style="color: #FF8000">// we are the child<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">// detatch from the controlling terminal<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">posix_setsid</span><span style="color: #007700">() == -</span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /> die(</span><span style="color: #DD0000">"could not detach from terminal"</span><span style="color: #007700">);<br />}<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 /><br /></span><span style="color: #FF8000">// loop forever performing tasks<br /></span><span style="color: #007700">while (</span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /><br /> </span><span style="color: #FF8000">// do something interesting here<br /><br /></span><span style="color: #007700">}<br /><br />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 /> default:<br /> </span><span style="color: #FF8000">// handle all other signals<br /> </span><span style="color: #007700">}<br /><br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pcntl.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.pcntl.html">PCNTL Functions</a></div> <div class="up"><a href="pcntl.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>