Skip to content

Instantly share code, notes, and snippets.

@iocanel
Created September 11, 2015 13:30
Show Gist options
  • Save iocanel/0a33dc5c89e06e0dcec9 to your computer and use it in GitHub Desktop.
Save iocanel/0a33dc5c89e06e0dcec9 to your computer and use it in GitHub Desktop.
Signal trapping in Docker tests
#!/bin/bash
function cleanup {
echo "Cleaning Up!"
echo "Container $HOSTNAME exited" > /path/to/volume
}
echo "My pid is $$"
trap 'kill ${!}; cleanup' SIGTERM
trap 'kill ${!}; cleanup' SIGKILL
trap 'kill ${!}; cleanup' SIGINT
//hide dind stuff so Jimmi won't find out.
sleep infinity
@rhuss
Copy link

rhuss commented Sep 11, 2015

Works for me with this dockerfile

FROM centos

ADD x.sh /
RUN chmod 755 /x.sh
ENTRYPOINT [ "/x.sh" ]

and this x.sh

#!/bin/bash

function cleanup {
echo "Cleaning Up!"
echo "Container $HOSTNAME exited" > /path/to/volume
}

sleep 1000000 &

echo "My pid is $$"
trap 'kill ${!}; cleanup' SIGTERM
trap 'kill ${!}; cleanup' SIGKILL
trap 'kill ${!}; cleanup' SIGINT

sleep 10000000

@jimmidyson
Copy link

This works for me:

#!/bin/bash                                                                     
trap 'echo TRAPed signal' TERM INT                                              

pid=                                                                            

function cleanup {                                                              
echo "Cleaning Up!"                                                             
kill $pid TERM                                                                  
echo "Container $HOSTNAME exited"                                                                                                                                                             
}                                                                               

echo "My pid is $$"                                                             

sleep 10000000 &                                                                
pid=$!                                                                          
wait

@rhuss
Copy link

rhuss commented Sep 11, 2015

yeah, you're the man ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment