I have an application capturing the output of two webcams.

For each webcam, cron runs a script to upload the latest image to a remote server. Cron calls the same script to upload images at the same time. Typically two instances of the script will be running. The only difference between them is an argument appended to the cron command so that the scripts know which webcam (1 or 2 ) it is dealing with. In effect, there are two parallel streams of data.

The image upload is over the wireless cellular network. It is therefore possible that a slow connection will prevent one or both scripts completing before cron calls the scripts again. If this happens, I want to allow the running script(s) to complete, and abort the latest attempt to upload an image.

The system is located on a physically remote site so it must be able to recover gracefully from an unexpected power loss or other disturbance.

So my problem is, how do I allow no more than one instance for each webcam to run?

I have found a number of solutions to prevent multiple instances. I haven't found a solution that prevents multiple instances of parallel streams.

If I use lock files, then I would need a time out to clear the lock file if it is stuck.