How to Use Linux Commands for Process Management

PRODUCTS THAT MIGHT INTEREST YOU:

Benefit from the best server plans and related services, competitive prices, coupled with personalized attention to each client. Supported by top-notch technical assistance that remains consistently accessible to address all your inquiries.

An Overview of Process Types in Linux

In the Linux operating system, processes can be classified into two types:

  • Foreground processes are a type of process that rely on user input. They are commonly known as interactive processes.
  • Background processes are a type of process that operate independently of the user. They are commonly known as non-interactive or automatic processes.

The following is an overview of the process states in Linux:

In the Linux operating system, a process can transition through various states from its creation to its termination. The following states are:

The following activities are being performed: 

1. Running 

2. Sleeping

  • Interruptible sleep refers to a state in which a process is waiting for an event to occur, such as input from a user or data from a file. 
  • An uninterruptible sleep is a state in which a process is temporarily halted and cannot be interrupted by any external events or signals. 
  • During this sleep state, the process can be stopped.
  • The process has been terminated and is no longer running. It was identified as a zombie process.
  • The term “running state” refers to a process that is currently executing or is prepared to execute.
  • The process enters a sleeping state when it is waiting for a resource to become available.
  • The behavior of a process in Interruptible sleep is to wake up and handle signals, while a process in Uninterruptible sleep does not wake up to handle signals.
  • The stopped state of a process is triggered when it receives a stop signal.
  • The term “zombie state” refers to a situation in which a process has terminated, but its entry remains in the process table.

Process Scheduling

The Linux kernel schedules processes based on their priority and niceness values. Higher-priority processes get more CPU time, while lower-priority ones may wait longer. Understanding process scheduling can help you optimize system performance by adjusting these values accordingly.

Commands for Process Management in Linux

In Linux, there are two commands that can be used to monitor and track running processes. The two commands being referred to are Top and Ps.

1. The top Command for Managing Linux Processes

The top command can be used to monitor the running processes on your machine.

top

The top command is used to display a real-time list of running processes, including information about their memory and CPU usage. In order to gain a better understanding of the output, let us delve into it further:

  • The Process ID (PID) is a unique identifier assigned to each process.
  • Process Owner Username: The username of the process owner.
  • PR: The term “PR” refers to the priority given to a process during the scheduling process.
  • The NI, or “nice” value, refers to the priority level of a process.
  • The term “VIRT” refers to the amount of virtual memory that is being utilized by a specific process.
  • The amount of physical memory used by a process is referred to as the Resident Set Size (RSS).
  • The SHR field represents the amount of memory that is shared with other processes.
  • The “S” refers to the state of the process.
‘D’ =uninterruptible sleep
‘R’ =running
‘S’ = sleeping
‘T’ =traced or stopped
‘Z’ =zombie
  • The %CPU metric represents the percentage of CPU utilization by a specific process.
  • The “MEM” metric represents the percentage of RAM utilized by the process.
  • The “TIME+” metric represents the total amount of CPU time that has been consumed by the process.
  • The command is used to activate the process.

The up/down arrow keys can be utilized to navigate through the list in an upward or downward direction. To exit the program, press the letter ‘q’. In order to terminate a process, the user should utilize the up and down arrow keys to select the desired process and then press the ‘k’ key.

Another option is to utilize the kill command, which will be discussed in more detail later.

More Process Management Commands

To enhance your process management toolkit in Linux, there are additional commands you can use:

  • htop: A more visually appealing alternative to top, offering an interactive interface that makes it easier to monitor processes.
  • pgrep: Used to search for processes by name, returning their process IDs.
  • pkill: Allows you to send signals to processes based on their name, making it easier to terminate processes without needing to know their exact PID.

2. ps command

The ‘ps’ command is an abbreviation for ‘Process Status’. The “ps” command is a utility in Unix-like operating systems that displays information about active processes running on the system. However, the output generated by this command is not in real-time, unlike the top command.

ps

The following terminology is used:

PIDprocess ID
TTYterminal type
TIMEtotal time the process has been running
CMDname of the command that launches the process

In order to obtain additional information using the ps command, follow these steps:

ps -u

The following information is provided: 

  • %CPU: This metric represents the amount of computing power that the process is currently utilizing. 
  • %MEM: This metric represents the amount of memory that the process is currently occupying.
  • The term “STAT” is an abbreviation for “process state”.

The ps command is primarily used to display the processes that are currently running. However, it can also be utilized to list all the processes.

ps -A

The following command provides a comprehensive list of processes, including those that are currently not running.

Handling Background Processes

In Linux, you can run processes in the background to keep your terminal free for other tasks. Here’s how:

  • Running a process in the background: Append ‘&’ to your command.

    command &
  • Sending a process from foreground (fg) to background (bg): If a process is already running in the foreground and you want to send it to the background, first stop the process by pressing Ctrl + Z, which pauses the process. Then, use the bg command to resume it in the background.

    Ctrl + Z  # stop the process
