views:

173

answers:

3

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: 

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.

Danten
I can use that workaround as a last resort. Is there any Windows shell script that will do the job?
Nirmal
Windows shell scripts are less user friendly than JCL and that's saying something, so I'm afraid I can't help you there. You could try looking into PowerShell though as this seems to be useful.
Danten
A: 

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.
silent
A: 

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

Leo
How this is efficient than updating a timestamp field in a database? I think updating a db is better for it has its own locking mechanisms. Whereas we can end up in a deadlock if we depend on a file. Please correct me if I am wrong.
Nirmal
I have run many scripts on my notebook. To access the mysql from my desktop I would have to install PHP. This becomes unnecessary when I work with just a text file. But, each case should be treated separately.
Leo