In Linux, a process is an instance of a running program. Every process has a unique process ID (PID), and processes can spawn child processes. The Linux operating system uses processes to manage system resources and run user applications. Understanding processes and how to manage them is an essential skill for any Linux user.
The ps
command is one of the most important tools for managing processes in Linux. The ps
command lists the currently running processes on your system. By default, ps
only shows you the processes running in your current terminal session. To see all running processes, including those running in other terminal sessions, you can use the -A
or -e
option:
ps -A
ps -e
The ps
command outputs a table with information about each process. The most important columns are the PID (process ID), the TTY (terminal), the STAT (status), the TIME (CPU time), and the CMD (command). Here’s a brief overview of what each column means:
- PID: The process ID.
- TTY: The terminal where the process was started. If the process is not associated with a terminal, the TTY will be “?”
- STAT: The current status of the process. Common statuses include “R” (running), “S” (sleeping), “Z” (zombie), and “D” (disk sleep).
- TIME: The amount of CPU time used by the process.
- CMD: The command used to start the process.
You can also use various options with ps
to customize the output. For example, the -f
option shows a full-format listing of processes, including additional information about each process:
ps -ef
The -l
option shows a long listing of processes, including additional columns for the user ID (UID), the CPU usage percentage (C), the memory usage (SZ), and the start time (STIME):
ps -l
Another useful option is -u
, which shows processes owned by a specific user:
ps -u username
Once you have identified a process you want to manage, you can use the kill
command to stop it. The kill
command sends a signal to a process, asking it to terminate. By default, kill
sends the TERM signal, which asks the process to terminate gracefully. If a process does not respond to the TERM signal, you can use the KILL signal to force the process to terminate immediately. Here’s an example of how to use kill
to terminate a process with a specific PID:
kill PID
You can also use various signal options with kill
to customize the behavior. For example, the -HUP
option sends the HUP signal, which tells the process to reread its configuration files:
kill -HUP PID
The -STOP
option sends the STOP signal, which pauses the process:
kill -STOP PID
In addition to processes, Linux also has a concept of jobs. A job is a process that is running in the background of your terminal session. To start a job in the background, you can add an ampersand (&
) to the end of the command. For example:
command &
To list all jobs running in your current terminal session, you can use the jobs
command:
jobs
The jobs
command shows you a list of all background jobs running in your current terminal session, along with their job number.
To bring a background job to the foreground of your terminal session, you can use the fg
command followed by the job number. For example:
fg %1
This would bring job number 1 to the foreground.
To terminate a job running in the background, you can use the kill
command with the job number. For example:
kill %1
This would send the TERM
signal to job number 1, asking it to terminate gracefully. If a job does not respond to the TERM
signal, you can use the KILL
signal to force the job to terminate immediately. Here’s an example of how to use kill
to terminate a job with a specific job number:
kill -9 %1
This would send the KILL
signal to job number 1, forcing it to terminate
Finally, the top
command is another useful tool for managing processes in Linux. The top
command shows a real-time view of the system’s processes and resource usage. The top
command outputs a table with information about each process, similar to the ps
command. However, unlike ps
, top
updates the table in real-time, allowing you to monitor changes as they happen.
top