Process Management in Linux: A Comprehensive Guide

g158b0e92908079443c8882081d76bed141873d0ea39b4a3a99778a16aba52e93e4db5cb1471d46bc74dc92cf192913c5aa5afebc36667b1260414db792aae83b_1280-4259595.jpg

Process management is a fundamental concept in Linux that involves managing the various (or “processes”) that are running on a system. Processes can be programs, commands, or tasks running in the background or foreground. This blog will walk you through the essential commands for managing processes, viewing their status, and controlling them.

Viewing Running Processes

The first step in process management is understanding how to view the processes that are currently running on your system. There are several commands that can help you do this:

ps (Process Status)

The ps command shows information about active processes on your system. A common usage of ps is to combine it with options to get a more detailed view of the processes.

ps – shows processes for the current user in the current terminal session

ps aux – lists all running processes along with detailed information such as the user, CPU usage, memory usage, and more.

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  22536  3384 ?        Ss   10:15   0:01 /sbin/init
user      1001  0.1  0.3 265672 27340 pts/0    S+   11:30   0:05 python3 myscript.py

top (Real-Time Process Monitoring)

The top command displays a real-time, dynamic view of running processes. It shows a continually updated list of processes, sorted by CPU usage

top

  • Interactive Options:
    • Press q to quit
    • Press k followed by a process ID to kill a process
    • Press M to sort by memory usage

htop (Interactive Process Viewer)

htop is an interactive, user-friendly alternative to top. It allows you to view and manage processes in a more visual way. You can scroll through the list of processes, search, and manage them more easily.

Fetures :

  • Color-coded resource usage.
  • Easily search for processes (/ to search).
  • Use function keys (F1-F10) to interact with processes.

pgrep (Process Grep)

pgrep is a handy tool for searching processes by name. It outputs the process IDs of processes that match the search criteria

pgrep python – this will return the process IDs of all running Python processes

jobs (Job Control)

The jobs command shows a list of background and stopped jobs in the current shell session.

jobs – list all the jobs

[1]+  Stopped                 myscript.sh
[2]-  Running                 python3 myscript.py &

Killing Processes

Sometimes processes become unresponsive, consume too many resources, or need to be terminated for other reasons. Linux provides several commands to kill processes.

kill

the kill command sends a signal to a process to terminate it. You can use it with a process ID (PID)

  • Basic usage
    • kill <PID>
  • Common Signals
    • SIGTERM (15): Graceful termination (default signal)
    • SIGKILL (9): Forcefully kill a process.
    • kill -9 1234 – this forcefully terminates the process with PID 1234
  • killall allows you to kill all instances of a process by name, rather than using a PID.
    • killall firefox

pkill

pkill is similar to pgrep, but instead of listing processes, it kills them based on the name or other criteria

  • pkill python -> kill process by name
  • pkill -9 python -> send specific signal

Background and Foreground Processes

Linux allows you to run processes in the background, meaning they do not block your terminal while running. You can also move them between the foreground and background.

Run a process in the background

To run a process in the background, append & to the command:

python3 myscript.py &

Bringing a background process to the foreground (fg)

The fg command brings a background process back to the foreground.

  • Bring a Process to Foreground: fg %1 -> the %1 corresponds to the job number, which can be seen by running the jobs command.

Moving a Foreground Process to background (bg)

if you started a process in the foreground but want to move it to the background but want to move it to the background, first stop it using Ctrl + Z and then use bg to resume it in the background

bg %1

Running a Process After Logout(nohup)

The nohup command allows you t o run a command immune to hangups, meaning it will continue to run even after you log out of the session

nohup python3 myscript.py & -> output will be redirected to nohup.out by default

Conclusion

Process management is a critical skill for any Linux user. Whether you’re monitoring resource usage, managing multiple tasks, or killing rogue processes, the commands covered here provide all the tools you need to stay in control of your system’s processes.

  • ps, top, and htop help you see what’s happening.
  • kill, killall, and pkill give you control over stopping processes.
  • &, fg, bg, and nohup allow you to move processes between the background and foreground, or keep them running after logging out.

Understanding these commands will make you more efficient in managing processes in Linux, helping you work smarter and faster.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top