Check if a php script is still running

十年热恋 提交于 2019-12-21 07:37:56

问题


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:

  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.



回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!