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