2016년 5월 14일 토요일

When Ctrl-C fails, use Ctrl-Z: Finding and Killing the pid of a runaway shell script

Recently I was writing a bash shell script with a while-do loop. The loop exit condition failed to be satisfied so the script was stuck in an infinite loop. Checking pidof bash and htop didn't give me conclusive results because I had several bash shells running and didn't know which pid to kill.

Pressing Ctrl-C in the terminal running the script failed to stop the runaway program. In such a case, press Ctrl-Z in the unresponsive terminal to background the current process and then type ps to get the  pid of all processes in the current terminal.

Doing this, I found the pid of my script foo.sh as well as the pid of all other commands being run by my script (nova, ping, neutron). Now just do a kill -9 pidOfScript and your runaway script will stop.