Tech Junkie Blog - Real World Tutorials, Happy Coding!: Linux Process Management: pgrep, pkill, And top

Monday, March 14, 2022

Linux Process Management: pgrep, pkill, And top

 Since searching for a process and killing process is such a common task there's are shortcut commands available for these tasks in form of pgrep and pkill.  As you suspect these commands are used for finding and killing processes respectively.

For example we can search for gnome processes like this instead of combining the ps command with the grep command

[jhuynh@cent7 ~]$ pgrep gnome
2292
2303
2521
2570
2822
5338
5345

Or to get more information you can use the command like  command below to get the full listing

[jhuynh@cent7 ~]$ ps -F -p $(pgrep gnome)

UID        PID  PPID  C    SZ   RSS PSR STIME TTY      STAT   TIME CMD
jhuynh    2292     1  0 79358  3836   0 06:53 ?        Sl     0:00 /usr/bin/gnome-keyring-daemon --daemonize --login
jhuynh    2303  2280  0 204743 11336  0 06:53 ?        Ssl    0:00 /usr/libexec/gnome-session-binary --session gnome-classic
jhuynh    2521  2303  1 756875 210812 0 06:53 ?        Sl     0:48 /usr/bin/gnome-shell
jhuynh    2570     1  0 162513 13756  0 06:54 ?        Sl     0:00 /usr/libexec/gnome-shell-calendar-server
jhuynh    2822  2303  0 278015 67620  0 06:54 ?        Sl     0:03 /usr/bin/gnome-software --gapplication-service
jhuynh    5338     1  0 167498 29064  0 07:43 ?        Sl     0:01 /usr/libexec/gnome-terminal-server
jhuynh    5345  5338  0  2134   724   0 07:43 ?        S      0:00 gnome-pty-helper

The pkill command works in similar fashion, so instead of killing the process by the number, you can just kill the bash process with this command pkill PID, one caveat is that it does not work like kill -9 so your usage may be limited.

Another useful command is the top command, this command will sort the processes that uses the most resources first by default. All you have to do is type top in the terminal









You sort it by other attributes as well such as memory, by default it's by CUP usage, to get to the other options type the f key












Select %MEM and type s to select it and press esc to sort the top screen by memory usage



No comments:

Post a Comment

Search This Blog