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

ps Cheatsheet

ps is the classic Unix/Linux command to list processes and their details. ps that ships with Linux is super-confusing to use because it supports three types of arguments: Unix options (-u), BSD options (x) and GNU options (--pid). The format of the display will also be different based on which type of option you use! I wish the Linux ps did not have so much backward compatibility and was as simple as the OpenBSD ps.

  • With no arguments, ps lists the processes associated with the current terminal:
$ ps

This would have been useful many years ago, when Unix users put running programs to background and switched them back to foreground later. Today most Linux users just open multiple terminals to achieve the same, so this invocation is pretty useless.

Note that is lists the process ID (PID), the terminal of the process (TTY), time it has consumed (TIME) and the command invoked to start the process (CMD).

  • To view details of a specific process using its PID, say 9999:
$ ps 9999
  • To view all processes associated with a terminal:
$ ps a
  • To view all processes on the system, try one of these:
$ ps ax
$ ps -e
  • To view the above processes, but with user (UID) of the processes:
$ ps aux
$ ps -ef

Note that this command might be a bit slow since it needs to convert UIDs to usernames.

  • To view the above processes, but with more excruciating details:
$ ps lax
$ ps elf
  • Any of the above options can be viewed with a simple ASCII tree linking parent and child processes. For example:
$ ps a --forest
  • To view the environment variables associated with a process, provide its PID (say 9999):
$ ps eww 9999
  • The columns that are displayed can be customized. For example:
$ ps -o user,pid,time,cmd

How to view PDF in terminal using FBGS

Framebuffer Ghostscript Viewer (fbgs) can be used to view PostScript (PS) and PDF files at the terminal. However, it only works with real terminals (/dev/tty) and not with pseudo terminals (/dev/pts).

fbgs ships along with the fbi package. So, to install it:

$ sudo apt install fbi

To be enable use of the program by any user, the username must be added to video group:

$ sudo adduser joe video

To view a PDF file:

$ fbgs foo.pdf

Tried with: FBI 2.07-11 and Ubuntu 14.04