10 Example of ps command in Linux

Hello guys, It's been a long time since I wrote about the Linux command in this blog. In my last article, I have shared free Linux courses for beginners which you many of you guys have liked, that's why I am writing another post on Linux, this time about the "ps" command.  If you have worked on a Linux machine then you are likely to be familiar with the "ps" command. The "ps" command is used to check the process status. It stands for "process statistics" and you can use the ps command to see all the processes and find things like PID which can be used to interact with the process. For example, you can kill a process by passing PID to kill command in Linux as shown here.  


If you want to learn ps command and looking for some examples then you have come to the right place.  Earlier, I have shared 10 examples of some of the essential Linux commands like find, grep, lsof, curl, chmod, etc and in this article, I will share some frequently used examples of the "ps" command in Linux. 

You can use these examples as it is or you can also combine this with grep and other commands to extract useful information about your processes. I will also share some useful ps command line options like "ps -ef" and "ps -auxxx" which you can use to find the full command-line argument used to run the process. 

They are also the most common way to use the ps command in Linux. Btw, if you are new to Linux  I highly recommend you check Linux Mastery: Master the Linux commands in 11.5 hours on Udemy to learn all the essential Linux commands which every developer should learn. 




10 Examples of ps command in UNIX

Without wasting any more of your time, here are some of the useful ps command examples in Linux

1. ps -fu example


for seeing process running under app_ev username :

$ ps -fu app_dev

2. ps + grep example


ps command to find all java process in your UNIX host

$ ps -ef | grep java

3.  !ps example


A shortcut for running the last ps command

!ps

4.  ps -ef | wc example


ps command to find the number of process running in your Linux server

$ ps -ef | wc -l


5.  ps -ef command example


ps command to see the full command-line arguments used to start a process for every user in the Linux server. 
$ ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
user           1       0  0 12:27 ?        00:00:00 /cocalc/bin/tini -- sh -c env -i /cocalc/i
user           6       1  0 12:27 ?        00:00:00 sh -c env -i /cocalc/init/init.sh $COCALC_
user           7       6 10 12:27 ?        00:00:02 node --optimize-for-size --gc-interval=100
user          19       7  0 12:27 ?        00:00:00 sshd: /usr/sbin/sshd -D -p 2222 -h /tmp/.c
user         223       1  0 12:27 ?        00:00:00 /cocalc/nvm/versions/node/v12.19.0/bin/nod
user         470       7  1 12:27 pts/0    00:00:00 /bin/bash
user         504     470  0 12:27 pts/0    00:00:00 ps -ef

You can not only find the PID of the process but also the exact command which is used to run the process as shown in the "CMD" column. The "ps -ef" command also shows the process owned by every user "-e" and "-f" is for full. 

6.  ps command example


This is the ps command to see the process for the current user. 
~$ ps
    PID TTY          TIME CMD
    470 pts/0    00:00:00 bash
    541 pts/0    00:00:00 ps
The ps command without any argument just shows the process started or owned by the current user. 


7. ps -f example


ps command to see the full information of all the processes for the current users. 

~$ ps -f
UID          PID    PPID  C STIME TTY          TIME CMD
user         470       7  0 12:27 pts/0    00:00:00 /bin/bash
user         588     470  0 12:31 pts/0    00:00:00 ps -f

The -f argument tells the ps command to supply full information about the processes it displays. In this example, ps displays full information about the processes you are running. You can further see this Linux Command Line Interface (CLI) Fundamentals course on Pluralsight to learn about the ps command. 

ps command examples in Linux



8.  ps -e example


ps command to show every process running on the Linux system

~$ ps -e
    PID TTY          TIME CMD
      1 ?        00:00:00 tini
      6 ?        00:00:00 sh
      7 ?        00:00:06 node
     19 ?        00:00:00 sshd
    223 ?        00:00:00 node
    470 pts/0    00:00:00 bash
    607 pts/0    00:00:00 ps


Now, you can also understand why "ps -ef" shows you the full information for every user because we have combined the "-e" and "-f" options that stand for every and full information.  This option is so popular the I always use this ps command to find any process in Linux followed by the grep command


9. ps -ef example


ps command to see the process for a particular Linux user. 

~$ ps -fu user
UID          PID    PPID  C STIME TTY          TIME CMD
user           1       0  0 12:27 ?        00:00:00 /cocalc/bin/tini -- sh -c env -i /cocalc/i
user           6       1  0 12:27 ?        00:00:00 sh -c env -i /cocalc/init/init.sh $COCALC_
user           7       6  1 12:27 ?        00:00:07 node --optimize-for-size --gc-interval=100
user          19       7  0 12:27 ?        00:00:00 sshd: /usr/sbin/sshd -D -p 2222 -h /tmp/.c
user         223       1  0 12:27 ?        00:00:00 /cocalc/nvm/versions/node/v12.19.0/bin/nod
user         470       7  0 12:27 pts/0    00:00:00 /bin/bash
user         664     470  0 12:35 pts/0    00:00:00 ps -fu user

This ps command shows full information about the processes currently being run by the user named "user", you can specify any user by using the "-u" command


10. ps -auxwww example

ps command to show full arguments for really long commands used to start a process. 
~$ ps -auxwww
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
user           1  0.0  0.0   2492   604 ?        Ss   12:27   0:00 /cocalc/bin/tini -- sh -c env -i /cocalc/init/init.sh $COCALC_PROJECT_ID $KUCALC_IMAGE_NAME $COCALC_EXTRA_ENV
user           6  0.0  0.0   2608   548 ?        S    12:27   0:00 sh -c env -i /cocalc/init/init.sh $COCALC_PROJECT_ID $KUCALC_IMAGE_NAME $COCALC_EXTRA_ENV
user          19  0.0  0.0  12168  7088 ?        S    12:27   0:00 sshd: /usr/sbin/sshd -D -p 2222 -h /tmp/.cocalc/ssh_host_rsa_key -o PidFile=/tmp/.cocalc/sshd.pid -f /cocalc/init/sshd_config [listener] 0 of 10-100 startups
user         223  0.0  0.1 852056 39300 ?        Ssl  12:27   0:00 /cocalc/nvm/versions/node/v12.19.0/bin/node /cocalc/nvm/versions/node/v12.19.0/lib/node_modules/npm/node_modules/update-notifier/check.js {"pkg":{"name":"npm","version":"6.14.8"}}
user         470  0.0  0.0   7844  6016 pts/0    Ss   12:27   0:00 /bin/bash
user         683  0.0  0.0   7872  3380 pts/0    R+   12:37   0:00 ps -auxwww
You can see that ps -auxwww is showing really long commands while ps-ef truncated them. 


Difference between ps -ef and ps -auxwww?

This is one of the most popular Linux interview questions and it was asked to me almost 3 to 4 times and I also ask this question to almost every person I interview as it is very useful during troubleshooting. The latter shows a very long command line while the former truncates them. 

Sometimes a  particular process with a really long command is not get captured by ps - ef instead they got caught up by the ps -auxwww command. So, it's always better to use the ps -auxwww process while troubleshooting, particularly if you don't see the process after running the ps -ef command. 


Further Learning
Linux Command Line Basics
Linux Command Line Interface (CLI) Fundamentals
Learn Linux in 5 Days and Level Up Your Career


Other Linux command tutorials you may like to explore
  • 10 example of the networking command in Unix (example)
  • My favorite Linux courses for beginners (Linux online courses)
  • 10 Example of tar commands in Unix (example)
  • 6 Free Online course to learn Bash (free course)
  • 5 Example of kill commands in Unix and Linux (example)
  • Top 5 Bash Scripting course for Beginners (bash courses)
  • Top 10 Courses to Learn Linux commands (courses)
  • VI Editor examples and tips for beginners (example)
  • How does nslookup command work in UNIX? (answer)
  • 10 books to learn essential commands of Linux or UNIX? (list)
  • How to use the CUT command in Linux (tutorial)
  • How to exit from the telnet command in UNIX? (example)
  • How to send an HTTP request to call a web service from the command line in Linux? (example)

Thanks for reading this Linux tutorial so far. If you like these Linux ps examples, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a comment.

P. S. - If you are looking for some free courses to learn or improve your Linux skills, you should check out this list of Free Linux Courses for Programmers and Developers.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.