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...


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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.