1

I read this interesting question, that basically says that I should always avoid reaching PID of processes that aren't child processes. It's well explained and makes perfect sense.

BUT, while OP was trying to do something that cron isn't meant to, I'm in a very different situation :

  1. I want to run a process say every 5 minutes, but once in a hundred times it takes a little more than 5 minutes to run (and I can't have two instances running at once).
  2. I don't want to kill or manipulate other processes, I just want to end my process without doing anything if another instance of the process is running.

Is it ok to fetch PID of "not-child processes" in that case ? If so, how would I do it ?

I've tried doing if pgrep "myscript"; then ... or stuff like that, but the process finds its own PID. I need to detect if it finds more than one.


(Initially before being redirected I read this question, but the solution given doesn't work: it can give pid of the process using it)

EDIT: I should have mentioned it before, but if the script is already in use I still need to write something in a log file, at least : date>>script.log; echo "Script already in use">>script.log", I may be wrong but I think flock doesn't allow to do that.

Community
  • 1
  • 1
Teleporting Goat
  • 417
  • 1
  • 6
  • 20
  • 2
    Basically, you want to make sure there is only one instance of your process at any time. I recommend using a lock file to implement mutual exclusion. See this question for lock related discussion: http://stackoverflow.com/questions/41275785/bash-how-to-implement-locking-in-a-multi-process-system/41275952#41275952 – codeforester Dec 22 '16 at 16:17
  • @codeforester Every time the script is called I write some stuff in a log file. I can't write the date + "script already in use" if the script is locked, can I ? – Teleporting Goat Dec 23 '16 at 10:51

1 Answers1

1

Use lckdo or flock to avoid duplicated running.

DESCRIPTION
       lckdo runs a program with a lock held, in order to prevent multiple
       processes from running in parallel. Use just like nice or nohup.

       Now that util-linux contains a similar command named flock, lckdo is
       deprecated, and will be removed from some future version of moreutils.

Of course you can implement this primitive lockfile feature by yourself.

if [ ! -f /tmp/my.lock ];then
     touch /tmp/my.lock
     run prog
     rm -f /tmp/my.lock
fi
Ipor Sircer
  • 3,069
  • 3
  • 10
  • 15
  • 1
    your "implementation" has a race condition. – Karoly Horvath Dec 22 '16 at 16:24
  • it's only an example which shows the way. He should use `flock` or `lckdo`. – Ipor Sircer Dec 22 '16 at 16:25
  • 1
    Edit the question then. That code is dangerous as it will work *most* of the time. – Karoly Horvath Dec 22 '16 at 16:29
  • Can you explain the race condition of this implementation ? I'm missing what could go wrong here. (Assuming I'll only be calling it from cron) – Teleporting Goat Dec 22 '16 at 16:42
  • He mention that there is a little chance you already have file named `/tmp/my.lock`. – Ipor Sircer Dec 22 '16 at 16:45
  • imagine a process has done the check ( `-f /tmp/my.lock` ) and found no file, but haven't created a lock file *yet*. If at this point another process does the check, it won't see a lockfile... so it proceeds. – Karoly Horvath Dec 22 '16 at 19:31
  • 1
    Fair enough. But in this case, the process is only called by cron, every five minutes, so this problem can't happen. I understand it can in other contexts, but not in mine, so why should I edit the question ? – Teleporting Goat Dec 23 '16 at 09:58
  • Because the explanation you just gave is missing from the answer. Anybody reading it will probably assume that it the impementation can be used in all cases. Certainly that's what the wording suggests. In short: because it's *misleading*. – Karoly Horvath Dec 23 '16 at 18:46