Thursday, August 4, 2022

10 Examples of ps command in Linux

In Today's article, You will be learning  about the "ps" command in Linux and  our objectives are: 

To know what Ps stands for, how it works with numerous options, and provide different examples and the usage of it.

PS stands for processes status. Basically used to list the processes that are running at the moment. As we all know that when a task needs to be implemented it must follow or have a process. There are different options you could use with it and this gives you a different result. 


10 ways to use ps commad in Linux

Now, Let us go through the ps command examples

1. "ps"



This is the simplest usage of ps, and the output is in four columns. the PID, TTY, TIME, and CMD.

PID  is the process Id, which must be unique.

TTY is the terminal type and this is what the user is logged into.

TIME - is the total amount of the CPU both in minutes and seconds that the process has been running. most times when you run the ps command you are likely going to see the time as 00:00:00.

CMD - is the name of the command line that carried out the process. For instance, mine is bash, yours could be different.



2. To print all processes in a different format.

"ps -A" or "ps -e"

There are two ways to go about this, either with the -A option or the -e option. they both give the same result. here is the output. I had to minimize it because it is a long one




3. To list the processes owned by the current user. 

Still the same simple way with Option x. so we have the below command with its output.

"ps x"




4. To display the process tree

"ps -e --forest"

A process tree shows how the system's processes are related or linked to each other and so we have the below 



5. To display in the user-oriented format

"ps u"



The option u that was provided enables the user-oriented format. So, it shows the information of the user who owns the processes.



 6. To view extra full format

"ps -F"

There could be times you want your processes to display in the full column format, using the above command gives us a full format display, so we have the below


If you check carefully we have the UID, PID, PPID, C, SZ, RSS, PSR, STIME, TTY, TIME, and CMD. So, it is still possible to specify columns based on what you want to achieve. So let us try that. For instance, I want the CMD, PID, and STIME alone so I am going to do:

 "ps -aN ---format cmd, pid, stime"

because I want more than one column it has to be comma separated.

So we have the below as output.




7. To display virtual memory format

"ps v 1"

Here we have the output



8. To display processes by group id or name

"ps -g"

There are two ways to do this, the first one is the option -g which I provided above and the second one is the option --group. The command above displays the processes owned by the group Id or name that you have specified. Note that if group ID works with -g, now let us see the result.


9.  To get all occurrences and PID of a program

"ps -C bash"

You may want to get the process ID of a program. This is important, the reason is that you may need to terminate a particular program or process for instance. How do you go about this?, you need to get the Process ID of that program first, then after that, you will be able to do whatever you need to do to it. Note that my own program name that I want to get all its occurrences is bash. So, you just have to put your own program name. Here is the result of the above command.

10 Examples of ps command in Linux



10. To find the process name from its PID
Now you have your PID, you could get the process name or information through the process ID.
Here we have:


CONCLUSION
In this article, you learned about PS (processes status). This utility is very important because there is no way you won't need anything to do with the status programs or processes. There are so many things associated with PS. The PID is the process ID, remember it is unique! , TTY is the type of terminal that the user is logged on, TIME is the amount of CPU both in minutes and seconds, etc. Ten (10) different examples or usage of ps were provided in this article so go to your command line and try it out.

Other Linux Articles and Resources you may like
  • How to set up cron jobs in Linux (Crontab example)
  • 5 Example of kill commands in Unix and Linux (example)
  • 10 examples of Networking commands in Unix (nslookup)
  • VI Editor examples and tips for beginners (vi examples)
  • 10 Linux command line courses for Beginners (courses)
  • How does the nslookup command work in UNIX? (answer)
  • Top 5 Courses to learn Vim Editor (online courses)
  • 10 examples of lsof command in Linux? (examples)
  • 10 Examples of chmod command in Linux (chmod)
  • How to use the netstat command to find which process is listening on a port? (example)
  • Linux find + du + grep example (example)
  • 7 Best Linux Courses for DevOps Engineers (Linux courses)
  • 10 Examples of curl command in Linux (cURL)
  • A Practical Guide to Linux Commands, Editors, and Shell Programming (guide)

Thanks for reading this article so far. If you like this ps command tutorial and my explanation of using ps command to find process and their status in Linux then please share it with your friends and colleagues.

No comments:

Post a Comment

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