in reply to Preventing multiple instances
How can I prevent more than one instance of a script from running?
Lockfiles!
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?
It's not necessarily the presence/absence of the lockfile that matters but rather the lock on that file. Locks don't persist through reboots so that problem is solved. See eg. the FAQ How can I lock a file?
Another file-based approach is a PIDfile. You store the PID of the running process in the file and on subsequent runs check that that PID does not exist (or doesn't relate to the process you are running). Still use locks on the PIDfile during access to avoid race conditions, of course.
🦛
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Preventing multiple instances
by jcb (Parson) on Dec 17, 2020 at 05:18 UTC | |
by jeffenstein (Hermit) on Dec 18, 2020 at 10:21 UTC | |
by Bod (Parson) on Dec 17, 2020 at 18:38 UTC |