Linux processes

To allow the user to manage and run different operations on operating system (OS) Linux, he has to be given rights to execute programs (more about that in our guide File and user management in command line).

Running programs are called processes in Linux. Each process contains in addition to a name (e.g. systemd) its own identifier (process ID or PID) represented with a number (systemd has PID 1). Each process (except the process PID 1) is executed by a different process. Process that executed another process (child), is called parent process and is identified with a parental identifier (parent process ID or PPID) represented with a number (e.g. 2). PID and PPID of separate processes is possible to write down with program ps (as Process Status) for example:

ps xafo user,pid,ppid,stat,pri,ni,%cpu,%mem,cmd

Program ps can give you more information about processes, but for the purpose of this guide we have chosen only username (user), PID, PPID, process status (stat), priority assigned by the core (pri), priority assigned by the user (ni as niceness), usage of CPU (%cpu), usage of RAM (%mem) and command of the running program (cmd) along with parent – child identification.

Dividing processes

Processes in Linux are divided into two large groups:

  1. automatic processes – these are automatically executed by the operating system (not the user) and run so to say on the background (background processes), and they do not require any input from the user (they are not interactive). These can be services (daemons) that Linux needs to secure its basic functions (like sshd orcrond).
  2. interactive processes – these are executed by the user and run so to say on the foreground (foreground processes). They expect an input from the user. This group can be set to run on the foreground or on the background. It can be programs like ps ortop.

Process states

