Suppose you find yourself exploring the Linux command line for the first time or entering into Linux administration. In that case, a low-level understanding of how to get around the terminal and complete basic tasks is essential. To help you grasps those concepts, check out my previous two articles:
However, if you feel comfortable with those concepts, we will advance your Linux knowledge a bit further in this article. We will be looking at processes and how to manage them.
So, what exactly is a process?
In Linux, a process is any active (running) instance of a program. But what is a program? Well, technically, a program is any executable file held in storage on your machine. Anytime you run a program, you have created a process. At a basic level, this is pretty easy to manage, and that is what we are going to take a look at today.
What you need to get started
I recommend that you follow along on your favorite virtual machine. That way, you can try and fail without consequence (which is definitely the best way to get comfortable at the terminal).
For this demo, I am going to start the sleep process for 500 seconds. This approach allows you to see the process without me making meaningful changes to my system.
[tcarrigan@client ~]$ sleep 500 ^Z + Stopped sleep 500
I then stopped the process with Ctrl+Z so that we can use our terminal.
1. List processes
To display your currently active processes, use the
[tcarrigan@client ~]$ ps PID TTY TIME CMD 2648 pts/0 00:00:00 bash 3293 pts/0 00:00:00 sleep 3300 pts/0 00:00:00 ps
Here you get a little information about the active processes on your system. You will want to pay attention to the PID (unique process ID), the TIME (amount of time that the process has been running), and the CMD (the command executed to launch the process).
2. Verbose list (processes)
To see an incredibly detailed list of processes, you can use the
ps aux command.
- a - all users
- u - shows the user/owner
- x - displays processes not executed in the terminal (making the output rather long)
You can see the command here (output edited for length):
[tcarrigan@client ~]$ ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND tcarrig+ 3293 0.0 0.0 215292 520 pts/0 T 13:41 0:00 sleep 500 root 3380 0.0 0.0 0 0 ? I 13:45 0:00 [kworker/1:1-mm_percpu_wq] root 3381 0.0 0.0 0 0 ? I 13:45 0:00 [kworker/1:3] root 3398 0.0 0.0 0 0 ? I 13:46 0:00 [kworker/3:2-ata_sff] root 3481 0.0 0.0 0 0 ? I 13:50 0:00 [kworker/u8:2-flush-253:0] root 3482 0.0 0.0 0 0 ? I 13:50 0:00 [kworker/0:1-events] root 3483 0.0 0.0 0 0 ? I 13:50 0:00 [kworker/0:2] root 3508 0.0 0.0 0 0 ? I 13:51 0:00 [kworker/3:0-ata_sff] root 3511 0.0 0.0 18892 7732 ? S 13:52 0:00 systemd-userwork root 3512 0.0 0.0 18892 7656 ? S 13:52 0:00 systemd-userwork root 3513 0.0 0.0 18892 7656 ? S 13:52 0:00 systemd-userwork root 3566 0.4 0.0 432792 8024 ? Ssl 13:54 0:00 /usr/libexec/fprintd tcarrig+ 3598 0.0 0.0 228208 3948 pts/0 R+ 13:54 0:00 ps aux
3. Kill by PID
Inevitably, a process will get hung, and you will need to
kill it. The more time you spend at the CLI, the more likely it is you will need the
kill command. The most accurate way to identify a process is by process ID (PID).
Use the following syntax:
[tcarrigan@client ~]$ kill PID
This command sends the SIGTERM signal. However, if you are dealing with a stuck process, add the
[tcarrigan@client ~]$ ps PID TTY TIME CMD 2648 pts/0 00:00:00 bash 3293 pts/0 00:00:00 sleep 4684 pts/0 00:00:00 sleep 40527 pts/0 00:00:00 sleep 40540 pts/0 00:00:00 ps [tcarrigan@client ~]$ sudo kill -9 3293 [sudo] password for tcarrigan:  Killed sleep 500
4. Kill by name/keyword
killall command to kill a process by name. This command will kill all processes with the keyword/name that you specify.
The syntax is:
[tcarrigan@client ~]$ killall sleep
This would kill all
sleep processes active on the system (the
-9 option works here as well). Here is an example:
[tcarrigan@client ~]$ ps PID TTY TIME CMD 2648 pts/0 00:00:00 bash 4684 pts/0 00:00:00 sleep 40527 pts/0 00:00:00 sleep 40540 pts/0 00:00:00 ps [tcarrigan@client ~]$ killall -9 sleep - Killed sleep 500 + Killed sleep 500
These next two commands go hand in hand. They allow you to move/manage background commands. I will give a basic look at the syntax below; however, for an in-depth look at these commands, see my previous article on the subject.
5. List background jobs and resume background jobs
To list and manage background jobs, we will use the
bg command. I started a new
sleep 500 process and then stopped it, sending it to the background. Thus we see it listed when running
[tcarrigan@client ~]$ bg + sleep 500 &
6. Bring the most recent job to the foreground
To do this, we are going to use the
fg command. This brings the most recently run job/process to the foreground. The following example is a continuation of the above command. The
sleep 500 process that is in the background is now active in the background. Let's bring it into the light...
[tcarrigan@client ~]$ fg sleep 500
This command brings us to our final command in this list.
7. Bring a specific job to the foreground
fg command again, but select a specific job to bring to the foreground (instead of the most recent). To do this, we are just going to add the job/process name to the command.
[tcarrigan@client ~]$ fg XXXample
This brings job XXXample to the foreground.
[ Want to test your sysadmin skills? Take a skills assessment today. ]
In today's Linux Command Basics, we looked at processes and how to manage them. You can now complete general process management tasks—everything from listing and killing to moving between the background and foreground. If there are other general Linux administration areas that you would like to see a dedicated list of commands for, email the team at firstname.lastname@example.org, and I will do my best to flesh that out for you.