1

I have a script that is launched by cron every day.

The launch command looks something like this: flock -n /tmp/mylock.LOCK /path/to/script/script.sh

Script script.sh updates configuration files for my server and tries to restart it (if it is running, it restarts it, if not - launches it). But the problem is that this server starts holding the lock /tmp/mylock.LOCK, which prevents script.sh from launching the next day.

Can I somehow run the script to restart my server without acquiring the file lock in the process?

OS: Ubuntu 12.04

Anton Guryanov
  • 133
  • 1
  • 8
  • Why is the lock needed for the script? How do you (re)start your server? – user236012 Mar 03 '15 at 12:38
  • The lock is there so that if (for any reason) the configuration update part of the script gets stuck, the second instance won't launch (two simultaneous configuration updates are really bad). The server is just a python program, I just stop in and then start it again (with the help of another script). – Anton Guryanov Mar 03 '15 at 13:54

1 Answers1

1

There is a -o option, I missed it on flock man page the first time.

Quote from the man page:

-o, --close
     Close  the file descriptor on which the lock is held before exe‐
     cuting command.  This  is  useful  if  command  spawns  a  child
     process which should not be holding the lock.

So it does exactly what I need, now only the parent flock process holds the lock.

Anton Guryanov
  • 133
  • 1
  • 8