I read with interest, a thread started by Nitrox over 4 yrs ago, about detecting whether there is another copy of a script already running. If I am only needing to run on a Linux system, are there any disadvantages to doing something like this instead of using locks as described in that thread?
sub ProcessRunning {
# If the passed string exists in the current Unix/Linux process list,
# then this subroutine returns the lines from a "ps -e" command which
# contain that string. These lines will look something like
# "26787 pts/1 00:00:00 TestLoop.pl". If more than one running
# process line contains that string, each of those lines will be
# included in the returned value. Since this is Perl, these
# returned values can be treated as TRUE if desired.
# Make sure to specify a string that will uniquely locate the
# processes of interest. Alternatively, the returned value could then
# be further examined for other analysis.
#
# If the string does NOT exist, an empty value is returned, which
# can be treated as FALSE.
#
# Sample usages:
# exit if &ProcessRunning($myProcess);
# OR
# &ProcessRunning($myProcess) and die
# "Another copy of process $myProcess is already running\n";
# OR
# $result = &ProcessRunning($myProcess); # Examine $result for
# further processing
return 0 unless (`ps -e | grep @_ | grep -v grep`);
} # end sub ProcessRunning
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.