Getting Inside Processes: Exploring the ps Command in Linux

ps-command-in-linux-to-display-processes-with-examples

AS you know Linux is multitasking and multiuser systems, you can run multiple precesses processes at the same time without interfering with each other.

Process: In Linux, a running instance of a program is called process. We can say the process is a fundamental concept of the Linux OS. In other words, You can say “A process is an executing instance of a program and carry out different tasks within the operating system”.

Occasionally, when you are working on a Linux machine, you can find out easily by using ps command in Linux “what processes are currently running on your machine”.

You can use, multiple commands to view process, for example, ps command, top command, and pstree command.

ps command in Linux with Examples

In this article I am going describe ps command in Linux with examples, Ps command is used to view information with the processes on a system that stands as an abbreviation for “Process Status”.

You can use ps command to list the currently running processes with their PIDs and some other information depends on different options.

Process information is saved in /proc location as virtual files. ps command reads the process information from the virtual files from /proc file-system.

Basic Syntax of ps command in Linux

Have a look on basic syntax of ps command before start exploring it. basic syntax is as follows:

ps [OPTIONS]

You can use ps command without arguments to displays processes for the current shell. Just type ps in the terminal and hit enter, you will get all processes on the screen.

[vijay@localhost ~]$ ps
  PID TTY          TIME CMD
 3589 pts/0    00:00:00 bash
 4211 pts/0    00:00:00 ps
[vijay@localhost ~]$ 

You can see the following information from above results:

PID – the unique process ID.
TTY – the terminal type that the user is logged into.
TIME – the amount of CPU in minutes and seconds that the process has been running.
CMD – the name of the command that launched the process.

Note – Sometimes when we execute ps command, it shows TIME as 00:00:00. It is nothing but the total accumulated CPU utilization time for any process and 00:00:00 indicates no CPU time has been given by the kernel till now. In the above example, we found that for bash no CPU time has been given. This is because bash is just a parent process for different processes that need bash for their execution and bash itself is not utilizing any CPU time till now.

Print all active processes on the screen:

If you want to print all the running processes on the screen, use -A or -e option with ps command. See the example below:

[vijay@localhost ~]$ ps -e
  PID TTY          TIME CMD
    1 ?        00:00:02 systemd
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00 rcu_gp
    4 ?        00:00:00 rcu_par_gp
    6 ?        00:00:00 kworker/0:0H-kblockd
    7 ?        00:00:00 kworker/u2:0-events_unbound
    8 ?        00:00:00 mm_percpu_wq
    9 ?        00:00:00 ksoftirqd/0
   10 ?        00:00:00 rcu_sched
   11 ?        00:00:00 migration/0
   12 ?        00:00:00 watchdog/0
   13 ?        00:00:00 cpuhp/0
   15 ?        00:00:00 kdevtmpfs
   16 ?        00:00:00 netns
   17 ?        00:00:00 kauditd
   18 ?        00:00:00 khungtaskd
   19 ?        00:00:00 oom_reaper
   20 ?        00:00:00 writeback
   21 ?        00:00:00 kcompactd0
   22 ?        00:00:00 ksmd
   23 ?        00:00:00 khugepaged
[vijay@localhost ~]$ ps -A
  PID TTY          TIME CMD
    1 ?        00:00:03 systemd
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00 rcu_gp
    4 ?        00:00:00 rcu_par_gp
    6 ?        00:00:00 kworker/0:0H-kblockd
    7 ?        00:00:00 kworker/u2:0-events_unbound
    8 ?        00:00:00 mm_percpu_wq
    9 ?        00:00:00 ksoftirqd/0
   10 ?        00:00:00 rcu_sched
   11 ?        00:00:00 migration/0
   12 ?        00:00:00 watchdog/0
   13 ?        00:00:00 cpuhp/0

View Processes not associated with a terminal

View all processes except both session leaders and processes not associated with a terminal.

ps -a

