Issue
I'm starting a number of screens in a bash script, then running django's runserver
command in each of them. I'd like to be able to programmatically stop them all as well, which requires me to send Control+c
to runserver
.
How can I send these keystrokes from my bash script?
Solution
Ctrl+C sends a SIGINT
signal.
kill -INT <pid>
sends a SIGINT
signal too:
# Terminates the program (like Ctrl+C)
kill -INT 888
# Force kill
kill -9 888
Assuming 888
is your process ID.
Note that kill 888
sends a SIGTERM
signal, which is slightly different, but will also ask for the program to stop. So if you know what you are doing (no handler bound to SIGINT
in the program), a simple kill
is enough.
To get the PID of the last command launched in your script, use $!
:
# Launch script in background
./my_script.sh &
# Get its PID
PID=$!
# Wait for 2 seconds
sleep 2
# Kill it
kill $PID
Answered By - Matthieu Napoli Answer Checked By - Marilyn (WPSolving Volunteer)