Re: use flock() to determine if other script is running
by diotalevi (Canon) on Aug 20, 2003 at 22:23 UTC
|
No one else mentioned this so I will - you are not using LOCK_UN safely. Under 99.9% of all circumstances you will want to have close() handle that for you. I didn't say 100% because while I don't know of any case where you actually would use LOCK_UN, I suppose it exists for a reason so there must be some reason, however obscure and remote, that it exists. Just don't do it.
| [reply] |
|
|
Thank you for your guidance.
| [reply] |
Re: use flock() to determine if other script is running
by valdez (Monsignor) on Aug 20, 2003 at 21:29 UTC
|
Can't you use your lock file to store this information? Of course you will need to keep track of every birth and death. Another possible approach is to drop a file every time you start a process in a predefined directory and count them to know how many processes are running. This kind of counter can use the timestamp of files to know if a process aborted (you need to simulate an heartbeat, updating mtime of your files periodically).
There is also Only let one copy of program run at a time, published today by esh, and Parallel::ForkManager.
Ciao, Valerio
| [reply] |
Re: use flock() to determine if other script is running
by Zaxo (Archbishop) on Aug 20, 2003 at 21:28 UTC
|
The semantics for nonblocking flock are to return false if the lock was not obtained, true if it was. If the platform does not support flock a fatal error is thrown.
You are apparantly on a win32ish system, which may or may not support flock, so it would be good to catch the error. See perlport and perlwin32 for details. Try something like this,
use Fcntl ':flock' ;
open ONE, ">> C:\\temp\\RTEOlock.log" or die $!;
my $lck = eval {flock ONE, LOCK_EX | LOCK_NB};
die 'flock not supported! ', $@ if $@;
if ($lck) {
# The other script is not running code here
}
else {
# The other script is running code here
}
For this semaphore system to be effective, the other script should flock the flag file first thing, and drop the lock at the very last, perhaps in a DESTROY() sub.
After Compline, Zaxo | [reply] [d/l] |
|
|
| [reply] |
|
|
Perhaps for VMS and suchlike, I don't know. On AS build 630 (perl 5.6.1)/Windows95, which does not support flock, my code gives:
C:\download>perl -c flock.pl
flock.pl syntax OK
C:\download>perl flock.pl
flock not supported! flock() unimplemented at flock.pl line
at which point winders wrote off the edge of the screen and lost the line number ;-)
My 'fatal error' jargon came directly from 'perldoc -f flock'.
After Compline, Zaxo | [reply] [d/l] |
|
|
Thank you, Perl Monks, for providing the venue where such novice questions can be aired and Zaxo, thank you for the code snippet. As someone that is struggling to learn Perl and has no mentor to turn to I do appreciate your investment in me.
Here is a brief description of what I am trying to do. Script ONE is activated by a message console and passed some message text which it turns into arguments against two SQL tables taking the extracted data and placing it in a data repository table then checks to see if script TWO is running. if no, starts it then in either case, script ONE ends.
Script TWO checks the repository table for an elligible candidate, when found writes it to Remedy then ends. All this to be done by a guy who as yet does not have a firm grasp of Perl basics hence my great appreciation for this site(it gives me hope).
| [reply] |
Re: use flock() to determine if other script is running
by tcf22 (Priest) on Aug 20, 2003 at 21:18 UTC
|
If all that you need to know is if another script is running, why not instead of using flock, just create an empty file and check if it exists.
Script 1:
my $file = 'c:\temp\running.tmp';
open(RUNNING, ">$file");
close(RUNNING);
##Do your stuff
unlink($file);
Script 2:
my $file = 'c:\temp\running.tmp';'
if(-e $file){
print "Running\n";
}else{
print "Not Running\n";
}
| [reply] [d/l] [select] |
|
|
That won't work... two processes can both look to notice a file doesn't exist, then both "create" it.
To properly lock, you must have an atomic "test-and-set" operation that
is uninterruptable by another process.
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.
| [reply] |
|
|
I realize that there is a mutual exclusion issue. Looking at the original post, he asked how to tell if the other process was running. Perhaps a better question would have been, how can I check if another process is in a critical section of the code. Normally with threads, you can use a semaphore. I guess I didn't look enough into the question, I just answered the one that was posed.
| [reply] |
|
|
| [reply] |
|
|
Thank you very much for your input.
| [reply] |
Re: use flock() to determine if other script is running
by mr_stru (Sexton) on Aug 21, 2003 at 07:46 UTC
|
I'd recommend having a look through Lincoln Stien's Network Programming with Perl as that has some stuff on writing code to do this sort of thing.
The other thing I'd say is that really make sure that anything that might cause the script that creates the lock file to fall over is caught in such a way that the lock file is deleted. Otherwise it will look like your script is running when it's not.
Struan
| [reply] |