How to Use the ps Command in Linux to Kill Process

Ps Command

When you are working, sometimes your programs suddenly freeze. Other times, the programs are still running but with a high processor or memory consumption. There is a way to fix this in Linux using the ps (Process Status) command. Here we show you how to use the ps command in Linux to list the currently running processes and their PIDs. You can then find and kill the processes consuming your resources.

Using the “ps” command

The ps command can be used on its own. It will output four columns of information:

  • PID: the unique identifier of the process defined by the system. It’s the value that we use to stop a process.
  • TTY: terminal from which the process was started.
  • TIME: the total amount of CPU time used by the process.
  • CMD: command that generates the process
Ps Output Command 1

Note that when you use the command without any options, it doesn’t show you much information. Here are a few ways to make it more useful.

1. List the process of all users

When some programs are installed, they sometimes also create some additional users to run the process. To list the processes of the users, use the -e option:

and its output:

2. List process with more information

It’s possible to have more information when you list the running process. To do this, you can use the ef option.

and its output:

3. Filter the Process by Process ID

If you know the process ID of the running process you want to show, you can filter for it specifically with the -p flag. This can take multiple PIDs as arguments, separated by a single comma and no space.

4. List the processes owned by a user

You can also list the processes that are owned by a user with the u option followed by the name of the user:

and its output:

5. List the actives processes

It’s possible to list all the processes that are active by using the ax option:

and its output:

6. List the active processes with the users

It’s possible to list all the active processes with the users when you add the -aux flag:

and its output:

7. Filter the process by the name of a program

It’s possible to retrieve the information about a specific program that is running by applying a filter on the ps result:

and its output:

Alternatively, you can also use the C option to filter the process by its name:

Use Ps On Linux C Flag

8. Display Specific Columns

In addition to the four default columns, you can get ps to display an additional column of information. For example:

Use Ps On Linux O Flag

The -o flag sets specific output display options for the ps command’s results. See a full list of standard display options for ps.

9. Display Results in Hierarchical Tree Style

This uses ASCII art to create a tree-style structure for displaying processes. Shows forked and children processes as descendants of the appropriate parent processes, sorting to match. To hide the “branches” of the tree, use -H in place of --forest.

10. Display Process Threads

Use Ps On Linux L Threads Flag

The -L flag toggles on a threaded display for any functionality of ps. It’s most useful when tracking down the threads of a specific process.

11. Show All Root Processes

Use Ps On Linux Root Processes 2

Execute a search for all processes running with real and effective root identifications. This shows them in the full-length format, thanks to the -f flag. You can combine it with the -o flag to customize output.

Use the kill command to stop a process

Once you have located the misbehaving process, you can use the kill command to kill a process that is running. The command sends a signal to a process that terminates it. When your programs are frozen, most of the time you will need to forcefully kill them with the -9 option.

Kill Output Command 1

The output of ps is an instant view. Unlike htop, It does not update itself dynamically. This means you might have to run it multiple times to get a clear picture of which process is misbehaving. To get an up-to-date view of the processes, you can try some other commands for the Linux system.

Related:

Alain Francois

A Linux system administrator passionate about Open-Source environments and working on installations, deployments for different IT solutions and cloud environments. I like to share my knowledge regarding the technologies that I can discover and use

One comment

  1. As I have my “conky” listing some processes down the right side of my screen, I can see the top 6 of my processes and memory users. If I need to ‘kill’ a process I like to use ‘pidof’ to find the number of the process to stop.
    ie … pidof conky
    1234
    kill 1234
    so simple, so quick!

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.