pgrep and pkill

pgrep and pkill are two useful commands that go well together. You can list processes using ps and kill them using kill. However, I find it easier to use pgrep and pkill when I want to find and kill a process.

pgrep

  • To list all the PIDs of a user:
$ pgrep --uid joe
$ pgrep -u joe
  • To list the PID and the corresponding process name for a user:
$ pgrep --uid joe --list-name
$ pgrep -u joe -l
  • To list the PID and the corresponding full command line for a user:
$ pgrep --uid joe --list-full
$ pgrep -u joe -a

This is extremely useful because to find Python scripts or commandline arguments to a program that is running as a process.

  • To list the PID whose process name matches input pattern:
$ pgrep foobar
  • To list the PID and process names that match input pattern:
$ pgrep -l foobar
  • To list the PID and command line of processes that match input pattern:
$ pgrep -a foobar

pkill

pkill is used to send a signal to or kill processes by using a pattern that matches a process name or its command line. pkill takes many arguments that are similar to pgrep.

  • To kill all processes of current user that matches input pattern:
$ pkill foobar
  • To kill all processes of current user that matches input pattern in its command line:
$ pkill -f foobar

Tried with: Ubuntu 16.04

Advertisements

How to kill a process

Sooner or later you will find that an errant process is running amok or in the background and it needs to be stopped. This is called killing a process in Linux.

kill

The most common command to kill a process is kill.

  • To kill a process, provide its PID:
$ kill 1234

By default, kill sends the TERM signal.

  • To kill multiple processes, provides all their PIDs:
$ kill 1234 5678 9999
  • Many other signals other than TERM can be sent to processes. To list all the available signal numbers and their corresponding signal names:
$ kill -L
  • To send a different signal provide the signal name:
$ kill -KILL 1234
  • The signal name can also be specified by appending a SIG as prefix:
$ kill -SIGKILL 1234
  • Alternatively, you can provide the signal number:
$ kill -9 1234
  • For most processes, sending the default TERM is enough. Most processes receive this signal, clean up their work and exit. For errant processes which do not respond to TERM, the final deathblow is sending a KILL. On sending this signal, the kernel steps in and it directly murders the process giving it no chance to clean up.

killall

Looking up the PID of a process and then killing it is usually cumbersome. What you usually want to do is to kill based on the process name. This can be done using the killall command.

  • To kill all Vim processes:
$ killall vim
  • To kill all processes that match a regex:
$ killall -r "*abcd*"
  • By default, killall sends the TERM signal. Other signals can be sent using signal number or signal name using the same command syntax as the kill command shown above.

  • By default, killall only prints out the processes that it could not kill. It might be better to also see output from the command when it succeeds. The verbose option can be used for this:

$ killall -v vim
Killed vim(28952) with signal 15
Killed vim(28347) with signal 15
  • You might be afraid that you might kill processes by mistake with this command. If so use the interactive option to pick and choose which process to kill:
$ kill -i vim
Kill vim(1067) ? (y/N)

Tried with: Ubuntu 14.04

How to close an app on Android

Having many apps running in the background is no big deal on mobile operating systems like Android. But, sometimes you just want to close or kill an erratically behaving app.

The easy method

Click the multi-tasking button. This is one of the three touch buttons at the bottom, typically the one on the right. It brings up a vertical list of open apps. Swipe the app you want to the right and it will be closed.

The next easier method

Open Settings > Apps, find your app and open it. A page with all the details of the app is displayed. Click the Force stop button here to close the app.

Tried with: CyanogenMod 10.1.2-encore and Nook Color