- 30 Useful ‘ps Command’ Examples for Linux Process Monitoring
- List All Processes in Current Shell
- Print All Processes in Different Formats
- Display User Running Processes
- Print All Processes Running as Root (Real and Effective ID)
- Display Group Processes
- Display Processes by PID and PPID
- Display Processes by TTY
- Print Process Tree
- Print Process Threads
- Specify Custom Output Format
- Display Parent and Child Processes
- Troubleshoot Linux System Performance
- Print Security Information
- Perform Real-time Process Monitoring Using Watch Utility
- How to restart (or reset) a running process in linux
- 5 Answers 5
- Simple way of restarting crashed processes?
- 6 Answers 6
30 Useful ‘ps Command’ Examples for Linux Process Monitoring
ps (processes status) is a native Unix/Linux utility for viewing information concerning a selection of running processes on a system: it reads this information from the virtual files in the /proc filesystem. It is one of the important utilities for system administration specifically under process monitoring, to help you understand whats is going on in a Linux system.
It has numerous options for manipulating its output, however, you’ll find a small number of them practically useful for daily usage.
In this article, we’ll look at 30 useful examples of ps commands for monitoring active running processes on a Linux system.
Note that ps produces output with a heading line, which represents the meaning of each column of information, you can find the meaning of all the labels on the ps man page.
List All Processes in Current Shell
1. If you run the ps command without any arguments, it displays processes for the current shell.
Print All Processes in Different Formats
2. Display every active process on a Linux system in generic (Unix/Linux) format.
3. Display all processes in BSD format.
4. To perform a full-format listing, add the -f or -F flag.
Display User Running Processes
5. You can select all processes owned by you (runner of the ps command, root in this case), type:
6. To display a user’s processes by real user ID (RUID) or name, use the -U flag.
$ ps -fU tecmint OR $ ps -fu 1000
7. To select a user’s processes by effective user ID (EUID) or name, use the -u option.
$ ps -fu tecmint OR $ ps -fu 1000
Print All Processes Running as Root (Real and Effective ID)
8. The command below enables you to view every process running with root user privileges (real & effective ID) in user format.
Display Group Processes
9. If you want to list all processes owned by a certain group (real group ID (RGID) or name), type.
$ ps -fG apache OR $ ps -fG 48
10. To list all processes owned by effective group name (or session), type.
Display Processes by PID and PPID
11. You can list processes by PID as follows.
12. To select process by PPID, type.
13. Make a selection using a PID list.
Display Processes by TTY
14. To select processes by tty, use the -t flag as follows.
$ ps -t pts/0 $ ps -t pts/1 $ ps -ft tty1
Print Process Tree
15. A process tree shows how processes on the system are linked to each other; processes whose parents have been killed are adopted by the init (or systemd).
16. You can also print a process tree for a given process like this.
$ ps -f --forest -C sshd OR $ ps -ef --forest | grep -v grep | grep sshd
Print Process Threads
17. To print all threads of a process, use the -L flag, this will show the LWP (lightweight process) as well as NLWP (number of the lightweight processes) columns.
Specify Custom Output Format
Using the -o or –format options, ps allows you to build user-defined output formats as shown below.
18. To list all format specifiers, include the L flag.
19. The command below allows you to view the PID, PPID, user name, and command of a process.
20. Below is another example of a custom output format showing file system group, nice value, start time, and elapsed time of a process.
$ ps -p 1154 -o pid,ppid,fgroup,ni,lstart,etime
Display Parent and Child Processes
22. To select a specific process by its name, use the -C flag, this will also display all its child processes.
23. Find all PIDs of all instances of a process, useful when writing scripts that need to read PIDs from an std output or file.
24. Check the execution time of a process.
$ ps -eo comm,etime,user | grep httpd
The output below shows the HTTPD service has been running for 1 hour, 48 minutes, and 17 seconds.
Troubleshoot Linux System Performance
If your system isn’t working as it should be, for instance, if it’s unusually slow, you can perform some system troubleshooting as follows.
26. Find top running processes by highest memory and CPU usage in Linux.
$ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head OR $ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head
27. To kill Linux processes/unresponsive applications or any process that is consuming high CPU time.
First, find the PID of the unresponsive process or application.
Then use the kill command to terminate it immediately.
Print Security Information
28. Show security context (specifically for SELinux) like this.
29. You can also display security information in a user-defined format with this command.
$ ps -eo euser,ruser,suser,fuser,f,comm,label
Perform Real-time Process Monitoring Using Watch Utility
30. Finally, since ps displays static information, you can employ the watch utility to perform real-time process monitoring with repetitive output, displayed after every second as in the command below (specify a custom ps command to achieve your objective).
$ watch -n 1 'ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head'
Important: ps only shows static information, to view frequently updated output you can use tools such as htop; top, and glances: the last two are in fact Linux system performance monitoring tools.
You might also like to read the following related articles.
That’s all for now. If you have any useful ps command example(s) to share (not forgetting to explain what it does), use the comment form below.
How to restart (or reset) a running process in linux
I have two Linux systems communicating over sockets (Desktop and ARM-based development board). I want to restart (or reset) my client application (running on a development board) when server sends a particular predefined message. I don’t want to restart (reboot) Linux, I just want that client application restart itself automatically. I am unable to understand how it should be done.
5 Answers 5
Make your client exec /proc/self/exe when it receives that paticular message. You don’t need to know where the executable actually resides in the file system. And you can reuse main() ‘s argv to construct a new argument vector.
#include #include #include int main(int argc, char **argv) < char buf[32] = <>; char *exec_argv[] = < argv[0], buf, 0 >; int count = argc > 1 ? atoi(argv[1]) : 0; printf("Running: %s %d\n", argv[0], count); snprintf(buf, sizeof(buf), "%d", count+1); sleep(1); execv("/proc/self/exe", exec_argv); /* NOT REACHED */ return 0; >
This restart.c runs like this:
$ gcc restart.c $ ./a.out 3 Running: ./a.out 3 Running: ./a.out 4 Running: ./a.out 5
The normal way to do this is to let your program exit, and use a monitoring system to restart it. The init program offers such a monitoring system. There are many different init programs (SysVinit, BusyBox, Systemd, etc.), with completely different configuration mechanisms (always writing a configuration file, but the location and the syntax of the file differs), so look up the documentation of the one you’re using. Configure init to launch your program at boot time or upon explicit request, and to restart it if it dies. There are also fancier monitoring programs but you don’t sound like you need them. This approach has many advantages over having the program do the restart by itself: it’s standard, so you can restart a bunch of services without having to care how they’re made; it works even if the program dies due to a bug.
There’s a standard mechanism to tell a process to exit: signals. Send your program a TERM signal. If your program needs to perform any cleanup, write a signal handler. That doesn’t preclude having a program-specific command to make it shut down if you have an administrative channel to send it commands like this.
Simple way of restarting crashed processes?
I need to monitor several processes running on my webserver. For some reason, varnish currently crashes once every day or two. I’m using monit to supposedly restart varnish automatically, but it doesn’t work. Here’s my monit.conf entry for Varnish.
check process varnish with pidfile /var/run/varnish.pid start program = "/etc/init.d/varnish start" with timeout 60 seconds stop program = "/etc/init.d/varnish stop" if failed host port 80 protocol http and request "/blank.html" then restart if 3 restarts within 5 cycles then timeout group server
The log file shows that after varnish stops running, the attempted restarts afterwards all fail. Then eventually monit stops monitoring varnish. Anyone have suggestions for how I can fix this? Or better yet, can you suggest other simple ways of automatically monitoring and restarting crashed processes? Thanks!
6 Answers 6
Supervise was built for exactly this purpose — to start processes and watch them, restarting them immediately if they ever terminate.
You could still use monit if you need to do anything more complicated than a simple «is it still running» check, and if the process needs to be restarted, then do that through supervise.
I use the daemontools too, for monitoring unstable services processes. Quite handy if i had to say. 🙂
You could also use /etc/inittab to restart dead processes using the respawn action.
You can use event handler scripts with Nagios if you have that in place to restart services.
If varnish requires root permission to start (init.d scripts usually do) change «/etc/init.d/varnish start» to «sudo /etc/init.d/varnish start». But that probably won’t be quite enough since you probably don’t want to give whatever user monit runs as total sudo nopasswd privileges to all commands and giving sudo to a shell script would be basically just as bad. So you are going to need to figure out which commands in that init script need sudo, give those commands sudo privileges in the /etc/sudoers file to the monit user, and the finally edit that init script accordingly. Or maybe instead of all this varnish can be run as non-root user?
Finally, I am sure you know this but I am going to say it anyways. You are clearly putting a lot of effort into this, I hope you are putting as much effort into figuring out why varnish is crashing and actually fixing it (or hounding the developers to figure out why) 🙂
Update:
This might not be as clean, but an easy way to get this done as root might be to set up a script that checks if the process is okay, and if not starts it. Then just run that script every couple minutes as a cron job.
I considered Nagios at first, but wanted something small and simple for my purposes. And yes, I’m looking into the Varnish issue. One of my servers has been running it stable for a very long time, so it’s definitely got to do with me. 🙁