How to create a PID trap for multiple commands separated by &&

I am running Stardog 4.0 in an Ubuntu 15.04 container with open jdk8, which is working fine. I want to handle gracefully closing the stardog using a trap.

To execute stardog in a container so that it continues to work, I used 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 several seconds and stop

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

However, this results in multiple PIDs in 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 turn off stardog when closing the container

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 to correctly capture the start of the $ SBIN / bin / stardog-admin server so that when the container is closed, it will fail due to an elegant exit.

Relations Conte

+4
2

runstardog.sh script .

set -e
function shutdown() {        
 $STARDOG_BINARIES/bin/stardog-admin server stop  -p xxx -u admin    
}

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

trap "{ shutdown }" EXIT
exit 0

startup.sh script,

trap 'kill -TERM $PID' TERM INT
$STARDOG_BINARIES/runstardog.sh &
PID=$!
wait $PID
trap - TERM INT
wait $PID
EXIT_STATUS=$?

, , stop.sh , , CID stardog, stardog docker exec .

replace="\1"
find="([a-z0-9]{12})\s+platform_stardog.*"
CID=$( docker ps | tail --lines=+2 | sed "/platform_stardog/!d"  | perl -lpe  "s@$find@$replace@g"  )

if [ -z "$CID" ]; 
then
    echo "No existing container named platform_stardog was found"
else
    echo "docker exec  /stardog_binaries/stopstardog.sh in platform_stardog Container $CID:"
    docker exec ${CID} /stardog_binaries/stopstardog.sh
    echo "stop and delete docker container."
    /usr/local/bin/docker-compose -p platform -f docker-compose.yml stop -t 30 stardog && /usr/local/bin/docker-compose -p platform -f docker-compose.yml rm --force -v stardog
fi

, - " Stardog " . 30 , , -t 30

@VonC .

+1

Source: https://habr.com/ru/post/1617764/


All Articles