Essential Examples of the ps Command in Linux
Commands

Essential Examples of the ps Command in Linux

Abhishek Prakash
Abhishek Prakash

What is ps command in Linux?

The ps command in Linux is used to display about running processes on the system. You can get information like process ID (PID) for the processes you or any other user is running on the same Linux system.

The ps command is an extensive tool in itself and it has over 80 command options. You can understand its strength and complexity. This is why I am going to show you some of the most common and the most useful examples of the ps command in Linux.

Essential usage of ps command in Linux

Ps Command In Linux

If you use the ps command without any options in Linux, it will show the running processes in the current shell:

ps

This is the output. I have sent Gedit command in the background this is why it shows three processes otherwise you will normally see just ps and bash.

  PID TTY          TIME CMD
  503 pts/0    00:00:00 gedit
 2053 pts/0    00:00:00 ps
31585 pts/0    00:00:00 bash

In here:

  • PID is the unique process ID of the process
  • TTY is the type of terminal user is logged in to. pts means pseudo terminal
  • TIME gives you for how long the process has been running
  • CMD is the command that you run to launch the process

Now this doesn’t really provide any real, useful information. Let’s see some better examples of ps command:

1. See all your running process

If you want to see all the processes run by you, you can use the ps command with option x like this:

ps -x

The x option will display all the processes even if they are not associated with current tty (terminal type) or if they don’t have a controlling terminal (like daemons).

The – before option x is optional but the general Linux convention is to use – before options so I advise you to keep on following it. It won’t hurt you.

This is what the output looks like. I have truncated the output because it had hundreds of lines:

  PID TTY      STAT   TIME COMMAND
  503 pts/0    Sl     0:00 gedit
 2245 ?        S      0:00 /usr/bin/ssh-agent -D -a /run/user/1000/keyring/.ssh
 3039 ?        Ss     0:00 /lib/systemd/systemd --user
 3040 ?        S      0:00 (sd-pam)
 3054 ?        SLl    0:01 /usr/bin/gnome-keyring-daemon --daemonize --login
 3059 tty2     Ssl+   0:00 /usr/lib/gdm3/gdm-x-session --run-script env 

The STAT in the above output means Process State Codes. You can find a detailed table in the man page of the ps command.

But you’ll rarely see ps command used with just option x. It is usually accompanied by option u in this manner:

ps -ux

With option u, you’ll have detailed information about each process:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
abhishek   503  0.0  0.4 681580 37516 pts/0    Sl   18:09   0:00 gedit
abhishek  2245  0.0  0.0  11300  1496 ?        S    18:37   0:00 /usr/bin/ssh-agent -D -a /run/user/1000/keyring/.ssh
abhishek  3039  0.0  0.0  77344  3508 ?        Ss   10:37   0:00 /lib/systemd/systemd --user
abhishek  3040  0.0  0.0 114632   360 ?        S    10:37   0:00 (sd-pam)
abhishek  3054  0.0  0.1 517104 11512 ?        SLl  10:37   0:01 /usr/bin/gnome-keyring-daemon --daemonize --login

As you can see, now you get the user name and the CPU consumption and memory usage of each process. RSS shows how much memory the process currently has in RAM while VSZ is how much virtual memory the process has in total.

2. See all running processes with ps aux command

You’ll probably see ps -aux or ps aux all the time in Linux tutorials and documentations.

With the added -a option, you can see the running processes by all the users on Linux system.

ps -aux

The command output is the same as ps -ux but now you have processes from other users as well. Thanks to the -u option, you can identify which process belongs to which user.

3. See all running processes with ps -ef command in Linux

Apart from ps -aux, you can also list all the running processes with -e command. The common practice is to combine it with option f to get full listing of the commands used to run the processes.

ps -ef

Do note that – before e is important otherwise it will show different result. I told you, ps is a messed up, complicated command.

You can also combine the H option to see all the processes in a threaded view with child processes under their parents:

ps -efH

4. See all running processes by a certain user

To get the information about all the processes run by a certain user, you can use the -U option with the user name:

ps -U user_name

For example, I can see all the processes running by the root user like this:

ps -U root
  PID TTY          TIME CMD
    1 ?        00:00:41 systemd
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00 rcu_gp
    4 ?        00:00:00 rcu_par_gp
    8 ?        00:00:00 mm_percpu_wq
    9 ?        00:00:03 ksoftirqd/0
   10 ?        00:01:22 rcu_sched

5. See all processes run by a group

You can also categorize running processes by group instead of user by providing group name or group id:

ps -G group_name_or_id

You can combine with option f to get the full listing.

6. Get all occurrences and PID of a program

One of the essential use of the ps command is to get the process ID (PID) of a running program. Normally when you are looking to kill a misbehaving program, you search for all the occurrences of the program, get their PIDs and use the kill command to end the process.

ps -C program__name

For example, if I have to find all running instances of the apt package manager:

ps -C apt
  PID TTY          TIME CMD
11425 pts/1    00:00:00 apt

You can also use grep command to get a similar result.

ps aux | grep program_name

7. Get process information about a PID

Alright! You have got a PID but you don’t know which process it belongs to. You can use the ps command to find the process information from its PID in this way:

ps -pN

You can use more than one PIDs by separating them with comma:

ps -pN1,N2,N3

What else?

As I said earlier, ps is a complicated and extensive command. These were some of the most common ps command examples you’ll need most of the time. If you want more, you can always refer to its man page.

If you have some other useful example of ps command that you use regularly, why not share it with rest of us in the comment section?



Join the conversation.