0

I'm using watch in a screenwindow to continually execute a PHP task which processes data every two seconds. Thus, I can logout of the server with it running and can check-in on it. I don't want to stop the task mid stream because it requires manual cleaning up if that happens, but I'd like to be able to reboot the server. Is there a way to stop watch between runs and be sure it hasn't started another copy of the task? Ie something like ctrl+c but that waits if the task is running in the background.

Alternatively, is there some other workflow that would accomplish this? I can't use a cron job since it needs to run every two seconds but never concurrently.

Also note, one run of the script takes between 10 seconds and 30 minutes depending on how big the data object is. (It processes one object per run.) This is why stopping watch between runs is very unlikely...

Loren
  • 233
  • 2
  • 8

1 Answers1

6

Just make a little bash script that loops as long as a file doesn't exist:

while [ ! -e /tmp/stoploop ]; do
    some_program
    sleep 2
done

Then touch /tmp/stoploop and it will stop looping.

DerfK
  • 19,493
  • 2
  • 38
  • 54