Thursday, July 29, 2021

10 Example of PS command in Linux and UNIX

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 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 these best Linux beginner courses from Coursera and Udemy to learn all the essential Linux commands which every developer should learn. 




10 Examples of ps command in Linux and UNIX

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

1. ps -fu example

This is one of the most useful ps command examples which you can use for seeing all the processes running under a particular username, in this case, its app_dev username. We have used two ps command-line options here -f and -u, -f stands for full which means it prints all the details while -u stands for the user, which means it will print all the processes owned or started by a particularly username. 

$ ps -fu app_dev

2. ps + grep example

This is my favorite ps command exam which you can use to find all java processes in your UNIX host. This command list all the process and because it uses -e and -fit also prints the full command which is used to start the process. This means if it's a java process then the java command will be present in the output and that's what we are searching for using the grep command. 

$ ps -ef | grep java

You can actually change the filtering condition on grep to find any kind of process. For example, I often replace"java" with "tomcat" to find all the tomcat running instances on the box. 

3.  !ps example

A shortcut for running the last ps command. You can use this command to find the last process you have identified. You don't need to specify all the command line options again, Linux will pick it up from the last instance of the ps command you have run.

!ps

4.  ps -ef | wc example

This is another common example of the ps command in Linux. You can use this ps command to find the number of processes running in your Linux server. This is mostly used by system admins and root users to check if there are not too many processes running on the host or not. Again, here we have used the -e and -f option of the ps command which prints every process with full details. Output is then passed to wc command which counts a number of lines using the -l option. 

$ ps -ef | wc -l


5.  ps -ef command example

This is the most popular ps command example and it's also my favorite. I used this ps command to see the full command-line arguments used to start a process for every user in the Linux server. I almost used it many times a day often with grep command as shown in the second example in this list to find out whether a particular server process is up or not and its PID to further investigate which ports are owned by this process and so on. 
$ 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 simples ps command example to see the process for the current user. Here we have not used the -u option unlike in the earlier example, hence it will display all the processes by 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

This is another ps command example to see the full information of all the processes for the current users. Here we have used -f which will show all the columns that the ps command can output but it won't show all the processes running on the host, instead, it will just show the process for the current user. 

~$ 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 these online Linux command line courses to learn about the ps command. 

ps command examples in Linux



8.  ps -e example

This ps command example is 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

Another popular ps command example to see the process for a particular Linux 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

~$ 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


10. ps -auxwww example

This is another common ps command example which many developers and IT professional uses to check if a particular process is running or not. This is a ps command to show full arguments for really long commands are 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 command?

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. 


Other Linux command tutorials you may like to explore
  • 10 examples 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 the 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.