Skip to content

Instantly share code, notes, and snippets.

Created February 8, 2018 11:09
What would you like to do?
A Bourne Shell script that will run a command in the background and will send it a SIGTERM when it is received
# The script demonstrates how to terminate a background process. It will run
# the process and wait till a SIGTERM signal is received, then it will pass that
# along to the background process and return its status.
# Syntax: command-line
# SIGTERM-handler
term_handler() {
if [ $pid -ne 0 ]; then
kill -TERM "$pid"
wait "$pid"
exit $?
# setup handlers
# on callback, kill the last background process, which is `tail -f /dev/null` and execute the specified handler
trap 'kill ${!}; term_handler' TERM
# run application that was passed as arguments to the script
$@ &
# wait forever
while true
tail -f /dev/null & wait ${!}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment