There comes a time where you need to find information about a certain process, often to find out what its ID is to parse to the ‘kill’ command. However, it need not necessarily be for that, of course. Two commands will be explained in this guide: ‘ps‘ and ‘top‘. Both of these should be in a standard Linux installation.
What is ‘ps’?
Short for process state, ‘ps‘ is a program that can read all of the process information on your computer, display the output on the terminal and exit. Information can include the process ID, user, and group it belongs to, the running state, and whether it was launched from a terminal.
It can also be used to list certain processes or all of them. It accepts standard style arguments (with a dash e.g. ‘-e’) as well as BSD-style arguments (without a dash), and GNU-style syntax (those preceded by two dashes). Consult the man page for more information.
What is top?
Like ps, the top is a program that lists information about processes in the system. Instead of printing a list to the terminal, though, it provides a real-time view of the processes with memory and CPU usages shown.
Each desktop environment typically comes with their own apps for viewing process activity. They typically feature graphs representing memory and CPU usage as well as network and disk usage by a process. These won’t be covered in this guide.
Like many Linux commands, this one is simple to use. When invoked with no arguments, it will print out some process information about ‘ps’ and the shell that ‘ps’ ran from. Here is an overview of some of the ways you can use ps.
Listing All Processes
You can use ps to show all of the processes running on your computer. To print a list of all processes and their details, use the ‘-e’ parameter to list all processes.
$ ps -e
For extra information about a process including its parent process ID (PID), and the username it belongs to, couple it with the –‘f‘ parameter.
$ ps -ef
For even more information, use the ‘-F’ parameter.$ ps -eF
If you only wish to view information about a specific process, or processes belonging to certain users, or filter by process names, there certainly some options to take advantage of. For instance, if you wanted to list all processes belonging to a user, such as a root, you would run this command:$ ps -u root -U root u
You can also view information about a specific process using the ‘-p’ parameter. This will require you to find out the process ID using ‘ps’ or even ‘top’ before-hand.
<strong>$ ps -lp <process-id></strong>
The ‘-l’ parameter is for displaying extra information, like the ‘-f’ parameter with a few differences, like the username isn’t displayed, but the user ID (UID) is.
Threads can also be shown alongside processes using the ‘L’ parameter, which will display the LWP column. This is where you will see IDs for each thread associated with a process. The ID of the first thread will always be identical to the process ID it came from.$ ps -eLf
Use the ‘-p PID’ argument to show threads from a particular process:
<strong>$ ps -Lfp <PID></strong>
You may pipe output from ps to grep if you are looking for specific information that you may not be easily able to obtain using the parameters. This method can be used to find certain processes with a specific name.
$ ps -ef | grep chromium-browser
Choosing What To Show
Using the ‘-o’ parameter allows you to define your own output format. You can choose to show the PID, PPID, user, and the command and its arguments in the output, for example.
$ ps -eo pid,ppid,%cpu,%mem,uname,comm,args
This is used when you wish to have a real-time view of all of the processes on your system. When run with no arguments, a full list of processes will be shown with the process with the most CPU usage always at the top.
Navigation is best done using the PgUp and PgDn buttons, with the Home and End buttons used to go to the top and bottom of the list, respectively. As with many things, the man page contains a lot of excellent information about its usage.Here is top launched with no arguments:
Show All Processes Belonging To A User
Top will accept either the username or UID using the ‘-u’ parameter to only show the processes belonging to that user.
$ top -u <UID|Username>
Monitoring a Specific Process
A process ID can be parsed to top so as to monitor its activity using the ‘-p’ argument:
$ top -p <PID>
Choosing What To Show
Like ps, you can have top display as much or as little information about each process by specifying columns to show. You can change which columns to show by pressing ‘f’ while top is running. Use the up and down arrow buttons to navigate, and press the space bar to select and deselect the columns. You can even change the sorting of the process list by pressing ‘s’ on the selected column.
Like ps, top can show individual threads from each process together with the processes using the ‘-H’ parameter. The thread ID will be listed under the PID column.$ top -HTo show threads from a particular process, run this:
$ top -Hp <PID>
One Practical Use
I use these programs (ps more than top) to track down a troublesome process and fetch its ID to kill it. The ‘kill’ command will normally send a TERM signal to a process to kill it.
$ kill <PID>
If this doesn’t do the trick, you can have it send the KILL signal (‘-9’ parameter), which cannot be caught by programs. This is the last resort, though.
Displaying process information is a simple process in Linux using either ‘ps‘ or ‘top‘ from the terminal. You can control how much detail you wish to view the processes that are running or sleeping on your system. The man pages, as well as Google searches, provide plenty of information so do look around.