1

continuing from Shell script: Ensure that script isn't executed if already running and with apologies: In the last solution, is "ps" enough or should it be "ps -ef" to show all running processes?

Sorry I don't have enough privilege to add a Comment to the previous Question.

9
  • 2
    You should add a comment beneath the answer if you need additional clarification. This shouldn't be a new question. Commented Oct 28, 2014 at 12:05
  • 3
    The flock solutions are better, the ps version is full of race conditions. whether ps is enough depends on how exclusive you need to be, if it needs to be exclusive across users / sessions then, YES, you need ps -e/ ps a. Also note ps is one of the most system dependant of commands, ie the output ps gives may be very different on Solaris to Linux to AIX so bear this in mind if that is relevant to you. Commented Oct 28, 2014 at 12:10
  • I think I understand the "race condition" scenario, and I doubt it would apply. I just need to ensure that no-one else has run the script before I'm trying to. Commented Oct 28, 2014 at 13:30
  • 1
    @AndyKendall, then you need to use some kind of locking, e.g. a symlink would do (e.g. create a symlink at the start of your script, set a trap to remove the symlink on termination). Creating a symlink is an atomic operation (on most FSes). Commented Oct 28, 2014 at 13:33
  • 1
    To answer your question directly: If you want to do this poorly, with ps, and care only about other instances run by the same user, ps alone is adequate. If you care about instances run by other users, yes, you'll want to add some arguments. In any event, though, if you actually need your script to behave reliably and correctly, don't rely on ps at all -- use flock. Commented Oct 28, 2014 at 13:38

1 Answer 1

0

You can find this script in this way: ps ax | grep scriptname

Sign up to request clarification or add additional context in comments.

1 Comment

With this command, even if grep finds the "scriptname", it does not mean the script is running. Example: it is just opened with "vim scriptname".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.