Find and Kill Process – How to Find the ID of a Process and Kill It

grepkillprocessps

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 ?

Best Answer

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

Related Question