LPIC-1 Exam Objective 103.5: Create, Monitor, and Kill Processes #
Overview #
In this tutorial, we’ll cover the essentials of process management in Linux. We’ll explore how to create, monitor, and kill processes, using real-world examples to distinguish between Ubuntu/Debian-based systems and Enterprise Linux (like CentOS and Red Hat).
Key Knowledge Areas: #
- Run jobs in the foreground and background.
- Signal a program to continue running after logout.
- Monitor active processes.
- Select and sort processes for display.
- Send signals to processes.
Relevant Commands and Utilities: #
&
,bg
,fg
,jobs
kill
,nohup
ps
,top
,free
,uptime
pgrep
,pkill
,killall
watch
,screen
,tmux
1. Running Jobs in the Foreground and Background #
Foreground Jobs #
By default, when you run a command in the terminal, it runs in the foreground. For example:
$ sleep 60
This command will run for 60 seconds and during this time, the terminal will be occupied with this process.
Background Jobs #
To run a process in the background, append an ampersand (&
) to the command:
$ sleep 60 &
[1] 1234
The [1]
is the job number, and 1234
is the process ID (PID). You can continue using the terminal while the command runs in the background.
Bringing Jobs to Foreground and Background #
To list current jobs:
$ jobs [1]+ Running sleep 60 &
To bring a job to the foreground:
$ fg %1
To send a foreground job to the background, first suspend it using
Ctrl+Z
, then use:$ bg %1
2. Signaling a Program to Continue Running After Logout #
The nohup
(no hangup) command allows a process to continue running even after the user has logged out.
Using nohup
#
$ nohup sleep 60 &
Output is redirected to nohup.out
by default.
3. Monitoring Active Processes #
Using ps
#
The ps
command is used to display information about active processes.
To display processes for the current shell:
$ ps PID TTY TIME CMD 1234 pts/0 00:00:00 bash 5678 pts/0 00:00:00 ps
To display all processes:
$ ps aux
To filter processes using
grep
:$ ps aux | grep sleep
Using top
#
The top
command provides a dynamic real-time view of running processes.
$ top
Press q
to quit.
Using htop
#
htop
is an interactive process viewer (not installed by default on some systems).
To install on Ubuntu/Debian:
$ sudo apt-get install htop
To install on Enterprise Linux:
$ sudo yum install htop
Run it using:
$ htop
Memory and System Load #
Check memory usage:
$ free -h
Check system uptime and load:
$ uptime
4. Selecting and Sorting Processes for Display #
Using ps
with Sorting Options
#
Sort by memory usage:
$ ps aux --sort=-%mem
Sort by CPU usage:
$ ps aux --sort=-%cpu
5. Sending Signals to Processes #
Using kill
#
The kill
command sends signals to processes. The default signal is TERM
(terminate).
To send a
TERM
signal:$ kill 1234
To send a
KILL
signal (force kill):$ kill -9 1234
Using pkill
and pgrep
#
pgrep
finds processes by name:$ pgrep sleep 1234
pkill
kills processes by name:$ pkill sleep
Using killall
#
The killall
command kills all instances of a process by name:
$ killall sleep
6. Watching Commands #
The watch
command runs a program periodically, displaying the output in a full-screen interface.
$ watch -n 1 'ps aux --sort=-%mem | head -10'
This runs the command every second and displays the top 10 memory-consuming processes.
7. Terminal Multiplexers: screen
and tmux
#
Using screen
#
screen
allows you to create and manage multiple terminal sessions.
Start a new session:
$ screen
Detach from the session with
Ctrl+A
followed byD
.List sessions:
$ screen -ls
Reattach to a session:
$ screen -r [session_id]
Using tmux
#
tmux
is a terminal multiplexer similar to screen
.
Start a new session:
$ tmux
Detach from the session with
Ctrl+B
followed byD
.List sessions:
$ tmux ls
Reattach to a session:
$ tmux attach-session -t [session_id]
Conclusion #
Process management is a fundamental skill for any Linux administrator. By understanding how to create, monitor, and manage processes effectively, you can ensure your systems run smoothly and efficiently. This tutorial provides a solid foundation, but be sure to practice these commands to become proficient.