0

I liked SensorSmith answer at Forward SIGTERM to child in Bash, but for my systemctl script, it resulted in a double SIGTERM, because systemctl sends SIGTERM to the parent bash instance AND the child I am running. The result was instant termination before my child could gracefully shut down.

What's the proper way to handle SIGTERM in a bash script based service?

0

So I ended up with this and I am happy:

#!/bin/bash

script_term()
{
    echo $0 SIGTERM
}

wait_for_child()
{
    term_child_pid=$!
    wait ${term_child_pid} 2>/dev/null
    trap - TERM INT
    wait ${term_child_pid} 2>/dev/null
}

trap 'script_term' TERM INT
node index.js
wait_for_child

It waits properly in all the cases I care about, though it does not SIGTERM the child when the parent bash is SIGTERM'd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.