bg        # resume the process in the background
  • Managing background processes: Use jobs to list active background processes, fg to bring a background process to the foreground, and bg to resume a stopped job in the background.

3. Stop a process

The process in Linux can be terminated by utilizing the ‘kill’ command. The “kill” command is used to send a signal to a process.

Various types of signals can be transmitted. The most commonly used command for terminating a process is ‘kill -9’, which corresponds to the signal ‘SIGKILL’.

To obtain a comprehensive list of signals, the following command can be used:

kill -L

The default signal is 15, also known as SIGTERM. The kill command, when used without specifying a number, sends the SIGTERM signal.

The syntax for terminating a process is as follows:

kill [pid]

Another option is to utilize:

kill -9 [pid]

The execution of this command will result in the transmission of a ‘SIGKILL’ signal to the designated process. The use of this method is recommended when the process fails to respond to a standard kill request.

Detailed Explanation of Signals

In Linux, signals are used to communicate with processes, instructing them to perform certain actions. Here are some commonly used signals:

  • SIGTERM (Signal 15): Politely asks a process to terminate, allowing it to clean up resources.
  • SIGKILL (Signal 9): Forces a process to terminate immediately without cleanup.
  • SIGHUP (Signal 1): Typically used to reload a process’s configuration without stopping it.

You can send a signal using the kill command followed by the signal number or name:

kill -s SIGTERM [pid]

4. Adjust the process’s priority

Within the Linux operating system, it is possible to allocate priority levels to different processes. The term used to refer to the priority value of a process is known as the “Niceness” value. The niceness value is a numerical range that spans from -20 to 19. The default value is 0.

The niceness value is represented in the fourth column of the output generated by the top command.

In order to initiate a process and assign it a custom value instead of the default value, utilize the following command:

nice -n [value] [process name]

In order to modify the nice value of a currently running process, utilize the following command:

renice [value] -p 'PID'

Security Considerations

Managing processes securely is crucial to maintaining system stability and security. Consider implementing the following:

  • Limiting user permissions: Restrict access to critical processes by setting appropriate user privileges.
  • Using cgroups: Control groups (cgroups) allow you to allocate resources (CPU, memory, etc.) to specific groups of processes, ensuring they do not consume excessive resources.

To sum up

In this tutorial, we’ve explored the essential aspects of process management in Linux. We covered the different types of processes—foreground and background—and discussed the various states a process can go through during its lifecycle. Key commands like top, ps, kill, and nice were introduced to help you monitor, manage, and prioritize processes effectively.

Understanding these fundamental tools and concepts is crucial for anyone managing Linux systems, as they provide the means to optimize system performance, troubleshoot issues, and ensure smooth operation. Additionally, by delving into advanced process management commands, signals, background processes, and security considerations, you can gain a deeper understanding of how to maintain a stable and secure Linux environment.

Whether you’re a beginner or looking to expand your knowledge, mastering process management is a vital skill for effective system administration. As you continue to explore Linux, remember to regularly monitor your processes, adjust priorities as needed, and leverage the powerful tools available to you.

HAVE A QUESTION OR WANT TO GET A CUSTOM SOLUTION?

CONTACT SALES

FAQs

A process in Linux refers to a program that is currently being executed. It can be in various states such as running, sleeping, stopped, or zombie, depending on its current activity.

Linux processes are categorized into two types: foreground processes, which require user interaction, and background processes, which run independently of user input.

The main process states in Linux include running, interruptible sleep, uninterruptible sleep, stopped, and zombie. Each state represents the process’s current status or activity.

You can monitor running processes using the top command, which displays real-time information about processes, including CPU and memory usage. Alternatively, the ps command provides a snapshot of current processes.

The top command provides a dynamic, real-time view of running processes, while the ps command gives a static snapshot of processes at the time the command is executed.

You can stop a process using the kill command followed by the process ID (PID). The command kill -9 [pid]sends a SIGKILL signal, forcibly terminating the process.

A zombie process is one that has completed execution but still has an entry in the process table. This occurs because the process’s parent has not yet read its exit status.

You can change a process’s priority using the renice command, which adjusts its “niceness” value. A lower niceness value gives the process higher priority.

htop is an interactive process viewer that provides a more user-friendly interface compared to top, with features like easy navigation and process management.

You can run a process in the background by appending & to the command. Use jobs to list background processes, fgto bring a process to the foreground, and bg to resume a stopped job in the background.

Signals are used to communicate with processes in Linux, instructing them to perform specific actions such as terminating (SIGTERM) or reloading configuration (SIGHUP).

It is important to limit user permissions and use control groups (cgroups) to restrict resource usage by processes, ensuring secure and stable system operation.