Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a PID trap for multiple commands separated by &&

I'm running Stardog 4.0 in an Ubuntu 15.04 container with open jdk8 which runs fine. I want to handle gracefully shutting down stardog using a trap.

To execute stardog in a container so that it continues to run I've been using the following which works well

$SBIN/bin/stardog-admin server start && (tail -f /storage/stardog.log &) && while (pidof java > /dev/null); do sleep 1; done

Without the following the container will run for a few seconds and stop

&& (tail -f /storage/stardog.log &) && while (pidof java > /dev/null); do sleep 1; done

However this leads to multiple PID's within the container

root         1  0.0  0.0  18384  3236 ?        Ss   14:04   0:02 /bin/bash /stardog_binaries/startup.sh
root        45  0.1  4.9 4619052 406940 ?      Sl   14:04   0:10 java -Xms2g -Xmx2g -XX:MaxDirectMemorySize=4g -XX:SoftRefLRUPolicyMSPerMB=1 -XX:+UseParallelOldGC -XX:+UseCompressedOops -Djavax.xml.datatype.DatatypeFactory=org.apache.xerc
root        97  0.0  0.0   4412   740 ?        S    14:04   0:00 tail -f /storage/stardog.log
root     12108  0.2  0.0  18184  3100 ?        Ss   15:44   0:00 bash
root     12122  0.0  0.0   4376   784 ?        S    15:44   0:00 sleep 1

And the following trap does not work to shutdown stardog when the container is shutting down

trap 'kill -TERM $PID' TERM INT
$SBIN/bin/stardog-admin server start && (tail -f /storage/stardog.log &) && while (pidof java > /dev/null); do sleep 1; done
PID=$!
wait $PID
trap - TERM INT
wait $PID
EXIT_STATUS=$?

So my question is how do I properly trap the $SBIN/bin/stardog-admin server start such that when the container shuts downs, stardog gracefully exits.

Regards Conteh

like image 346
conteh Avatar asked Oct 30 '22 14:10

conteh


1 Answers

You can use a dedicated image made to handle that kind of issue, like phusion/baseimage-docker.

See "PID 1 zombie reaping issue": by declaring your script as a daemon, you will make sure the image handles the shutdown signal properly.

like image 154
VonC Avatar answered Nov 15 '22 07:11

VonC