using flock in bash, why does killing a child process kill the parent process too?

Posted by Robby on Server Fault See other posts from Server Fault or by Robby
Published on 2011-02-07T04:03:22Z Indexed on 2011/02/07 7:27 UTC
Read the original article Hit count: 508

Filed under:

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

© Server Fault or respective owner

Related posts about bash