Bod has asked for the wisdom of the Perl Monks concerning the following question:
Because the Raspberry Pi does not have an onboard time clock (thanks Marshall for pointing this out), the first thing my script does is to check that it has a valid time that has been obtained from the network. It does this by comparing the current year as given by localtime to 2020. I've written some code which is probably unnecessary in this application but I'm asking about this as a wider learning point.
How can I prevent more than one instance of a script from running?
I have at times used a lockfile but there is always the risk that the power could go off or some other catastrophe could occur between the lockfile being created and being removed. So is there a better way to do it?
This is the code to check is the RPi has a valid date and to wait ever increasing intervals before checking again and eventually to reboot as a possible cause after this amount of time is that the WiFi 'card' hasn't properly initialised.
my @time = localtime; $control->log("Starting Curtain Controller") if $DEBUG > 1; # Check Pi has valid time my $wait = 1; while ($time[5] + 1900 < 2020) { if ($wait > 30) { $control->log("Still no valid time. Aborting!"); sleep(2); system("sudo reboot"); exit 0; } my $plural = $wait == 1?'':'s'; $control->log("No valid time. Waiting $wait minute$plural"); print "Waiting for $wait minutes$plural\n" if $DEBUG; sleep ($wait * 60); $wait = int(0.5 + $wait * 1.5); @time = localtime; }
At present I am unable to install any modules from CPAN although this will be solved in time - it is more important to get this project working, built in a nice box and set up in its new home before Christmas.
As this Raspberry Pi script is running from CRON every 2 minutes, the delay code is probably not necessary. Instead I will just make the check and if the time is invalid, log that and exit. 2 minutes later it will try again anyway. But I would be interested to learn of other ways of preventing two instances of the same script running because not all scripts run so regularly from cron as this one does.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Preventing multiple instances
by stevieb (Canon) on Dec 16, 2020 at 22:01 UTC | |
by Bod (Hermit) on Dec 17, 2020 at 23:07 UTC | |
by afoken (Canon) on Dec 18, 2020 at 00:27 UTC | |
by Bod (Hermit) on Dec 18, 2020 at 14:38 UTC | |
by afoken (Canon) on Dec 18, 2020 at 16:33 UTC | |
Re: Preventing multiple instances
by jszinger (Scribe) on Dec 16, 2020 at 22:32 UTC | |
Re: Preventing multiple instances
by eyepopslikeamosquito (Bishop) on Dec 17, 2020 at 08:32 UTC | |
Re: Preventing multiple instances
by hippo (Chancellor) on Dec 16, 2020 at 22:00 UTC | |
by jcb (Parson) on Dec 17, 2020 at 05:18 UTC | |
by jeffenstein (Friar) on Dec 18, 2020 at 10:21 UTC | |
by Bod (Hermit) on Dec 17, 2020 at 18:38 UTC | |
Re: Preventing multiple instances
by davido (Cardinal) on Dec 17, 2020 at 21:29 UTC | |
Re: Preventing multiple instances
by afoken (Canon) on Dec 18, 2020 at 00:13 UTC | |
Re: Preventing multiple instances
by Anonymous Monk on Dec 17, 2020 at 09:03 UTC | |
Re: Preventing multiple instances
by Anonymous Monk on Dec 17, 2020 at 08:39 UTC | |
Re: Preventing multiple instances
by perlfan (Vicar) on Dec 18, 2020 at 17:50 UTC |