Linux Commands

Commands for Process Management in Linux

Linux is a highly capable and adaptable operating system that has use in different domains. One of the key features of Linux is its command-line interface, which allows users to interact with the system directly and perform a wide range of tasks.

One of the most important of these tasks is process management, which involves controlling the various programs and services running on the system. This article covers commands for process management in Linux and provides detailed explanations of how to use them.

Content for this Article:

What is a Process in Linux?

A process in simple words is the program running on a computer system. In simple words, whenever we send a command to the Linux system, it initiates a new process. A Linux process is a program which is currently under execution.

More than one process can also be initiated for a single program such as multiple windows or terminal.

Types of Processes in Linux

We can divide the Linux processes in two categories:

Foreground Processes: These processes are real time and run on the system screen. They required real time input from the user. These processes are also known as interactive processes. For example, Office Programs

Foreground processes can be started using the GUI or terminal. If we start a certain foreground process from the terminal, then we have to wait for the terminal until the process begins.

Background Processes: These processes run in background and do not need user interference or input. These processes are also known as non-interactive processes. For example, Antivirus.

When a background process starts using the terminal the terminal is bound to that process and we cannot process any other command unless the process is stopped.

There are certain processes that take hours to complete. To avoid situations like these we  can send a program to the background.

Now we will look at how we can manage all processes in Linux using different commands.

Different Commands for Process Management in Linux

Process management refers to the ability to start, stop, and manage the various processes running on a Linux system. Let’s discuss different commands that help to manage processes in Linux.

Linux Command to Send a Process in Background (bg)

The bg command sends a suspended or stopped process to run in the background. This command will free the terminal which means the terminal is available for other input while the process continues in the background.

Now we started a process by running VLC player using:

vlc

We can see the VLC player started in the new window but now the terminal is bound and doesn’t allow us to process other commands unless we stop this process.

Now press Ctrl + Z to stop this process. After pressing run following command to confirm the status:

jobs -l

Now using the bg command we will send this process in the background and after that it will start running.

To send VLC in background run:

bg vlc

Now using the jobs command again list the process to confirm vlc running in background.

Linux Command to List Running Processes (top)

The top command displays real-time information about running processes, such as resource usage and CPU time. The top command displays the processes in order of their resource usage.

To track the running processes run:

top

Following result displays the list of processes that are running on the system. Press ‘Q’ to exit the top command details menu.

Below is the detail of all given fields:

PID: Every process is assigned a unique identifier called the PID.

User: Process owner username (system name).

PR: This indicates the priority given to a process during scheduling.

NI: This field displays a nice value.

VIRT: Virtual memory used by a certain process.

RES: Physical memory used by a certain process.

SHR: Shared memory with other processes.

S: This field displays the state of the process, which can be

  • ‘D’ = uninterruptible sleep
  • ‘R’ = running
  • ‘S’ = sleeping
  • ‘T’ = traced or stopped
  • ‘Z’ = zombie

%CPU: CPU percentage used by a certain process.

%MEM: It gives us the percentage of RAM a process is utilizing.

TIME+: This gives information about the total CPU time used by a process.

Command: Command used to activate the process.

Following table gives an example from the above result shown in image:

Field Example
PID 2443
User Kash
PR 20
NI 0
VIRT 568324
RES 58372
SHR 43172
S R
%CPU 25.0
%MEM 0.7
TIME+ 0:58.43
Command gnome-terminal-

Linux Command to Display Process Status (ps)

The ps command in Linux stands for “Process Status” and is used to display information about the running processes. It provides us with the current state of the system’s processes. Unlike the top command, the information displayed by ps is not updated in real-time.

By default, it shows the processes associated with the current terminal session. The most used options for the ps command are:

: Displays information about all processes in the system.

: Displays information about processes owned by a specific user.

: Displays a full-format listing of processes with additional details.

: Displays a user-oriented view of active processes with additional details.

: Displays information about all processes except those associated with terminals.

: Displays information about all processes on the system, including those not associated with terminals.

Run the ps command to get the information of current running process:

ps

