You are currently viewing ps command in Linux / Ubuntu

ps command in Linux / Ubuntu

ps command in Linux / Ubuntu

ps command in Linux / Ubuntu, which lets us to view the current running processes.its is also usefull in killing a process or to view which applications other users are running.

ps stands as abbreviation for “Process Status”

Syntax

ps [options]

1. Simple process selection :
Shows the processes for the current shell

ps

ps command in linux / ubuntu | ps

Where,
PID  the unique process ID
TTY  terminal type that the user is logged into
TIME  amount of CPU in minutes and seconds that the process has been running
CMD  name of the command that launched the process

Also please note TIME 00:00:00 means the total accumulated CPU utilization time for any process and 00:00:00 indicates no CPU time has been given by the kernel till now.


2. List All Process

ps -A

the -ax options in order to list all processes.

ps -ef

ps command in linux / ubuntu | ps -ef

Where,

-e to display all the processes.
-f to display full format listing.

In BSD we can use ps -aux

 ps -aux


3. List Processes As Tree

This command will display parent and child tree like structure information of process.

ps -A --forest

ps command in linux / ubuntu | ps -A --forest


4. List Process Info

ps -A

We will use -u option for this detailed information.


5. Show process by name or process id

ps -C apache2

ps command in linux / ubuntu | ps -C apache2

Here we will use -C parameter and process name for filter operation.

ps -p 2048,2184

ps command in linux / ubuntu | ps -p

We will use the -p option and PID in order to filter.

Often ps is used with grep like ps -aux | grep command to get the list of process with the given command.

ps -ef | grep apache2

ps command in linux / ubuntu | ps -ef

The grep command helps you filter the results from the ps command.


6. How to list all processes for a user

-u option to display all the process used by user. This supports the user ID or name.

ps -u ubuntu

ps command in linux / ubuntu | ps -u


7. How to list all processes for a group

To list all processes by group use the -g option. This supports the group ID or name.

 ps -g users


8. Display threads of a process

-L  will display the threads of the process. It will display all threads of a particular process.

Below command will display all the threads owned by the process with id 2048.

ps -p 2048 -L

ps command in linux / ubuntu


Now Some Advance Usage of ps command

Now below command will search for processes by the name apache2 and construct a tree and display detailed information.

ps -f --forest -C apache2

Turn ps into an realtime process viewer

 watch -n 1 'ps -e -o pid,uname,cmd,pmem,pcpu --sort=-pmem,-pcpu | head -15'

The output on my desktop is something like this.

ps command in linux / ubuntu

The output would be updated every 1 second to refresh the stats. However do not think that this is similar to top.


How to search for an kill a process

To  kill a process first we need to find process id (PID) then using the kill command to terminate the process. In the following example ps is piped to grep

Example

ps -ef | grep tomcat

now we got pid of tomcat

sudo kill -9 PID

You May Also Enjoy Reading This …