Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Process Management Commands in Linux

Introduction

In Linux, process management is an essential skill for system administrators and users alike. Processes are instances of running programs, and managing these processes effectively is crucial for maintaining a stable and efficient system. This tutorial covers various commands used to manage processes in Linux, providing examples and detailed explanations.

1. Viewing Processes

The ps command is used to view the currently running processes. It provides information such as the process ID (PID), terminal associated with the process, CPU time used, and the command that started the process.

ps
PID TTY TIME CMD
723 pts/0 00:00:00 bash
746 pts/0 00:00:00 ps

The ps command with the -aux option provides a more detailed list of all processes running on the system.

ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 22512 4100 ? Ss 09:00 0:01 /sbin/init
root 723 0.0 0.0 15900 2100 pts/0 Ss 09:05 0:00 bash
root 746 0.0 0.0 36648 2880 pts/0 R+ 09:06 0:00 ps -aux

2. Killing Processes

The kill command is used to terminate processes. You need the PID of the process you want to terminate. The kill command sends a signal to the process, with the default signal being SIGTERM (15), which requests a graceful termination.

kill 723

If a process does not terminate with SIGTERM, you can use the SIGKILL (9) signal to forcefully terminate it.

kill -9 723

3. Background and Foreground Processes

Processes can be run in the background or foreground. A foreground process occupies the terminal until it completes, while a background process releases the terminal for further use. To run a process in the background, append an ampersand & to the command.

sleep 100 &
[1] 789

To bring a background process to the foreground, use the fg command followed by the job number.

fg %1

4. Monitoring Processes

The top command provides a dynamic, real-time view of running processes. It displays system summary information as well as a list of tasks currently managed by the kernel.

top
top - 09:10:10 up 1:10, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 91 total, 1 running, 90 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.2 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1016116 total, 213560 free, 392096 used, 410460 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 490756 avail Mem

To exit the top command, press q.

5. Changing Process Priority

The nice command is used to start a process with a specified priority, and the renice command is used to change the priority of an existing process. The priority range is from -20 (highest priority) to 19 (lowest priority).

nice -n 10 sleep 100

To change the priority of an existing process, use the renice command followed by the priority and PID.

renice 5 723

6. Viewing Process Tree

The pstree command displays the processes in a tree format, showing the parent-child relationship between processes.

pstree
init-+-bash---pstree
`-nginx---nginx

Conclusion

Process management is a critical skill for anyone using Linux. With the commands covered in this tutorial, you should be able to view, manage, and manipulate processes effectively. Remember that understanding how to handle processes can greatly enhance your ability to maintain a stable and efficient system.