How to use the ps command to monitor Linux processes

Contents

A terminal window on a Linux laptop.

Get a snapshot of the processes running on your Linux computer with the ps command. Locate processes by name, user or even terminal with as many or as few details as you need. We show you how.

Process management in Linux

The heart of all Linux and Unix-like operating systems is the kernel. Among his many responsibilities is the allocation of system resources, like RAM and CPU time. These need to be combined in real time so that all running processes get their fair share, according to the priority of each task.

Sometimes, tasks can crash, entering a closed circuit or becoming unresponsive for other reasons. Or they can continue to run, but they consume too much CPU or RAM time, or behave in an equally antisocial manner. Sometimes, chores should be removed as a mercy to everyone involved. The first step. Decidedly, is to identify the procedure in question.

But maybe you don't have any homework or performance issues at all. Maybe you are just curious what processes are running inside your computer and would like to take a look under the hood. the ps The command satisfies both needs. Gives it to you a snapshot of what is happening inside your computer “now.”

ps is flexible enough to give you exactly the information you need in exactly the format you would like. Actually, ps you have many options. The alternatives described here will cover the most common needs. If you want to delve into ps of what we have taken in this post, you will find that our introduction makes the man page easier to digest.

Listing processes

The easiest way to use ps is to turn it on without parameters:

ps

ps displays a list of processes started by the user who ran the command.

The four columns are:

  • PID: The procedure identification number of the procedure.
  • TTY: The name of the console the user is logged into.
  • WEATHER: The amount of CPU processing time used by the procedure.
  • CMD: The name of the command that started the procedure

Listing procedure for all users

adding the -e (select all processes) we can do ps list the processes that have been started by all users, not just the user who is running the ps command. Because this will be a long list, we are integrating it in less.

ps -e | less

The process list is piped to less.

We have many more entries in the list, but we see the same four columns as before. Entries with a question mark ? at TTY The column did not start from a terminal window.

Show process hierarchy

Sometimes, You can help fix a roadblock or identify a particular procedure if you can see which processes started other processes. We use the -H (hierarchy) option to do it.

ps -eH | less

Indentation indicates which processes are parents of which other processes.

To add a little more clarity, we can ask ps to add something ASCII lines and to draw the hierarchy as a tree. The option to do this is the --forest option.

ps -eH --forest | less

This makes it easy to keep track of which processes are the parents of other processes..

List of processes by name

You can pipe the output from ps through grep to list entries that have names that match the search term. now. “now.”:

ps -e | grep firefox

For this case, the output is a single input for the procedure that interests us. Decidedly, if we had launched multiple instances of Firefox, there would be more than one item returned in the list.

Show more columns in the output

To add more columns to the output, use el -f option (full format).

ps -ef | less

An additional set of columns is included in the output of ps.

The columns are:

  • UID: User ID of the owner of this procedure.
  • PID: The procedure ID of the procedure.
  • PPID: ID of the main procedure of the procedure.
  • C: The number of children the procedure has.
  • WEATHER: Start time. The moment the procedure began.
  • TTY: The name of the console the user is logged into.
  • WEATHER: The amount of CPU processing time used by the procedure.
  • CMD: The name of the command that started the procedure.

Using the -F (full extra format) we can get even more columns:

ps -eF | less

The columns we get this time require the screen to scroll sideways to reveal them all.

now. “Right arrow”, the screen scrolls to the left.

The columns we get now are:

  • UID: User ID of the owner of this procedure.
  • PID: The procedure ID of the procedure.
  • PPID: ID of the main procedure of the procedure.
  • C: The number of children the procedure has.
  • SZ: Procedure image size in RAM pages.
  • RSS: Resident set size. This is the unchanged physical memory used by the procedure.
  • PSR: The processor to which the procedure is assigned.
  • WEATHER: Start time. The moment the procedure began.
  • TTY: The name of the console the user is logged into.
  • WEATHER: The amount of CPU processing time used by the procedure.
  • CMD: The name of the command that started the procedure.

List of processes by procedure ID

Once you have found the procedure ID for the procedure you are interested in, you can use it with the ps command to list the details of that procedure. Use the -p (choose by procedure ID) option to achieve this:

ps -p 3403

Details of this procedure are listed:

You are not restricted to a procedure ID. You can provide a list of process IDs, separated by spaces.

List of processes by command

the -C The option (command) enables you to search for a procedure using the command name. In other words, the name of the command that started the procedure. This is subtly different from the command line, which can include path names and parameters or options.

ps -C shutter

Details of the shutter procedure are listed.

List of processes owned by a user

