There is a standard method, if the programs cooperate. Run kill -- -42
where 42 is the pid of the parent process. This sends a signal to all the processes in the process group lead by process 42 (the minus sign before the pid means process group).
Normally, if you run your python script from a shell prompt and it simply forks gnuchess
, the two processes should remain in the same process group. But this doesn't seem to be the case, since Ctrl+C sends SIGINT
to the whole foreground process group.
Gnuchess might be in its own process group because it made itself a session leader (but I don't know why it would do this), or because you've double-forked it (python forks a shell which forks gnuchess). A double fork is probably avoidable, but I can't tell you how without seeing your code.
A reasonably reliable and POSIX-compliant way of finding the pid of the gnuchess
process is
gnuchess_pids=$(ps -A -o pid= -o cmd= | awk '$2 ~ /(^|\/)gnuchess$/ {print $1}')
Specific unix variants may have better ways of achieving this, such as pgrep
.
pkill
to kill a process by name, so yourps aux | grep gnuchess
andkill -9 PID
can just becomepkill -9 gnuchess