Re: self detecting script
by merlyn (Sage) on Sep 15, 2005 at 17:49 UTC
|
| [reply] |
Re: self detecting script
by phaylon (Curate) on Sep 15, 2005 at 17:44 UTC
|
| [reply] |
|
|
The only problem with this type of an approach is if the guts of your script are long and complex, having multiple exit points or failure modes, such as "do this or die" or "do this or croak". It gets even worse if you are using some other module which has a "do or die" type function. This way your program will exit without cleaning up the lockfile.So, the obvious solution to this is to put the lockfile cleanup into an END block. However, even that will not work all the time. For example, if you are using a module which was compiled from an XS and that module takes a core dump, you will leave the lockfile behind. All this is to say that lockfiles have their weaknesses. One way to overcome these flaws is by trying to run your program and checking the presence of the lockfile for, say, average duration of script times a factor of 2. This way, if the lockfile still exists, then it must be a stray file which should be deleted so the script can run.
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
| [reply] |
Re: self detecting script
by ruoso (Curate) on Sep 15, 2005 at 17:45 UTC
|
The standard way of doing that is creating a lockfile which contains the pid of the proccess. The proccess will remove the file when exit.
When checking, the new instance will see if the file exists, and if exists, check if the pid stored in the file is still alive (a kill -0 $pid will do it).
If, for some reason, the last proccess didn't remove the lockfile, it will be detected and the next proccess will continue, overriding the lockfile.
| [reply] |
Re: self detecting script
by saintmike (Vicar) on Sep 15, 2005 at 17:52 UTC
|
| [reply] |
Re: self detecting script
by tcf03 (Deacon) on Sep 15, 2005 at 18:18 UTC
|
die "a horrible death" if ( -f "lockfile");
open LOCK, ">", "lockfile" or die;
close (LOCK);
... do some stuff ...
unlink("lockfile");
Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
--Ralph Waldo Emerson
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
|
|
Probably not ideal, but this is almost a not too bad method when you know that separate instances of the same program will always start not less than a minute apart. There should be no race conditions. But if for some reason the previous instance never unlinks the file, no subsequent instance will run, which may or may not be what you want. Or if you manually start up the process at the same time cron kicks it off, you may run into a race condition. It may be best to open the file unconditionally and then use lock to get an exclusive lock on the file. Or use one of the previously mentioned modules to do that for you.
I'll also mention that we do that sort of thing alot in our shell scripts, but it's the only solution since we can't call lock from the shell (also perl is not installed on many of our customer's systems). Unfortunately, we also use this method in shell scripts outside of cron where race conditions are possible, and some sort of file lock would be better.
| [reply] |
Re: self detecting script
by tweetiepooh (Hermit) on Sep 16, 2005 at 12:18 UTC
|
Instead of cron we often use a sub-shell type mechanism. It doesn't do quite what you want but it sort of does.
#!/bin/sh
(while true
do
<script>
sleep 60
done ) &
This shell will spawn a second shell in memory that will execute your script and once ended (for any reason) restart it in 60 secs. So it's not running on the minute but we find it often does the job. | [reply] [d/l] |