Ssh – Using nohup to keep a script running indefinitely

cronnohupPHPrhelssh

I am very new to Linux and I've only been using it to SSH to my web server using Putty. The issue I am having is this:

Basically I've got 2 php scripts. One get_tweets.php constantly listens for tweets on twitters Streaming API whilst the other – parse_tweets.php logs these into a db. In order for these to work they've to be kept running continuously.

I've been using the following commands to run them in the background and from what I've seen they run for most of the day, however whenever I log on to my computer in the morning the scripts have stopped and I've to run commands again.

nohup php my_script.php > /dev/null &

I'm just wondering if that is normal based on the commands I am using, should they run indefinitely when using nohup and if not what is the alternative? A CRON job?

Thanks for the help

Best Answer

With nohup you must redirect also errors. The next command run script with output and errors redirecting to /dev/null:

nohup php my_script.php >/dev/null 2>&1 &

But your script can be terminated by some other reasons (error in script, oom-killer, etc). So, you should daemonise it by system's init (if it support auto-restart - upstart, systemd, and some others can do it). Or you should write cron task for check and restart your script if it not run.

Related Question