3 # docker stop trap signals
4 # https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86#.5d6q01x7q
10 # send SIGTERM also to the executed command in the docker container (the containernet topology)
12 function term_handler
() {
14 pid
=$
(pgrep
-f "$command" |
sed -n 1p
)
17 # avoid that the process triggers its own handler by sending sigterm
18 if [ $pid -ne 0 ] && [ $term_recvd -eq 0 ]; then
19 echo "sigterm received"
27 # do some manual cleanup
28 # remove all containers started by son-emu
29 docker ps
-a -q --filter="name=mn.*" |
xargs -r docker
rm -f
30 # cleanup remaining mininet
34 exit 143; # 128 + 15 -- SIGTERM
38 # on callback, kill the last background process, which is `tail -f /dev/null` and execute the specified handler
39 trap 'term_handler' SIGTERM
42 service openvswitch-switch start
44 if [ ! -S /var
/run
/docker.sock
]; then
45 echo 'Error: the Docker socket file "/var/run/docker.sock" was not found. It should be mounted as a volume.'
49 # this cannot be done from the Dockerfile since we have the socket not mounted during build
50 echo 'Pulling the "ubuntu:trusty" image ... please wait'
51 docker pull
'ubuntu:trusty'
53 echo "Welcome to Containernet running within a Docker container ..."
55 if [[ $# -eq 0 ]]; then
58 #remember command to send it also the SIGTERM via the handler