Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How many scripts running?

by luxs (Beadle)
on Dec 15, 2019 at 08:19 UTC ( #11110170=perlquestion: print w/replies, xml ) Need Help??

luxs has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl-script which should be started automatically from cron. How can i check from this scripts, how many copies of this script already stated? At the moment i have only one idea, which works ,but looks not very nice 1. check process ID 2. read list of processes 3. find line with my ID and read how this scripts looks there (this is nesessary to avoid some mistakes related with non unique script name or other procesessa used this cript - for example editor) 4. find all identical lines in process list. lock or log file is not very good idea, becasue it is not protected from accidental script crash

Replies are listed 'Best First'.
Re: How many scripts running?
by Corion (Pope) 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.

      How many copies i want to run - lets say i will define it in config file for this script
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.
      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.
      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).

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11110170]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2020-07-16 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?