- Published on
Process Management in Linux
- Authors
- Name
- Yisak Abraham
- @YisakAbrahamK
Introduction
A process is an instance of a running program. It's a fundamental concept in the Linux operating system, and understanding how processes work is essential for system administrators and developers. In this post, we'll cover the basics of process management in Linux, including how to view, manage, and monitor processes.
- Processes are the running instances of programs in Linux.
- Each process has a unique process ID (PID) that identifies it.
- Process are managed by the kernel and can be in different states like running, sleeping, stopped, or zombie.
- Running: The process is currently running on the CPU.
- Sleeping: The process is waiting for an event to occur.
- Stopped: The process has been stopped, usually by a signal.
- Zombie: The process has completed execution but still has an entry in the process table.
Type of Processes
There are two types of processes in Linux:
1. Foreground Processes
These are processes that run in the foreground and interact with the user.
They receive input from the user and display output on the screen.
The shell waits for the foreground process to complete before accepting new commands.
To run a process in the foreground, you can simply execute the command without any special characters.
zsh$ ls
2. Background Processes
These are processes that run in the background without interacting with the user.
They do not require user input and do not display output on the screen.
The shell does not wait for background processes to complete and allows the user to continue entering commands.
To run a process in the background, you can append an ampersand
&
to the end of the command.zsh$ sleep 10 &
ps
Command
Listing Processes using The ps
command is used to list processes running on the system. It provides detailed information about each process, including the process ID (PID), parent process ID (PPID), CPU utilization, start time, terminal, CPU time, and command.
Here is a table for the columns and their description for the ps
command:
Column | Description | Full Description |
---|---|---|
UID | User ID | User ID is the unique number assigned to a user. |
PID | Process ID | Process ID is a unique number assigned to a process. |
PPID | Parent Process ID | Parent Process ID is the ID of the parent process. |
C | CPU utilization | CPU utilization is the percentage of CPU time used by the process. |
STIME | Start time | Start time is the time when the process started. |
TTY | Terminal | Terminal is the terminal where the process is running. |
TIME | CPU time | CPU time is the total CPU time used by the process. |
CMD | Command | Command is the name of the command that started the process. |
ps
command:
Common options for the ps
: Display information about processes running in the current shell.ps -e
: Display information about all processes on the system.ps -f
: Display a full listing of processes with additional details.ps -a
: Display information about all processes except session leaders.ps -x
: Display information about processes without controlling terminals.ps -l
: Display a long listing of processes with additional details.ps -u
: Display processes for a specific user.zsh$ ps -u username
ps -p PID
: Display information about a specific process identified by its PID.zsh$ ps -p 1234
Managing Processes
The Linux operating system provides several commands to manage processes, including starting, stopping, pausing, and monitoring processes. Here are some common commands used to manage processes in Linux.
Command | Description |
---|---|
ps | Display information about processes. |
top | Display real-time information about processes. |
kill | Send a signal to a process to terminate it. |
pstree | Display a tree of processes. |
pgrep | List processes based on name or other attributes. |
killall | Send a signal to all processes with a specific name. |
systemctl | Control the systemd system and service manager. |
jobs | List background jobs. |
bg | Move a job to the background. |
top
command
monitoring processes using The top
command is used to display real-time information about processes running on the system. It provides a dynamic view of the system's CPU, memory, and process activity.
Command | Description |
---|---|
top | Display real-time information about processes. |
iotop | Display I/O usage by processes. |
uptime | Display system uptime and load averages. |
iostat | Display CPU and I/O statistics. |
Stopping processes
Ending a process can be done in several different ways. From a console-based command, sending a CTRL + C keystroke (the default interrupt character) will exit the command. This works when the process is running in the foreground mode.
The kill
command is used to send a signal to a process to terminate it. The default signal sent by the kill
command is SIGTERM
, which allows the process to perform cleanup operations before exiting.
To kill a process, you need to know its process ID (PID). You can find the PID of a process using the ps
command.
$ ps -e | grep process_name
Once you have the PID, you can use the kill
command to terminate the process.
$ kill PID
Sending signals to processes
In Linux processes, system admins can send signals to communicate with processes and request specific actions.
Signals are software interrupts delivered to a process by the operating system or another process.
- Each signal has a default action associated with it, which determines what the process does when it receives that signal.
- Common default actions include termination, stopping, or ignoring the signal.
- Signals allow processes to respond to various events, such as the termination of another process, user input, or changes in system conditions.
- Signals are identified by unique numbers, known as signal numbers. Each signal number corresponds to a specific event or action.
Linux systems have a set of standard signals defined, each with its own signal number. Here are some signals and their numbers:
SIGTERM
(15): Termination signal. This is the default signal sent by thekill
command.SIGKILL
(9): Kill signal. This signal immediately terminates the process.SIGSTOP
(19): Stop signal. This signal stops the process.SIGCONT
(18): Continue signal. This signal resumes a stopped process.SIGHUP
(1): Hangup signal. This signal is typically used to reload configuration files or restart daemons.
Conclusion
Process management is a critical aspect of system administration in Linux. Understanding how processes work, how to monitor them, and how to manage them is essential for maintaining system performance and stability. By using the commands and techniques like covered in this post, system administrators can effectively manage processes and troubleshoot issues that may arise during system operation.