<p>What is the best way to keep a PHP script running as a daemon, and what's the best way to check if needs restarting.</p>

<p>I have some scripts that need to run 24/7 and for the most part I can run them using <a href="http://en.wikipedia.org/wiki/Nohup" rel="noreferrer">nohup</a>.  But if they go down, what's the best way to monitor it so it can be automatically restarted?</p>

[{'answer_id': 38608, 'author': 'rmmh', 'author_id': 3694, 'author_profile': 'https://Stackoverflow.com/users/3694', 'pm_score': 3, 'selected': True, 'text': "<p>If you can't use the (proper) init structure to do this (you're on shared hosting, etc.), use cron to run a script (it can be written in whatever language you like) every few minutes that checks to see if they're running, and restarts them if necessary.</p>\n"}, {'answer_id': 38665, 'author': 'Robin Barnes', 'author_id': 1349865, 'author_profile': 'https://Stackoverflow.com/users/1349865', 'pm_score': -1, 'selected': False, 'text': "<p>TBH, PHP probably isn't the best tool for this, really not what it was designed for. I've heard of memory leaks and other bad things happening when you try this. Also bear in mind PHP only has a finite amount of resource ids (for file handles, db connections ect) per execution of a script.</p>\n\n<p>Be better of using something else, maybe python or perl, though I don't have any real experience writing these sorts of apps, but I do know PHP isn't right for what your trying to do.</p>\n"}, {'answer_id': 38712, 'author': 'Daniel', 'author_id': 416, 'author_profile': 'https://Stackoverflow.com/users/416', 'pm_score': 1, 'selected': False, 'text': "<p>I've had success with running a wget and sending the result to /dev/null on a shared server.</p>\n"}, {'answer_id': 39609, 'author': 'Gary Richardson', 'author_id': 2506, 'author_profile': 'https://Stackoverflow.com/users/2506', 'pm_score': 2, 'selected': False, 'text': "<p>Quick and dirty cron to restart your daemon:</p>\n\n<pre><code>* * * * * USER ps auxww | grep SCRIPTNAME &gt; /dev/null || SCRIPTNAME\n</code></pre>\n\n<p>Replace USER with the user that the daemon runs as and SCRIPTNAME with the name of your script. Stick this in <code>/etc/cron.d/restart_php_daemon</code>. It should run every minute. Change the first <code>*</code> to <code>*/2</code> or <code>*/5</code> to run less frequently.</p>\n\n<p><em>UPDATE</em></p>\n\n<p>If you're putting this into your own crontab:</p>\n\n<p>Run <code>crontab -e</code> and add:</p>\n\n<pre><code>* * * * * ps auxwww | grep SCRIPTNAME &gt; /dev/null || SCRIPTNAME\n</code></pre>\n"}, {'answer_id': 109865, 'author': 'Alister Bulman', 'author_id': 6216, 'author_profile': 'https://Stackoverflow.com/users/6216', 'pm_score': 0, 'selected': False, 'text': '<p>I use a PHP-based script to read from a database and send emails out (using the PEAR Mail_Queue library).  I run it from within a bash script and based on the returned result (from "exit $status;") either halt, sleep X seconds, or immediately restart. (I also put a check of the load average/sleep into the PHP script to avoid stressing the mail system).</p>\n\n<p>If it was for a long-term daemon that had to be continually running, then I agree, it probably would not be the best thing to run this (though I have heard of some socket servers that did run successfully long term), however, PHP 5.3 does also now have improved garbage collection, and if the script is well written enough to not exit unplanned, then memory should be far less of a problem that before.</p>\n'}, {'answer_id': 110787, 'author': 'troelskn', 'author_id': 18180, 'author_profile': 'https://Stackoverflow.com/users/18180', 'pm_score': 2, 'selected': False, 'text': '<p>We run our daemons by piping the output to mail.</p>\n\n<pre><code>php daemon.php | mail -s "daemon stopped" foo@example.org\n</code></pre>\n\n<p>That way, when/if the daemon stops, it will send a mail, and we will be notified that way.</p>\n\n<p>It still means manual restart of the daemons of course, but we\'ll know right away. Usually, if the daemons stopped, it means that there is something else that needs to be taken care of anyway, so that\'s usually ok.</p>\n'}, {'answer_id': 4919432, 'author': 'SlappyTheFish', 'author_id': 207753, 'author_profile': 'https://Stackoverflow.com/users/207753', 'pm_score': 0, 'selected': False, 'text': '<p>I agree that PHP is not the best tool for this, however I can understand why you want to use PHP so you can reuse components from your application such as database access, and so on.</p>\n\n<p>I had a similar problem and I ended up developing The Fat Controller which is a daemon written in C that can run PHP scripts.   It can also run as a multithreaded daemon, running many instances of a script in parallel.</p>\n\n<p>There\'s more information and use cases here:   <a href="http://www.4pmp.com/fatcontroller/" rel="nofollow">http://www.4pmp.com/fatcontroller/</a></p>\n'}, {'answer_id': 10361464, 'author': 'Janus Troelsen', 'author_id': 309483, 'author_profile': 'https://Stackoverflow.com/users/309483', 'pm_score': 2, 'selected': False, 'text': '<p>The most elegant solution is <a href="http://reactphp.org" rel="nofollow noreferrer">reactPHP</a>.</p>\n'}, {'answer_id': 22238374, 'author': 'sergioska', 'author_id': 2496366, 'author_profile': 'https://Stackoverflow.com/users/2496366', 'pm_score': 1, 'selected': False, 'text': '<p>Daemon is a linux process that runs in background; apache or mysql are daemons.\nIn a linux environment, we can run a background program  using cronjob, but it has some limitations, and in some scenarios it\' s not a good idea.\nFor example, using cronjob, we can\'t control if the previously run has finished yet.\nSo often it\'s more convenient run a process as a daemon.</p>\n\n<pre><code>// Daemonize\n$pid = pcntl_fork(); // parent gets the child PID and child gets 0\nif($pid){ // if pid is not 0\n     // Only the parent will know the PID. Kids aren\'t self-aware\n     // Parent says goodbye!\n     print "Parent : " . getmypid() . " exiting\\n";\n     exit();\n}\nprint "Child : " . getmypid() . "\\n";\n</code></pre>\n\n<p>The code above is taken from very good article about how to create a daemon in php. You can read this at <a href="http://stuporglue.org/writing-a-daemon-with-php/" rel="nofollow">link</a> </p>\n'}]
['https://Stackoverflow.com/questions/38596', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4133/']