Monitor one process linux

20 ps Command Examples to Monitor Linux Processes

Monitoring Linux system is one of the most quintessential task that any Linux user or a systems administrator needs to carry out round the clock to ensure the system is running smoothly and avoid bottle-necks. Linux ships with a few built-in tools that can help you monitor your system. One of these is the ps (processes status) command that specifically monitors the running processes and among other metrics associated with the processes such as percentage CPU and memory utilization. The ps command reads the /proc filesystem which is RAM based or virtual file system.

In this guide, we dive deeper and let you on 20 of the most commonly used ps commands.

1) Listing Process in the Shell

The simplest form of the ps command in linux, lists the running processes in the current shell. This can be illustrated below.

[[email protected] ~]$ ps PID TTY TIME CMD 1437 pts/0 00:00:00 bash 1465 pts/0 00:00:00 ps [[email protected] ~]$
  • PID : This is the Process ID.
  • TTY : This refers to the terminal that started and is controlling the process.
  • TIME : This is the time / cumulative time that a process has run for.
  • CMD : Name of the command that started the process.

2) Listing processes associated with a terminal

This is much like the listing the processes in the shell. To list the processes associated with the terminal you are currently running, execute the command:

[[email protected] ~]$ ps -T PID SPID TTY TIME CMD 1437 1437 pts/0 00:00:00 bash 13357 13357 pts/0 00:00:00 ps [[email protected] ~]$

3) Displaying all the running processes on your system

To get an overview of all the running processes on your Linux system use the -A flag as shown:

ps-command-display-all-running-process

Alternatively, you can also use the –e flag as shown. You’ll still get similar results.

4) Displaying output in BSD format

To list the processes in BSD format, run the command

Let’s break down the command:

The -a flag instructs ps to display all the processes from all the users. This, however, excludes the processes associated with a specific terminal.

The -u flag implies a user-oriented format. It provides more detailed information associated with the running processes.

The x flag lists processes that are usually started upon a system boot as well as background processes.

ps-command-output-bsd-format

  • USER – This specifies the user running the process
  • PID – This is the Process ID of the process
  • %CPU – This it the percentage CPU usage of each process
  • %MEM – This is the % of the physical memory used up by the process
  • VSZ – This is the size of virtual memory of each process in kilobytes
  • RSS – This is the size/amount of physical memory being used by a process
  • STAT – This points to the process state code e.g S (sleeping), Z (zombie) and R (Running)
  • START – This is the time the process started running
Читайте также:  Youtube downloader to linux

5) Displaying output in UNIX format

If you wish to display the output in UNIX format , then execute the ps command with a combination of -ef flags

The -e flag tells the ps command to list all the processes

The -f flag (full format) instructs the ps command to list detailed information about the processes.

ps-command-output-unix-format-linux

Let’s define a few new terms from the screenshot:

  • PPID : This refers to the Parent Process ID
  • C : This refers to the %CPU or percentage of the CPU utilization of a process.
  • STIME : This points to the time the command was started. Same as START

6) Displaying output in a user-defined format

In a user-defined format output, you have the power to decide which columns to be displayed. The -o flag determines which columns will be displayed when running the ps command. In the example below, only the PID and START columns are displayed. Be sure to note the columns options are passed in lowercase in the ps command.

[[email protected] ~]$ ps -efo pid,start PID STARTED 1437 21:55:28 13632 01:41:16 [[email protected] ~]$

7) Show the current running processes of a user

Sometimes, you may want to find out processes that are associated with a certain user, so to achieve this, use the -u flag as shown”

For instance, to view processes associated with user ‘Linuxtechi’ run the command:

User-Specific-Process-ps-command-Linux

Alternatively, you can use the user’s uid (User ID). You can get the user’s id by running the command:

[[email protected] ~]$ id linuxtechi uid=1000(linuxtechi) gid=1000(linuxtechi) groups=1000(linuxtechi),10(wheel) [[email protected] ~]$

