Check if a php script is still running

試著忘記壹切 提交于 2019-12-04 01:26:47

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

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