I am wondering if someone might point me in the right direction. I've got little experience working with the Linux command line and recently due to various factors in work I've been required to gain knowledge.
Basically I have two php
scripts that reside in a directory on my server. For the purposes of the application these scripts must be running continuously. Currently I implement that this way:
nohup sh -c 'while true; do php get_tweets.php; done' >/dev/null &
and
nohup sh -c 'while true; do php parse_tweets.php; done' >/dev/null &
However, I've noticed that despite the infinte loop the scripts to stop periodically and I'm forced to restart them. I'm not sure why but they do. That has made me look into the prospect of a CRON
job that checks if they are running and if they are not, run them/restart them.
Would anyone be able to provide me with some information on how to go about this?
Best Answer
I'd like to expand on Davidann's answer since you are new to the concept of a cron job. Every UNIX or Linux system has a crontab stored somewhere. The crontab is a plain text file. Consider the following: (From the Gentoo Wiki on Cron)
Being new to cron, you probably want to comment the starred columns so that you know what each column is used for. Every Cron implementation that I know of has always been this order. Now merging Davidann's answer with my commented file:
* * * * * lockfile -r 0 /tmp/the.lock && php get_tweets.php; rm -f /tmp/the.lock
Putting no value in each column defaults to:
Every Minute Of Every Hour Of Every Day Of Every Month All Week Long, --> Every Minute All Year Long.
As Davidann states using a lockfile ensures that only one copy of the php interpreter runs,
php parse_tweets.php
is the command to "run" the file, and the last bit of the line will delete the lock file to get ready for the next run. I don't like deleting a file every minute, but if this is the behavior you need, this is very acceptable. Writing and Rewriting to disk is just personal preference