An intro to Process Management in Linux
A process is a program that is running and a Linux system typically has hundreds of processes running simultaneously.
Let's take a look at
- Viewing processes
- Finding processes
- Managing processes
- Prioritizing processes
- Killing processes
- Running processes in the background
- Scheduling processes
Viewing processes
The Linux Kernel assigns a unique process ID (PID) to each process sequentially, as the processes are created. In general, to perform any action on a process, we must specify the PID
, sometimes we can use the name.
The ps
command is the primary tool for viewing processes. Running it without any options lists the processes started (invoked) by the currently logged-in user and what processed are running on that terminal.
If we add the aux
options
- a = show processes for all users
- u = display the process's user/owner
- x = show processes not attached to a terminal
The processes are displayed in the order they were started, so you'll see in ordered by PID
.
There are several columns are info, let's go over a few:
- USER: The user who invoked the process.
- PID: The process ID.
- %CPU: The percent of CPU the process is using.
- %MEM: The percent of memory the process is using.
- COMMAND: The name of the command that started the process.
Finding Processes
We can use the grep command to find a specific process, let's say we want to find all processes with mfsconsole
in the name
ps aux | grep msfconsole
We can use the top
command to dynamically list processes ordered by resources used, starting with the largest. By default this list will refresh every 3 seconds.
top
Managing Processes
Priorities
The kernel will have the final say over the priority of a process, but we can use the nice
command to suggest that a process should be elevated in priority. The values for nice range from -20 (most likely to receive priority) to +19 (least likely to receive priory).
A high nice
value translates to a low priority (when you are being nice to other users and processes), and a low nice
value translates to a high priority (when you are not being so nice to other users and processes).
When a process is started, all standard processes are launched with a nice
value of 0.
We can use the nice
command to set the nice value when a process is launched and we can use renice
to adjust the nice value of a running process.
When we start a process using the nice
command but without providing any value, the default nice
value is 10.
The nice
command requires that you increment the nice value, the renice
command wants an absolute value for niceness.
Setting the Priority When Starting a Process with nice
We can use the nice
command to adjust the nice value for a program as we launch it. This allows us to increase or decrease the priority given to the process by the kernel, relative to the other processes.
On the left, I've execute watch -n1 free
to show me details on the system’s memory usage. On the left I've got top
running and you can see that the watch
command has PID
9717 and a nice
value of 0.
Let's now execute the same watch
command, except this time with the nice
command.
nice -15 watch -n1 free
Now we see watch
has a nice value of 15. A few things to keep in mind here:
- The watch command has a different
PID
than the previous watch command. That's becausenice
will start a new process and not change an existing process. - With the nice command
-15
means 15. If we wanted to specify a negative number (higher priorty) we would use double - - Here is what
sudo nice --10 watch -n1 free
would look like. Yes you have to use sudo if you are increasing priority. Anyone can decrease priority, only sudo can increase.
Changing the Priority of a Running Process with renice
The renice
command takes absolute values between -20 and 19 and requieres the PID
of the process.
Let's run our watch command
again.
watch -n1 free
Let's check it's nice
value, since we didn't specify it, it should be 0. Instead of using top
I'll use ps
and grep
here, to simplify the output.
We can see the eighth column is 0, which is the nice
value and the PID
is the third column. Let's now use renice
sudo renice -15 14318
We can see that the nice
value is now -15
We can also use the top
utility to change the nice
value.
In top
hit R
and supply a PID
Hit enter and provide new nice
value
top
has changed the nice
value
Killing Processes
You can stop a problematic process with the kill
command. The kill
command has 64 different kill signals and the syntax is kill -signal PID
If you don't provide a signal flag, it will default to SIGTERM. I'll focus on a few here.
SIGNAL NAME | NUMBER | DESCRIPTION |
---|---|---|
SIGHUP | 1 | The Hangup (HUP) signal. It stops the designated process and restarts it with the same PID. |
SIGINIT | 2 | The Interrupt (INT) signal. It's a weak kill signal that isn't guaranteed to work, but does it cases. |
SIGQUIT | 3 | The core dump. Terminates the process and saves the process information in memory, and then it save this information in the current working directory to a file named core. |
SIGTERM | 15 | The Termination (TERM) signal. It is the kill command's default kill signal. |
SIGKILL | 9 | This is the absolute kill signal. It forces the process to stop by sending the process's resources to a special device /dev/null. |
This command would restart our watch
command with the HUP signal.
kill -1 14318
This command would make certain that the process is terminated
kill -9 16318
If we don't know the PID
we can use the killall
command, it takes the name of the process.
killall -9 watch
Running Processes in the Background
When you execute a command, the shell waits until the command is complete before offering another command prompt. We can run a process in the background, it will continue to run without needing the terminal, freeing the terminal up for other jobs. We do this buy appending an ampersand to the end of a command.
geany sample.txt &
geany
is no longer taking up the entire terminal.
How do we bring it back to the foreground? Use the fg
command with the PID
fg 18345
You can also move a process to the background using the bg
command.
bg 18345
Scheduling Processes
In Linux we can schedule processes with at
and crond
. crond
is a little more involved, I'll have a separate post for that.
The at
command
The at
command is useful for scheduling a job to run once at some point in the future. It sets up the atd
daemon. A daemon is a program that sits in the background and does its thing without having any user interface.
The syntax is at
followed by the time to execute the process. The time can be provided in various formats.
TIME FORMAT | MEANING |
---|---|
at 7:20pm | Run at 7:20 PM of current day. |
at 7:20pm June 25 | Run at 7:20 PM on June 25 |
at now + 20 minutes | Run in 20 minutes |
at 7:25pm 06/10/2021 | Run at 7:25 pm on June 10, 2021 |
at noon
We can see that at
puts us into interactive mode, here we type in the command we want executed at the specified time. Hit CTRL+D when you're done.
Use atq
to list all scheduled at
jobs.