Re: How many scripts running?
by Corion (Patriarch) on Dec 15, 2019 at 08:37 UTC
|
If you only want a single instance of your script running, the easiest approach is to lock the script file itself while the script is running. See Highlander for that approach.
Of course, the locking needs to be done in the top-level invocation, so the best approach is to have a separate top-level script that gets invoked and that locks itself.
| [reply] |
|
|
How many copies i want to run - lets say i will define it in config file for this script
| [reply] |
Re: How many scripts running?
by Anonymous Monk on Dec 15, 2019 at 21:19 UTC
|
One somewhat-clever way is to populate a directory with a series of files, the name of each of which is a consecutive number. Each script loops through the directory and locks the first one it can. It then runs, holding on to that lock for the entire time ... trusting that when it ends, the OS will clean up the lock as it closes all the process's files. If it cannot lock any file, it exits because too many instances must now already be running. Not perfect because it is subject to race conditions but fairly-okay for long running things. | [reply] |
|
|
Wow, I was wondering the same thing. And creating files seems to be a great idea. Perhaps these files could be stored on a ramdisk where they are easily accessed and flushed each time the computer is restarted.
| [reply] |
|
|
The Anon Monk's idea is reasonable for a small number. No, these files aren't stored on RAM disk. They are static and do not change. I would create these files with 0 bytes with the number of files equal to the number of max running processes. The file lock is an O/S concept, not part of the stored file system. A "lock" or "unlock" makes no difference upon the disk storage. All locks get released if the OS reboots because the locks are in memory, not on the hard drive. Something to consider is whether your lock gets released (or not) if your process exits without explicitly releasing it.
| [reply] |
|
|
Wow, it is a fresh idea!!!
Just to confirm (i don't know yet) - if process will die becasue of some errors - the lock wil lbe removed by OS?
| [reply] |
|
|
I would think so, too. However, there's the possibility of a "defunct" process (Unix calls this a Zombie process). If these occur seldom enough, you might ignore the situation (or solve it manually when it occurs).
| [reply] |
|
|
| [reply] |