From the output above, we can clearly see that the uid for the user ‘linuxtechi’ is 1000. You can then use the ps command and the uid to find the processes associated with the user.

id-based-process-ps-command-linux

For the root user, run the command

root-owned-process-linux

Similarly, you can check using the uid of the root user whose value is 0

8) Displaying group processes

Listing group processes is similar to listing user processes. You can list a group process using the ps command by passing the name of the group by specifying the group ID.

For example, to display the processes associated with the group ‘apache’ run the command:

group-based-process-linux-ps-command

Alternatively, you can pass the group ID (gid) instead of the group name. you can obtain the GIDs from the /etc/group file.

For example, in this case, the group ID for Apache is 48. So, instead of specifying the group name ‘apache’ simply specify the GID no. 48.

group-id-based-process-linux-ps-command

9) Displaying processes using PID and PPID

Linux processes can also be displayed using the PID (Process ID) and the PPID (parent process ID). The PPID refers to the PID of the parent of a process.

To list processes by PID, run

For instance, to list the processes associated with PID 1286 run the command:

[[email protected] ~]$ ps -fp 1286 UID PID PPID C STIME TTY TIME CMD postfix 1286 1285 0 03:49 ? 00:00:00 pickup -l -t unix -u [[email protected] ~]$

To list processes by PPID, run

For example, to list processes owned by PPID 31903 run

Читайте также:  Загрузочная флешка реаниматор linux

process-based-on-ppid-ps-command

10) Searching for a PID using process name

For the most of the time we usually do not know the PID of the process name. To do so, use the -C flag as shown

[[email protected] ~]$ ps -C firefox PID TTY TIME CMD 1446 tty2 00:00:04 firefox [[email protected] ~]$

11) Displaying process using TTY

If you wish to filter processes by the tty value use the -t flag. For example to display the processes attached to tty1, run the ps command as shown:

list-tty-process-ps-command-linux

12) Printing the Process Tree

A process tree shows how processes are linked in a Linux system. Processes with no parents link to the system through init or systemd. Printing requires that you use the following format:

process-tree-ps-command-linux

13) Filter a process according to the thread process

To filter a process using the process thread, use the -L flag as shown,

process-thread-ps-command-linux

14) Real-time Process Monitoring

You can use the watch utility to monitor static information about a process. The output plays every second as shown below

$ watch -n 1 'ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head'

watch-command-process-monitor-linux-ps

The command prints the pid,ppid,cmd,%mem and %cpu columns and monitors the statistics every single second while sorting the %CPU utilization in descending order.

15) Troubleshoot Performance Issues of Linux Machine

When your system is slow or overloaded, you can Identify the top running process by highest CPU and memory usage

Execute following ps command to list top process which are consuming high memory,

$ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

Execute following ps command to list top process which are consuming high cpu,

$ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head

Sample output of above ps command on my system is below,

top-process-memory-cpu-ps-command-linux

16) Killing unresponsive processes

The ps command can allow you to kill unresponsive applications or any processes that are hogging CPU and RAM. To do this, you need to find the PID of that particular application using the command:

Once you’ve found the PID of the process, you can terminate it using the kill command, example is shown below,

17) List all PID’s of all process instances

You can list all the PIDs associated with a process by using the syntax:

For example to find out the PIDs associated with httpd web server run:

pids-process-linux-ps-command

18) Display the duration that processes have been running

To show for how long processes have been running, run the command:

process-duration-linux-ps-command

Use below ps command to know how long a particular process has been running,

Let’s suppose we want to know about mysql process

[[email protected] ~]$ ps -C mysqld -o pid= 3807 [[email protected] ~]$ [[email protected] ~]$ ps -o etime -p 3807 ELAPSED 01:13:56 [[email protected] ~]$

19) List child processes of a process

To print or list the child processes of a process, use -C flag in the syntax below:

For example, to list child processes associated with httpd process, run

[[email protected] ~]$ ps -C httpd PID TTY TIME CMD 31911 ? 00:00:00 httpd 31918 ? 00:00:00 httpd 31919 ? 00:00:00 httpd 31920 ? 00:00:01 httpd 31921 ? 00:00:01 httpd 31922 ? 00:00:01 httpd [[email protected] ~]$

