in reply to How many scripts running?

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.

Replies are listed 'Best First'.
Re^2: How many scripts running?
by harangzsolt33 (Deacon) on Dec 15, 2019 at 23:37 UTC
    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.
      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.
Re^2: How many scripts running?
by luxs (Beadle) on Dec 16, 2019 at 08:07 UTC
    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?
      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).
Re^2: How many scripts running?
by Anonymous Monk on Dec 16, 2019 at 14:12 UTC

    This is the kind of crappy kludg I came up with when I was a novice programmer, too.