When a process launched become hanged, the most useful option to kill it is to press
Ctrl+C, but what to do when Ctrl+C doesn’t work?
In this cases it is very useful to use Ctrl+Z. While Ctrl+C tries to kill the process with the signal SIGINT, Ctrl+Z sends the process to the background, releasing the terminal that was inoperative because the process, and giving us the control again.
Let’s see how to do it using an example. (At the end of the post you can find a video explaining all the process)
To simulate a hang process I am going to launch the python script ignoring_CtrlC.py, that intercept the Ctrl+C signal, making a forever process.
# Handler Ctrl+C pressed buttons, to don't allow the script to terminate
def signal_handler(signal, frame):
print(' Sorry, I\'m too busy ignoring you!')
# MAIN PROGRAM
if __name__ == "__main__":
#to capture te Ctrl+C signal
value = True
print "Running forever"
When I execute the script, and press Ctrl+C this is what happen
Ctrl+C is not working, instead is displaying a message.
Let’s press now Ctrl+Z and see what happen now
Killing a background process
To kill a background process, we use the command kill, but instead of use the PID of the process, we have to use the background ID, that is indicated between .
kill -9 %1
The process is killed and the terminal control recovered 🙂