Skip to content

Instantly share code, notes, and snippets.

@kumichou
Created June 24, 2014 18:36
Show Gist options
  • Save kumichou/7312f01fc3128325986a to your computer and use it in GitHub Desktop.
Save kumichou/7312f01fc3128325986a to your computer and use it in GitHub Desktop.
Ever wanted to see how to do something simple like catch Ctrl-C in a BASH script? I'll illustrate how to start two different Python "apps" that would normally take over the terminal and stop them both
#!/usr/bin/env bash
CWD=`pwd`
function cleanup {
echo "You pressed Ctrl-C, time to exit the Python Services";
ps aux | grep myservice2 | grep -v grep | awk '{print $2}' | xargs kill -9;
ps aux | grep myservice1 | grep -v grep | awk '{print $2}' | xargs kill -9;
exit 1;
}
trap cleanup INT
cd ${CWD}/myservice1/myservice1-main.py &
cd ${CWD}/myservice2/myservice2-main.py
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment