In the code snippet below, I want the script to be limited to only running one copy at a time, and for it to restart server.x if it dies for any reason.
Without flock involved, the loop correctly restarts if I kill the server process, but once I use flock to ensure the script is only running once, if I kill server.x it also kills the parent process.
How can I ensure that killing the child process in a flock script keeps the parent around?
#!/bin/bash
set -e
(
flock -x -n 200
while true
do
./server.x $1
done
) 200>/var/lock/.m_rst.$1.lock
Best Answer
I think that the lock file might be bothering the subprocess, causing it to terminate with a non-zero exit status:
set -e
will cause your script to terminate if one of the commands inside exit non zero for example:Outputs:
It's also worth noting that the flock(2) manpage indicates that the
flock
system call will copy the file handle to forked processes and across execve; this might mean thatserver.x
is unhappy having the lock file open. In the following example the lock will be held by both processes:Executing this test while performing
lsof | grep /tmp/test.lock
will show the lockfile is opened by both processes.