20) Filtering output of ps command using grep

To get specific results in ps command , you can pipe the output to grep utility tool and provide the search criteria.

For example , to search or narrow down to php process, run the command:

grep-ps-command-output-linux

We have listed the top 20 ps commands that are commonly used for listing and monitoring the processes running on your Linux system . These commands will work across all Linux distributions and will certainly give you an overview of the processes running on your system and help you determine which are overworking your CPU and RAM resources. Did you find this guide insightful ? Feel free to like and share it across your social media platforms. Your feedback is most welcome.

Читайте также:  Установка сетевой операционной системы linux

2 thoughts on “20 ps Command Examples to Monitor Linux Processes”

Hi,
Like the way you’ve presented the article. Liked every example used for monitoring linux processes. Pls. continue your good work for the linux community… Reply

Thank you Pradeep Kumar,
Your explanations are clear, concise and presented in a way that I (Debian novice) could readily understand.
Marj Reply

Leave a Comment Cancel reply

Recent Posts

WP-Subscribe-MailChimp

  • How to Install PHP 8 on RHEL 9 | Rocky Linux 9 | AlmaLinux 9
  • How to Install PHP 8 on RHEL 8 / Rocky Linux 8 / CentOS 8
  • How to Install and Use Wireshark in Ubuntu 22.04
  • Top 10 Things to Do After Installing Debian 12 (Bookworm)
  • How to Install Debian 12 (Bookworm) Step-by-Step
  • How to Upgrade Debian 11 to Debian 12 (Bookworm) via CLI
  • How to Setup Dynamic NFS Provisioning in Kubernetes Cluster
  • How to Install Nagios on Rocky Linux 9 / Alma Linux 9
  • How to Install Ansible AWX on Kubernetes Cluster
  • How to Install Docker on Fedora 38/37 Step-by-Step

Pages

Источник

How to monitor a process in Linux CPU, Memory and time

How can I benchmark a process in Linux? I need something like «top» and «time» put together for a particular process name (it is a multiprocess program so many PIDs will be given)? Moreover I would like to have a plot over time of memory and cpu usage for these processes and not just final numbers. Any ideas?

2 Answers 2

I typically throw together a simple script for this type of work.

Take a look at the kernel documentation for the proc filesystem (Google ‘linux proc.txt’).

The first line of /proc/stat (Section 1.8 in proc.txt) will give you cumulative cpu usage stats (i.e. user, nice, system, idle, . ). For each process, the file /proc/$PID/stat (Table 1-4 in proc.txt) will provide you with both process-specific cpu usage stats and memory usage stats (see rss).

If you google a bit you’ll find plenty of detailed info on these files, and pointers to libraries / apps / code snippets that can help you obtain / derive the values you need. With that in mind, I’ll focus on the high-level strategy.

For CPU stats, use your favorite scripting language to create an executable that takes a set of process ids for monitoring. At a fixed interval (ex: 1 second) poll / calculate the cumulative totals for each process and the system as a whole. During each poll interval, write all results on a single line to stdout.

For memory stats, write a similar script, but simply log the per-process memory usage. Memory is a bit easier as we directly obtain the instantaneous values.

Run these script for the duration of your test, passing the set of processes ids that you’d like to monitor and redirecting its output to a log file.

./logcpu $(pidof foo) $(pidof bar) > cpustats ./logmem $(pidof foo) $(pidof bar) > memstats 

Import the contents of these files into a spreadsheet (for certain applications this is as easy as copy / paste). For CPU, you are after instantaneous values but have cumulative values, so you’ll need to do some minor spreadsheet work to derive these values (it’s just the delta ‘t(x + 1) — t(x)’). Of course you could have your cpu logger write the delta, but you’ll be spending a bit more time up front on the script.

Finally, use your spreadsheet to generate a nice plot.

Источник

Оцените статью
Adblock
detector