问题
I have a script that listens to a jabber server and responds accordingly. Though it's not supposed to stop, last night it did. Now I want to run a cron job every minute to check if the script is running, and start it if not.
The question is, how do I check if a particular script is still running?
Some solutions have been posted here, but those are all for Linux, while I am looking for a Windows solution. Any ideas please? Thanks.
回答1:
A quick and dirty workaround could be for the script to update a row in a database with a date column set to CURRENT_TIMESTAMP. Have the cron second script check if the timestamp of this row is recent.
回答2:
Here's what I usually do:
- create a status directory, eg: /data/status/
- on my script, have it check if status file is exists
- if it does exist, exit script
- if not exist, continue the process
However, there's a problem in this: What if script dies and it didn't remove status file?
You can add a little save check like this:
- For every minute, update status file
- For every cron start, check if status file is last updated eg: 10 or 30 minutes ago (we can assume that our cron script is die) we can start out cron script.
回答3:
I had the same problem and I solved with this code:
file_put_contents("status.txt","ping");
I put it inside the loop of my script
so, I check the last modified date of the file to see if the script is active
to not overload the HD you can do something like this:
$i++
if( $i%100 == 0)
file_put_contents("status.txt","ping");
this will write the file when $i == 100 or 200 or 300 etc...
this is a workaround
来源:https://stackoverflow.com/questions/2634379/check-if-a-php-script-is-still-running