Mastering the ps command in Linux: A Comprehensive Guide with Examples

Introduction

ps command in Linux means ps displays information about a selection of the active processes. In the Linux operating system, managing and monitoring processes is a crucial task for system administrators and users alike. The ps command is an essential tool for viewing the currently running processes on a system.

It provides detailed information about each process, including its process ID (PID), the user who owns the process, and the resources it is consuming. In this article, we will explore the ps command in detail, learn how to use it effectively and provide practical examples to demonstrate its capabilities in real-world scenarios.

ps command in Linux with Examples

Syntax

ps [options]

According to the man page, the ps the command provides a snapshot of the current processes.

For more detailed information about the ps command, you can use:

man ps

ps command in Linux with Examples

$ ps

Display User Running Processes

[vagrant@DevopsRoles ~]$ ps -X
#To display a user’s processes by real user ID (RUID) 
[vagrant@DevopsRoles ~]$ ps -fU vagrant

Display Group Processes

[vagrant@DevopsRoles ~]$ ps -fG vagrant

print a process tree for a given process

[vagrant@DevopsRoles ~]$ ps -ef --forest | grep -v grep | grep sshd 
 root      2414     1  0 06:38 ?        00:00:00 /usr/sbin/sshd -D -u0
 root      4087  2414  0 06:39 ?        00:00:00  _ sshd: vagrant [priv]
 vagrant   4090  4087  0 06:39 ?        00:00:00      _ sshd: vagrant@pts/0

Print Process Threads

[vagrant@DevopsRoles ~]$ ps -fL -C sshd
 UID        PID  PPID   LWP  C NLWP STIME TTY          TIME CMD
 root      2414     1  2414  0    1 06:38 ?        00:00:00 /usr/sbin/sshd -D -u0
 root      4087  2414  4087  0    1 06:39 ?        00:00:00 sshd: vagrant [priv]
 vagrant   4090  4087  4090  0    1 06:39 ?        00:00:00 sshd: vagrant@pts/0
 root      4730  2414  4730  3    1 07:17 ?        00:00:00 sshd: vagrant [priv]
 vagrant   4733  4730  4733  0    1 07:17 ?        00:00:00 sshd: vagrant@pts/1

To view the PID, PPID, user name, and command of a process.

[vagrant@DevopsRoles ~]$ ps -eo pid,ppid,user,cmd | grep vagrant
  4087  2414 root     sshd: vagrant [priv]
  4090  4087 vagrant  sshd: vagrant@pts/0
  4091  4090 vagrant  -bash
  4730  2414 root     sshd: vagrant [priv]
  4733  4730 vagrant  sshd: vagrant@pts/1
  4734  4733 vagrant  -bash
  4770  4091 vagrant  ps -eo pid,ppid,user,cmd
  4771  4091 vagrant  grep --color=auto vagrant

Find the top running processes by highest memory and CPU usage in Linux.

[vagrant@DevopsRoles ~]$ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
   PID  PPID CMD                         %MEM %CPU
  2413     1 /usr/bin/python2 -Es /usr/s  3.4  0.0
  1666     1 /usr/sbin/NetworkManager --  1.7  0.0
  1502     1 /usr/lib/polkit-1/polkitd -  1.6  0.0
     1     0 /usr/lib/systemd/systemd --  1.2  0.0
  4730  2414 sshd: vagrant [priv]         1.1  0.0
  4087  2414 sshd: vagrant [priv]         1.1  0.0
  2359  1666 /sbin/dhclient -d -q -sf /u  1.0  0.0
  1076     1 /usr/lib/systemd/systemd-ud  0.9  0.0
  2418     1 /usr/sbin/rsyslogd -n        0.9  0.0
 [vagrant@DevopsRoles ~]$ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head
   PID  PPID CMD                         %MEM %CPU
     1     0 /usr/lib/systemd/systemd --  1.2  0.0
     2     0 [kthreadd]                   0.0  0.0
     3     2 [ksoftirqd/0]                0.0  0.0
     5     2 [kworker/0:0H]               0.0  0.0
     6     2 [kworker/u2:0]               0.0  0.0
     7     2 [migration/0]                0.0  0.0
     8     2 [rcu_bh]                     0.0  0.0
     9     2 [rcu_sched]                  0.0  0.0
    10     2 [lru-add-drain]              0.0  0.0

Conclusion

ps command in Linux is a simple command in Linux. The ps command is a powerful and versatile tool in Linux for monitoring and managing system processes. By mastering its options and syntax, you can gain valuable insights into the processes running on your system, diagnose issues, and optimize performance.

Hopefully, this article has provided you with a clearer understanding of how to use the ps command effectively and apply it in your daily tasks. Keep exploring and leveraging the powerful commands in Linux to enhance your efficiency and productivity in system administration and process management.. Thank you for reading the DevopsRoles page!

, ,

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.