17

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 ?

Gautam
  • 2,335
  • 5
  • 18
  • 18
  • This is a special case of →Q705296 "How to get pid of process with exact name?". Albeit it's such a common combination of tasks that pkill has been invented for it. – cachius Sep 08 '22 at 17:36

4 Answers4

21

(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.

8
  • killall ProcessName (there is a disadvantage with this command in that you don't always know the process name of a program).
  • pidof ProccessName and kill the result form pidof
  • ps xu | grep <process name> | grep -v grep | awk '{ print $2 }' | xargs kill -9 Try this one line and reuse it form the history of your bash, or better create an alias for it .
Hanan
  • 5,771
  • The second pidof seems a little bit more easier, but still it take 2 steps – Gautam Dec 17 '11 at 23:49
  • I can search for/create a one line command that do that in one step (with a rough process name) but that would require you to type a long line (which is harder that the two steps above), or you could type it once and reuse it form the history. – Hanan Dec 17 '11 at 23:54
  • @GautamK i have updated the answer with the single command option. – Hanan Dec 17 '11 at 23:58
  • @Gautam "more easier". Got it. – Sapphire_Brick Jun 18 '20 at 01:23
6

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`
Kevin
  • 40,767
  • pkill was exactly what I was looking for , I most of the time know the process name, Mostly its conky or firefox or chrome or something like that. Thanks – Gautam Dec 18 '11 at 00:16
1

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
jaypal singh
  • 1,592