When ever I need to kill a background process I do ps -e | grep <process_name>
Which prints something like this 1766 ? 00:00:13 conky , Then I use the process ID to kill it like so kill 1766 .
Is there any way I can simplify this ? Make it quicker ? reduce the amount of typing ?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
(TL,DR: pgrep, pkill)
Many unix variants come with the pgrep and its companion pkill: Solaris, Linux (part of the standard process utilities, may be absent from embedded Linux systems), FreeBSD, OpenBSD, NetBSD, … but only from MacPorts on OS X, not AIX, and only recently in HP-UX. The pgrep utility shows the process ID of processes matched by name, user and a few other criteria. The argument to pgrep is interpreted as a regexp that must match part of the process’s executable’s name (unless you pass an option to change this). If you call pkill instead of pgrep, the utility sends a signal instead of displaying the process IDs.
Another similar utility is pidof. On Linux, it’s provided by SysVinit or BusyBox (so you’ll often find it on an embedded Linux system that doesn’t have pgrep); there are also ports on other unix variants. The pidof utility has fewer options, it mostly only matches whole executable file names. Its companion utility killall sends a signal to the matched programs¹.
¹
Beware that killall has a different meaning on Solaris and possibly other unix variants; do not type killall as root on Solaris.
Method 2
killall ProcessName(there is a disadvantage with this command in that you don’t always know the process name of a program).pidof ProccessNameandkill the result form pidofps xu | grep <process name> | grep -v grep | awk '{ print $2 }' | xargs kill -9Try this one line and reuse it form the history of your bash, or better create an alias for it .
Method 3
While Hanan has some good suggestions, I’ll add pgrep / pkill. They allow much finer control over which process you find, and regular expressions if you don’t know the precise process you’ll need to kill.
P.S. Hanan’s pidof can be fed to kill directly with backticks:
kill `pidof processname`
Method 4
How about this –
ps -e | awk '$4~/<process name>/{print $1}' | xargs kill
Example:
[jaypal:~/Temp] sleep 100&
[1] 74863
[jaypal:~/Temp] ps -e | awk '$4~/sleep/{print $1}' | xargs kill
[1]+ Terminated: 15 sleep 100
Update:
Sorry, this obviously does not meet the requirement of less typing so a good way of doing it would be to add a function to your .bashrc, .profile or whatever the startup script. The function can be something like this –
killp() {
awk -v pname="$1" '($4==pname){print $1}' <(ps -e) | xargs kill
}
Once added, you can simply pass the name of your process:
[jaypal:~] sleep 100& [1] 77212 [jaypal:~] killp sleep [1]+ Terminated: 15 sleep 100
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0