开发者

Check if a php script is still running

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 run开发者_StackOverflowning?

Some solutions have been posted here, but those are all for Linux, while I am looking for a Windows solution. Any ideas please? Thanks.


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.


Here's what I usually do:

  1. create a status directory, eg: /data/status/
  2. on my script, have it check if status file is exists
    1. if it does exist, exit script
    2. 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:

  1. For every minute, update status file
  2. 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