CTRL-C usually sends a SIGINT signal to a process, so you can simply:
kill -INT <processID>
from the command line (or script) to affect a specific processID .
I say “generally” because, as with most UNIX, this is almost endlessly configurable. If you execute stty -a , you will see which key sequence is bound to the intr signal. It will probably be CTRL-C , but this sequence of keys can be mapped to something else entirely.
The following script shows this in action (albeit with TERM , not INT , because sleep does not respond to INT in my environment):
#!/usr/bin/env bash sleep 3600 & pid=$! sleep 5 echo === echo PID is $pid, before kill: ps -ef | grep -E "PPID|$pid" | sed 's/^/ /' echo === ( kill -TERM $pid ) 2>&1 sleep 5 echo === echo PID is $pid, after kill: ps -ef | grep -E "PPID|$pid" | sed 's/^/ /' echo ===
It basically starts the sleep log watch process and captures its process id. He then displays the relevant details of the process before killing the process.
After a short wait, he then checks the process table to see if the process has passed. As you can see from the script output, it really disappeared:
=== PID is 28380, before kill: UID PID PPID TTY STIME COMMAND pax 28380 24652 tty42 09:26:49 /bin/sleep === ./qq.sh: line 12: 28380 Terminated sleep 3600 === PID is 28380, after kill: UID PID PPID TTY STIME COMMAND ===
paxdiablo Apr 26 '11 at 11:33 2011-04-26 11:33
source share