How can I wrap an executable in UNIX (SunOS) so that it never runs more than once at the same time?
I have an executable (no source) that I need to wrap to make sure it doesn't get called more than once at a time. I immediately think of some kind of queue wrapper, but how can I make it so that my wrapper gets called instead of the executable itself? Is there a better way to do this? The solution should be invisible because the users are other applications. Any information / advice is appreciated.
a source to share
Method 1. Place the executable file in a certain location, not in the standard path. Create a shell script that checks the sentry file and, if the sender file is missing, executes the program, waits for ptogram to complete, and then deletes the watch file. If a watch file is present, the script will go into a loop with a short delay (1 second? How long does the standard execution of this program take? Take that and half of it), check the available file again, etc.
Method 2: Create a separate program that does the same as the script, but instead uses a system-level semaphore or locking. You can even just use a read / write lock on the file. The program would fork () and exec () in a real program, waiting for the child to exit before clearing the sentinel.
a source to share
If the users are other applications, you can simply rename the executable (eg name -> name.real) and invoke the shell with the original name. To make sure it only gets called once at a time, you can use a command pidof
(e.g. pidof name.real
) to check if the program is already running ( pidof
actually gives you the PID of the current process, so you can use things like kill
or whatever to send signals to it).
a source to share