[vijay@localhost ~]$ ps -a
  PID TTY          TIME CMD
 2032 tty1     00:00:00 gnome-session-b
 2045 tty1     00:00:04 gnome-shell
 2076 tty1     00:00:00 Xwayland
 2094 tty1     00:00:00 ibus-daemon
 2097 tty1     00:00:00 ibus-dconf
 2100 tty1     00:00:00 ibus-x11
 2138 tty1     00:00:00 gsd-xsettings
 2140 tty1     00:00:00 gsd-a11y-settin
 2143 tty1     00:00:00 gsd-clipboard
 2145 tty1     00:00:00 gsd-color
 2150 tty1     00:00:00 gsd-datetime
 2152 tty1     00:00:00 gsd-housekeepin
 2157 tty1     00:00:00 gsd-keyboard

You can use -d option to view all the processes except session leaders. Use syntax as follows:

$ps -d

[vijay@localhost ~]$ ps -d
  PID TTY          TIME CMD
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00 rcu_gp
    4 ?        00:00:00 rcu_par_gp
    6 ?        00:00:00 kworker/0:0H-kblockd
    7 ?        00:00:00 kworker/u2:0-events_unbound
    8 ?        00:00:00 mm_percpu_wq
    9 ?        00:00:00 ksoftirqd/0
   10 ?        00:00:00 rcu_sched
   11 ?        00:00:00 migration/0
   12 ?        00:00:00 watchdog/0
   13 ?        00:00:00 cpuhp/0
   15 ?        00:00:00 kdevtmpfs
   16 ?        00:00:00 netns

Let,s view all processes except those that fulfill the specified conditions (negates the selection) :

For the Example – If you want to see only session leaders and processes not associated with a terminal. Then, run the following syntax:

ps -a -N
OR
ps -a –deselect

[vijay@localhost ~]$ ps -a --deselect
  PID TTY          TIME CMD
    1 ?        00:00:03 systemd
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00 rcu_gp
    4 ?        00:00:00 rcu_par_gp
    6 ?        00:00:00 kworker/0:0H-kblockd
    7 ?        00:00:00 kworker/u2:0-events_unbound
    8 ?        00:00:00 mm_percpu_wq
    9 ?        00:00:00 ksoftirqd/0
   10 ?        00:00:00 rcu_sched

It’s time to print all processes associated with this terminal, So you can -T option to do this :

ps -T

[vijay@localhost ~]$ ps -T
  PID  SPID TTY          TIME CMD
 4967  4967 pts/0    00:00:00 bash
 5703  5703 pts/0    00:00:00 ps
[vijay@localhost ~]$ 

You can view all the running processes, by using -r option :

ps -r

[vijay@localhost ~]$ ps -r
  PID TTY      STAT   TIME COMMAND
 5766 pts/0    R+     0:00 ps -r
[vijay@localhost ~]$ 

View all processes owned by you : Processes i.e same EUID as ps which means runner of the ps command, vijay in mine case –

ps -x

[vijay@localhost ~]$ ps -x
  PID TTY      STAT   TIME COMMAND
 2306 ?        Ss     0:00 /usr/lib/systemd/systemd --user
 2316 ?        S      0:00 (sd-pam)
 2324 ?        S   0:00 /usr/bin/pulseaudio --daemonize=no
 2330 ?        Sl     0:00 /usr/bin/gnome-keyring-daemon --daemonize --login
 2337 ?        Ssl    0:00 /usr/bin/dbus-daemon --session --address=systemd: --n
 2342 tty2     Ssl+   0:00 /usr/libexec/gdm-wayland-session gnome-session
 2348 tty2     Sl+    0:00 /usr/libexec/gnome-session-binary
 2410 tty2     Sl+    2:45 /usr/bin/gnome-shell
 2428 ?        Ssl    0:00 /usr/libexec/gvfsd 

If you like our content, please consider buying us a coffee.
Thank you for your support!