Controlling Processes from the Linux Command Line
Controlling processes is a fundamental skill for Linux users and administrators, allowing them to manage system resources effectively, enhance system performance, and maintain system security. A “process” in Linux is a running instance of a program, and controlling it refers to starting, stopping, pausing, and managing these processes, either in the foreground or in the background. Efficient process control is crucial to optimize the performance of the Linux system, especially in multi-user or high-demand environments.
Starting a Process
Initiating a process in Linux is as simple as running a command or a program.
For example, gedit
can be started from the command line by typing the
following command and pressing Enter:
## Starts gedit in the forground
$ gedit
## Adds gedit in the background
$ gedit &
When started this way, gedit
runs in the foreground, blocking the terminal
from which it was started for further input until the process is stopped or sent
to the background.
Understanding Background Processes
When a process is started, it’s typically run in the foreground, occupying the terminal until it completes. While a process is running in the foreground, the terminal is blocked, and you can’t use it to run other commands until the process completes.
In contrast, a background process runs without occupying the terminal, allowing the user to continue running other commands and managing other tasks from the same terminal. This enables multitasking and allows for more efficient use of system resources.
Checking if a Process is Running in Foreground or Background
To determine whether a process is running in the foreground or the background,
you can examine the process status using the jobs
command:
$ jobs
This command lists all jobs started in the current shell, showing their status ( running, stopped, or done) and whether they are running in the foreground or the background.
For example, if gedit
is running in the background, executing jobs
will show
something like:
[1]+ Running gedit &
Here, 1
is the job ID, and gedit
is the process running in the background.
The +
sign indicates that this is the current (or most recently used) job,
and &
denotes that the process is running in the background.
Bring Job to Foreground
To bring a background process to the foreground, use the fg
command followed
by the job ID:
$ fg 1
After executing this command, the gedit
process, in this case, will be moved
to the foreground, allowing you to interact with it directly from the terminal.
Sending a Process to the Background
To send a foreground process like gedit
to the background, first, suspend it
by pressing Ctrl + Z
, then use the bg
command:
$ bg %1
Managing Processes: Stop, Pause, and Terminate
Managing processes efficiently is a crucial aspect of Linux administration. There are different commands and signals available to control the execution states of a process, such as stopping, pausing, or terminating it.
Starting a Process
For illustration, let’s start a gedit
process:
$ gedit
Identifying Process Information
You can identify the Process ID (PID) of the gedit
process by using the ps
command:
$ ps aux | grep gedit
Process States
A process in Linux can exist in several states including Running, Stopped, or Terminated. Here's a simplified table to understand these states:
State | Description |
---|---|
Running | The process is either running or it is ready to run. |
Stopped | The process is stopped and can be restarted by the user. |
Terminated | The process has been terminated, either by user command or by the system. |
Managing Process States
1. Stopping a Process
To stop a process means to put it in a state where it is paused and can be
resumed later. You can stop the gedit
process by sending a SIGSTOP
signal
using the kill
command:
$ kill -SIGSTOP [PID]
Replace [PID]
with the actual PID of the gedit
process.
2. Continuing a Stopped Process
You can continue a stopped process using the SIGCONT
signal:
$ kill -SIGCONT [PID]
3. Terminating a Process
To terminate a process means to kill it. You can terminate the gedit
process
using the SIGTERM
signal:
$ kill -SIGTERM [PID]
Monitoring Processes with top
and htop
For real-time process monitoring, top
and htop
are invaluable tools,
providing detailed, dynamic views of system activity and process status:
$ top
$ htop
Conclusion
Controlling and managing processes are crucial skills when working with Linux.
Whether you're using gedit
or any other program, understanding how to start,
stop, pause, resume, and monitor processes from the command line is essential
for effective system administration and usage.
Remember to replace [PID]
with the actual Process ID of the gedit
instance,
and [nice_value]
with the desired nice value when experimenting with these
commands. Always exercise caution, particularly with the kill
command, to
avoid inadvertently terminating essential system processes.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.