Question

We are using Upstart to launch/terminate an in-house developed binary.

In the Upstart configuration file for this binary, we define the script as such:

script
   exec su - user -c "screen -D -m -S $product /opt/bin/prog /opt/cfg/$product -v 5 --max_log_size=7"
end script

When the runlevel is set to 5, Upstart launches the script. When the runlevel is set to 3, Upstart terminates the script.

My problem, is Upstart is sending a SIGTERM and then a SIGKILL.

The SIGTERM is being 'handled' by screen, and not by my custom binary, so the signal handlers in our binary dont get the SIGTERM, and thus, cannot shut down cleanly.

I've verified that the signal handlers in our binary do allow it to shut down cleanly when it is NOT launched via screen.

Was it helpful?

Solution

Turns out I had to approach this from a different perspective, and handle it via Upstart. The addition of a pre-stop script, allowed me to identify the Screen session, and then stuff in the commands ("quit\n" and then "y\n") to cleanly shut down the binary that Screen was running.

pre-stop script
   SESSID=`ps -elf | grep '/opt/bin/prog /opt/cfg/$product' | grep SCREEN | awk '{print $4}'`
   QUIT_CMD="screen -S $SESSID.$product -X stuff \"exit"$'\n'"\""
   exec `su spuser -c "$QUIT_CMD"`
   QUIT_CMD="screen -S $SESSID.$product -X stuff \"y"$'\n'"\""
   exec `su spuser -c "$QUIT_CMD"`
   sleep 20
end script
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top