Running program (proces) can acquire these states:

  1. Running (or R – process that is already running or waiting for allocation of CPU cores.
  2. Waiting – process that awaits an event or system resources. This state proces is dividet to:
    1. Interruptible (or S – process can be terminated
    2. Uninterruptible (or D) – process cannot be terminated
  3. Stopped (or T) – in this guide bellow we will show you how to terminate this process.
  4. Defunct (or Z) – processes that are terminated (parent) but still in the process list, because there are other processes to be terminated (child).

Running processes

To show you how the programs are executed and how to work with the processes, we will showcase program sleep. This program ensures that in the Linux command line waits for 1000 sec before continuing:

sleep 1000

If you don’t want to wait 1000 sec you can terminate the program with a key shortcut Ctrl + C or you can stop the program with shortcut Ctrl + Z. In the job manager we can find out, that there is only one process, that is stopped.

jobs

Stopped job sleep 1000 can be reinitiated with a help of program fg (as foreground) by transfering the task to foreground (first line). Then we can stop it with Ctrl + Z and with the help of program bg (as background) it can be transferred to background, where the countdown will finish (Running or S) and program sleep 1000 will terminate.

fg %1
bg %1

After you run program bg %1 (number after the percent from the job list jobs) a character & will be added to a program sleep 1000, and thus we have gained a parameter sleep 1000 &. The symbol & at the end of the command means that we want to run the program on the background and thus continue to work in the command line. If we need to run the program on the background, we can write it right away as:

sleep 1000 &

Running program can be delayed, if we add the process sleep before it wit the desired amount of seconds. If we need to run a program at the specific time and day, we have these two options:

  1. at – if we need to run the program in the future only once.
  2. cron (as Command Run ON) – if we need to run the program in the future repeatedly.

AT

Let say that we need to enter the content from a home directory to a text file at 12:00 during lunch (in 24 hour a day format). Since we will be on lunch and not at the computer, we will do the computer to do the job for us:

at 12:00

Press Enter and enter command:

ls -lah ~/ > ~/content_of_home_directory.txt

After you press Enter and then use key shortcut Ctrl + D, by which we complete the command, that will run in the future.

For sure we check, that the job is planned at the correct time (first line) with the correct command (second line):

at -l
at -c n

Symbol n changes for the number of task that we determined from the list of planned tasks (at -l).

If we would make a mistake, we can terminate the job with this command:

at -r n

Symbol n is changed for a number from the job list, that we learned before (at -l).

After the lunch we check, that the file exists and the content is correct and what we expected.

CRON

If we need to do this command every day at 12:00, it is easier to use cron. Firstly we check that for the currently logged user there is a table of jobs planned in the cron program:

crontab -l

If there is none the answer will be “no crontab for username” (while username will be replaced with the currect user), we can create the table:

crontab -e

Press Enter, file in the text editor nano will open and at the end of the opened file on the new line we add:

0 12 * * * ls -lah ~/ > ~/content_of_home_directory.txt

Save the file with Ctrl + X and then pres y and confirm with enter.

First value (0) is for minutes, second value (12) represents hours, third represents days in the month (1 - 31), fourth represents month (1 - 12) and fifth day of the week (0 - 6). In the places of third, fourth and fifth value there is wildcard symbol, that represents all possible values. At the end you set what command that will be executed.

Very handful tool is to enter the tasks to the table via online cron tool crontab.guru.

We will check thet the task has been written:

crontab -l

If it is written at the end, from now on every day at 12:00 the content of the home directory will write down into the content_home_directory.txt file.

The same way we have written down the job (crontab -e), we can also change it or delete it.

Process priority

For the Linux to be able to securely assign the processing time, it firstly needs to resolve the priority of the processes. The higher the priority, the sooner the process will gain the processor time and and the sooner will be executed. Sometimes we need the process to be executed as soon as possible, sometime we allow the longer period of time before execution (the process is not that important).

User can assign priority to the processes (nice), but the core (kernel) of the Linux can reassign the priority according to the current system load. It may happen that for the system a critically vital system will exploit the processor to the maximum. At that moment any process run by the user will have its priority reduced enough not to take any process time to the more important system processes.

The priority assigned by core is represented with a number between 0 – 139. Priority set by the user (nice) can be between -20 and +19. -20 means the lowest priority. The formula is: priority assigned by core = priority set by the user + 20.

If we would like the process to have the highest priory, we would run it as follows:

sudo nice -n -19 sleep 1000

Set the highest priority only after careful consideration of the possible consequences.

From 0 to +19 (positive values) nice can be without the root rights (e.g. nice -n 10 sleep 1000), but all bellow zero (negative values) has to be set via root(e.g. sudo nice -n -19 sleep 1000).

It is possible to change nice to already running processes. First we need to learn the PID of the process (first line) and then we can make the change (second line).

ps -C sleep
sudo renice -n -10 -p n

Symbol n (at the end of the second line) we change for the PID, that we have received from the first line (running processes).

Terminating processes

Process can be terminated in Linux either after its execution or if we terminate it manually. Manual termination may be inevitable, if the process is not responding (does not react to the user input) or uses too many of the system resources (CPU or RAM).

Whether the process uses system resources in the long term and thus preventing the stable run of the OS can be checked with the program top:

top

Program top, similarly to program ps, will list the processes but also updates the list, therefore it provides constant overview. Processes are listed by the parameter on how much they burden the system (column %CPU). If we add capital M (Shift + M), the processes will be listed on how much they burden the RAM (column %MEM). Back to the overview of CPU load enter capital P (Shift + P).

Process that according to the program top doesn’t respond or burden the system OS inadequately has to be terminated manually. Interactive process created by the user on the foreground can be terminated with a Ctrl + C key shortcut. The background processes has to be ended with a program kill.

Manual termination of the process shall be done only after careful consideration of the possible consequences.

From the list of the program “top” we can receive the PID of the process that does not respond or burden the OS and can be terminated with the program kill. By this program we send to the process different signals based on which the task will be executed. Basic use is:

kill n

Symbol n shall be replaced with PID that we have acquired from the list of running processes via program top.

With that we send to a process a signal number 15 (SIGTERM), by which we request the soft termination of the process. That means that we give the process time to let other connected processes (parent – child) know, that this process will be terminated and that they should correctly end they work with the files.

If the process would not let other processes know, it could happened that other processes he run would be left behind and wouldn’t work correctly. These would be orphan processes, when their parent was terminated, but the child process is still running without its parent.

To prevent unwanted processes-orphans, it is good to use program killall. This program ends whole family of the processes by the process name (column COMMAND in program top), not by PID. If we would run the program sleep 1000 & three times and we would like to terminate all three instances, we would use the command:

Program killall or pkill terminates all instances of the program (in our case program sleep). Manual termination with the program killall or pkil shall be done only after careful consideration of the possible consequences.

killall sleep

Program pkill will terminate all processes, that start with “sleep”:

pkill sleep

It could happen that even after we send the preset signal (15 – SIGTERM) via program kill, killall or pkill the process is not terminated. In that case we send the signal number 9 (SIGKILL), by which we request the hard termination of the process. By this option we terminate the process without letting other processes (parent – child) know about the termination and also without letting them stop the work with the files properly. This means that orphan processes may be created.

Hard termination of the process shall be done only after careful consideration of the possible consequences.

Command for soft termination kill n/killall sleep/pkill sleep didn’t work? The signal for hard termination will:

kill -9 n
killall -9 sleep
pkill -9 sleep

Symbol n shall be replaced with PID that we have acquired from the list of running processes via program top.

If you need to end a process of a different use, you have to use programsudo:

sudo kill n
sudo killall sleep
sudo pkill sleep

Even while terminating processes of a different user, we firstly send a signal number 15 (SIGTERM) for soft termination. Only in inevitable cases we send signal number 9 (SIGKILL) for hard termination.

Summary

We have defined the term process in Linux, its types and statuses. On specific examples we have learned how to run, set priority or terminate processes. If it is the case that can affect the whole run of OS Linux, it is necessary to proceed with the changes only after careful consideration of the possible consequences.

Updated on December 18, 2024

Was this article helpful?

Related Articles