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.

Community
  • 1
  • 1
Andy Kendall
  • 141
  • 1
  • 1
  • 10
  • 2
    You should add a comment beneath the answer if you need additional clarification. This shouldn't be a new question. – Tom Fenech Oct 28 '14 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. – tolanj Oct 28 '14 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. – Andy Kendall Oct 28 '14 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). – galaxy Oct 28 '14 at 13:33
  • @AndyKendall, do you care about whether its behavior is correct when two copies of the script are run very near the same time? That's a race condition, and an uncautious solution can give both copies approval to run at the same time. – Charles Duffy Oct 28 '14 at 13:36
  • 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`. – Charles Duffy Oct 28 '14 at 13:38
  • By the way, that solution has a bunch of other bugs (completely lacks quoting, for one); the race conditions are only one of many reasons to avoid it. – Charles Duffy Oct 28 '14 at 13:40
  • Hmmm, looks like flock it'll have to be then. Although it'll be VERY unlikely due to low numbers logged on at any one time (it's a router not a server), I can't afford to have any chance of killing the device by having lots of copies running simultaneously. Shame because I just got the "ps" checking working, and I know nothing about locking. – Andy Kendall Oct 28 '14 at 14:28
  • flock is not available to me....... – Andy Kendall Oct 28 '14 at 15:14

1 Answers1

0

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

  • 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". – syme Oct 28 '14 at 20:42