The terminology is as follows:

PID (Process ID): A unique numerical identifier given by system to a process.

TTY (Terminal Type): The type of terminal or console associated with the process.

TIME (Total Time): The amount of time, typically measured in CPU seconds, that the process has been running since it started.

CMD (Command): The name of the command or executable that starts a process.

Use below command to get more info about a system processes:

ps -u

Here:

%CPU It shows computing power for a certain process.

%MEM It shows memory taken by a process.

STAT State of system process.

To list all processes including which are not associated with terminal run:

ps -A

Above command list all processes including the one that are not running.

Run below command to check process run by a user:

ps -ux

To check a state of single process using its PID (Process ID) use the syntax:

ps [pid]

For example, to check the process state of VLC run:

ps 13786

Here first we have attained the process ID by listing all jobs first.

For further information on ps command run:

man ps

Following window will open showing us all examples related to the ps command.

Linux Command to Kill a Process (kill)

The kill command stops or kills a process. This command sends a signal to the specified process, causing it to stop executing and exit.

By default, the kill command sends a SIGTERM [-15] signal, which completely stops and cleans the process before exiting. However, it is also possible to send a SIGKILL[-9] signal, which immediately terminates the process without allowing it to clean up.

Multiple signals are there to use with the kill command. You can list all the signals using:

kill -L

If we send a kill command without any number, it will send the SIGTERM signal [15].

For killing a certain process with the help of its process id [pid] use:

kill [pid]

Or we can also send:

kill -9 [pid]

A [SIGKILL-9] signal will be sent to process by above command.

To know the process ID we use following command:

pidof [process name]

For example, killing a process having PID [3000]:

kill -9 3000

killall: This command is used to kill all instances of a particular process.

killall vlc

Linux Command to Set Process Priority (nice)

The nice command sets a process priority. If a process has more priority, it means the system will give more CPU time to that process.

By default, all processes run at the same priority level, but the nice command can be used to increase or decrease the priority of a particular process. A higher priority value means that the process will be given more CPU time, while a lower priority value means that it will be given less CPU time.

In Linux, running too many processes can slow down the performance of high-priority processes. To prioritize processes according to needs, we can adjust their “niceness” value. Niceness values range from -20 to 19 and lower the values means more priority will be given to that process. By default, all processes have a niceness value of 0.

To change a process’s niceness value run below command:

nice -n [nice value] [process name]

First, we will check the nice value of VLC player using top command:

top

Here we can see the NI value for VLC is 0.

Now change VLC NI value to 10 using below command:

nice -n 10 vlc

Now run the top command again to check the new NI value for VLC player:

top

We have successfully modified the priority level of the VLC player using a nice command.

Linux Command to Change Priority of a Process(renice)

The renice command is used to adjust the priority of a process that is already running in the background without having to stop and restart it. The syntax for the renice command is similar to that of the nice command, and it uses the same priority values.

To change already running process nice value use:

renice [nice value] -p [pid]

To change the nice value for process ID [4898] use:

renice 15 -p 4898

Linux Command to Check Free Disk Space (df)

The df command is used to check free disk space available. It displays information about the total size of the file system and total space used. It also gives space available and used space percentage.

df

Run below command to get above information in more simplified way:

df -h

Linux Command to Check Memory Usage (Free)

In Linux free command is used to check the memory usage and free space available on a system.

free

Following two arguments can display information in different format:

For information in MB use:

free -m

For information in GB use:

free -g

Summary

Command Description
bg Send a process to background
top Active processes details
ps Details of process running under user
ps PID Status of a particular process
pidof Give process ID
kill PID Kills process
nice Set process starting priority
renice Reconfigure the already running process priority value by changing nice value
df Gives free disk space
free Gives free RAM of system

Conclusion

Managing processes and system resources is a critical task in Linux administration. The bg, top, and ps commands are useful for controlling processes and viewing their status. The nice command can help you prioritize processes, and the kill command is used for terminating them. Additionally, the df and free commands are valuable for checking free hard disk space and RAM on your system. Read the article for detail on each command.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.