To view the processes that are owned by a particular user, use el -u (user list) option:

ps -u mary

The processes owned by the mary user account are shown.

List of processes by terminal

To view the processes associated with a TTY, use el -t (choose by TTY) option. Used without a TTY number, the -t The option reports on the processes associated with the current terminal window.

tty
ps -t

the tty command reports that this is pseudo-ticker 0. The processes listed by ps -t are all associated with TTY pts/0.

If we pass a TTY number on the command line, we should get a report on the processes associated with that TTY.

ps -t 1

This time all processes are associated with TTY pts/1.

RELATED: What is a TTY in Linux? (and how to use the tty command)

Choose columns to display

With the -o (format) you can choose the columns you want to include in the output of ps. Columns are specified by name. The list (long) column names can be seen in the man page now. “now.”. In this example, we choose to have the CPU time (pcpu) and the command line with arguments (args) included in the output.

ps -e -o pcpu,args | less

The output only includes our two requested columns.

Sort the output by columns

You can order the output for you using the --sort option. Let's sort the output by the CPU column:

ps -e -o pcpu,args --sort -pcpu| less

now. “-” about him pcpu The sort parameter gives a descending sort order.

To see the ten most CPU intensive processes, channel the outlet through the head command:

ps -e -o pcpu,args --sort -pcpu | head -10

We get an ordered and truncated list.

If we add more columns to our screen, we can sort by more columns. Let's add the pmem column. This is the percentage of the computer's memory that is being used by the procedure.. now. ” +“, Sort order is ascending.

ps -e -o pcpu,pmem,args --sort -pcpu,pmem | head -10

We get our additional column and the new column is included in the classification. The first column is sorted before the second column, and the second column is sorted in ascending order because we didn't put a dash in pmem.

Let's make it a little more useful and add in the procedure ID column (pid) so we can see the procedure number of each procedure in our list.

ps -e -o pid,pcpu,pmem,args --sort -pcpu,pmem | head -10

Now we can identify the processes.

Ps -e -o pid output, pcpu, pmem, args --sort -pcpu, pmem |  head 10

Kill processes by procedure ID

We have covered a range of alternatives to identify processes, including name, the command, user and terminal. We also cover alternatives to identify processes by their dynamic attributes, like CPU and memory usage.

Then, one way or another, we can identify the processes that are running. Knowing your procedure ID, we can (if required) delete any of those processes using the kill command. If we wanted to kill the procedure 898, we would use this format:

sudo kill 898

If everything goes fine, the procedure ends silently.

RELATED: How to kill processes from the Linux terminal

Kill processes by name

the pkill the command enables you kill processes by name. Make sure you have identified the correct procedure! This command will finish the upper procedure.

sudo pkill top

One more time, not having news is good news. The procedure ends silently.

Kill multiple processes by name

If you have multiple copies of a running procedure, or a procedure has spawned multiple child processes (how can google chrome do), How can you remove them? That's just as easy. We use the killall command.

We have two copies of top running:

ps -e | grip top

We can finish both with this command:

sudo killall top

If there is no answer, no problems, so both processes have ended.

sudo killall top output in a thermonal window

Get a dynamic view with the top

The output of ps it's a snapshot view. It does not actualize. To get an up-to-date view of your processes, Use the top command. Provides a dynamic view of the processes running on your computer. The screen is divided into two parts. There is a dashboard area at the top of the screen made up of lines of text and a table at the bottom of the screen made up of columns.

Start top with this command:

top

The columns contain information about the processes:

  • PID: Procedure identification
  • USERNAME: Name of the holder of the procedure
  • PR: Priority procedure
  • NI: The good value of the procedure
  • VIRT: Virtual memory used by the procedure
  • RES: Resident memory used by the procedure
  • SHR: Shared memory used by the procedure
  • S: Status of the procedure. See the list below of the values ​​this field can take
  • %UPC: the percentage of CPU time used by the procedure since the last update
  • % MEM: percentage of physical memory used
  • WEATHER +: total CPU time used by the task in hundredths of a second
  • COMMAND: command name or command line (command line name and parameters) If you can't see the command column, press the "Right Arrow" key.

Procedure status can be one of the following:

  • D: Uninterrupted sleep
  • R: In a hurry
  • S: Asleep
  • T: Tracing (stopped)
  • WITH: Zombie

Press the key “Q” now. top.

RELATED: 37 Important Linux Commands You Should Know About

Before killing a procedure

Make sure it is the one you are looking for and verify that it does not cause you any problems. In particular, It is important to highlight consult with the -H (hierarchy) and --forest options to make sure you don't have important child processes that you have forgotten.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.