Skip to content

8. Process Management

8.1 Viewing Processes

bash
# View all processes
ps aux

# View processes in the current terminal
ps

# Show process relationships in a tree format
ps auxf

# View a specific process
ps aux | grep nginx

# View the process tree (built-in)
pstree -p
bash
$ ps aux | head -5
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.3 169436 13244 ?        Ss   Jun19   0:12 /sbin/init
root         2  0.0  0.0      0     0 ?        S    Jun19   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        I<   Jun19   0:00 [rcu_gp]
root         4  0.0  0.0      0     0 ?        I<   Jun19   0:00 [rcu_par_gp]

top — Real-time Process Monitoring

bash
top
# Useful shortcuts: M=sort by memory, P=sort by CPU, k=kill process, q=quit

htop
# A more user-friendly interactive interface (install with: sudo apt install htop)
bash
top - 10:30:45 up 15:23,  2 users,  load average: 0.52, 0.48, 0.35
Tasks: 234 total,   1 running, 233 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.2 us,  1.3 sy,  0.0 ni, 93.1 id,  0.3 wa,  0.0 hi,  0.1 si
MiB Mem :   4096.0 total,    256.3 free,   2840.5 used,    999.2 buff/cache
MiB Swap:   2048.0 total,   1800.2 free,    247.8 used.   1024.1 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   1234 mysql     20   0 1843652 512340  23456 S   2.3  12.5  10:30.45 mysqld
   2345 www-data  20   0  856432 335876   8765 S   0.5   8.2   2:15.23 apache2
   3456 user      20   0  987654 115234   5678 S   1.2   2.8   0:15.67 vim

8.2 Controlling Processes

bash
# Send a signal to a process
kill PID               # Send SIGTERM (graceful termination)
kill -9 PID            # Send SIGKILL (force termination)
kill -HUP PID          # Send SIGHUP (reload configuration)

# Kill a process by name
killall nginx
pkill -f "python app.py"

# Run a command in the background
long_task &

# Suspend/resume a foreground process
# Ctrl+Z to suspend, bg to resume in background, fg to resume in foreground

# nohup — continue running after disconnecting from the terminal
nohup ./backup.sh > backup.log 2>&1 &

# Change process priority (-20 is highest, 19 is lowest)
nice -n 10 ./task.sh          # Start with a lower priority
renice 5 -p 1234              # Change the priority of a running process
bash
$ nohup ./backup.sh > backup.log 2>&1 &
[1] 5678

$ jobs
[1]+  Running                 nohup ./backup.sh > backup.log 2>&1 &

$ kill 5678
[1]+  Terminated              nohup ./backup.sh > backup.log 2>&1 &

8.3 systemctl — Service Management

bash
# Start/stop/restart a service
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx    # Reload configuration (without interrupting the service)

# View service status
systemctl status nginx

# Enable/disable at boot
sudo systemctl enable nginx
sudo systemctl disable nginx

# View all service statuses
systemctl list-units --type=service

# View failed services
systemctl --failed
bash
$ systemctl status nginx
 nginx.service - A high performance web server and reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2026-06-20 10:00:00 CST; 30min ago
       Docs: man:nginx(8)
    Process: 1234 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; (code=exited, status=0/SUCCESS)
   Main PID: 1235 (nginx)
      Tasks: 3 (limit: 4915)
     Memory: 8.5M
        CPU: 120ms
     CGroup: /system.slice/nginx.service
             ├─1235 "nginx: master process /usr/sbin/nginx -g daemon on;"
             ├─1236 "nginx: worker process"
             └─1237 "nginx: worker process"

Jun 20 10:00:00 server systemd[1]: Starting A high performance web server...
Jun 20 10:00:00 server systemd[1]: Started A high performance web server.

journalctl — Viewing System Logs

bash
# View all logs
journalctl

# View logs for a specific service
journalctl -u nginx

# Follow logs in real-time
journalctl -f

# View today's logs
journalctl --since today

# View the last 50 lines
journalctl -n 50

# Show only errors
journalctl -p err