The best way to kill running Bash scripts from Cygwin is using the Sysinternals tool PsKill64.exe
. The reason is that Cygwin's ps
also give PID's that are different from WINPID
.
In addition, pskill
also has the -t
flag that kills the entire process tree, which means any other threads/sub-processes that your script may have started. Of course kill -9 <PID>
also works, but it doesn't kill descendants already started by the script.
# cat sleeper.sh
ZID=$$
WINPID=$(cat /proc/${ZID}/winpid)
echo "WINPID: ${WINPID}"
sleep 10
Now run with:
$ ./sleeper.sh &
[1] 8132
WINPID: 8132
$ ps
PID PPID PGID WINPID TTY UID STIME COMMAND
#5280 1 5280 5280 ? 1001 14:21:40 /usr/bin/mintty
#7496 5684 7496 3836 pty0 1001 20:48:12 /usr/bin/ps
#5684 5280 5684 6052 pty0 1001 14:21:40 /usr/bin/bash
5948 8132 8132 3900 pty0 1001 20:48:11 /usr/bin/sleep
8132 5684 8132 8132 pty0 1001 20:48:11 /usr/bin/bash
As you see, this starts two processes, the main bash
script and the sleep thread. So if you kill -1 8132
the sleep thread will continue running, but if you use pskill64 -t 8132
, you will kill all its descendants too. Analogous to the linux killall
command.