The best way to ensure only 1 copy of bash script is running?

Posted by depesz on Stack Overflow See other posts from Stack Overflow or by depesz
Published on 2009-11-11T13:21:24Z Indexed on 2010/05/19 12:00 UTC
Read the original article Hit count: 226

Filed under:
|
|

What is the simplest/best way to ensure only 1 copy of given script is running - assuming it's bash on linux?

At the moment I'm doing:

ps -C script.name.sh > /dev/null 2>&1 || ./script.name.sh

but it has several issues:

  1. it puts the check outside of script
  2. it doesn't let me run the same script from separate accounts - which I would like sometimes.
  3. -C checks only first 14 characters of process name

Of course I can write my own pidfile handling, but I sense that there should be some simple way to do it.

© Stack Overflow or respective owner

Related posts about linux

Related posts about bash