#Process Management
Learn to manage processes, services, and system resources on Linux.
#📋 Table of Contents
#Viewing Processes
#ps Command
bash
1# All processes
2ps aux
3
4# Process tree
5ps auxf
6
7# Specific process
8ps aux | grep nginx
9
10# Custom format
11ps -eo pid,ppid,user,%cpu,%mem,cmd --sort=-%cpu | head
12
13# Output fields:
14# PID - Process ID
15# PPID - Parent Process ID
16# USER - Owner
17# %CPU - CPU usage
18# %MEM - Memory usage
19# STAT - State (R=running, S=sleeping, Z=zombie)
20# CMD - Command#top and htop
bash
1# Interactive process viewer
2top
3
4# Better alternative
5htop
6
7# Key shortcuts in top:
8# k - Kill process
9# r - Renice (change priority)
10# f - Select fields
11# q - Quit#pgrep and pidof
bash
1# Find PID by name
2pgrep nginx
3pgrep -u root
4
5# Find exact PID
6pidof nginx#Process Control
#Killing Processes
bash
1# Graceful termination (SIGTERM)
2kill PID
3kill 1234
4
5# Force kill (SIGKILL)
6kill -9 PID
7kill -KILL PID
8
9# Kill by name
10pkill nginx
11killall nginx
12
13# Common signals:
14# SIGTERM (15) - Graceful shutdown
15# SIGKILL (9) - Force kill
16# SIGHUP (1) - Reload configuration
17# SIGSTOP (19) - Pause process
18# SIGCONT (18) - Resume process#Process Priority (nice)
bash
1# Run with lower priority (higher nice value)
2nice -n 10 ./script.sh
3
4# Run with higher priority (requires root)
5sudo nice -n -10 ./critical-script.sh
6
7# Change priority of running process
8renice 10 -p 1234
9sudo renice -10 -p 1234
10
11# Nice values: -20 (highest) to 19 (lowest)
12# Default is 0#Background Jobs
bash
1# Run command in background
2./long-script.sh &
3
4# List background jobs
5jobs
6
7# Bring to foreground
8fg %1
9
10# Send to background
11Ctrl+Z # First suspend
12bg %1 # Then background
13
14# Keep running after logout
15nohup ./script.sh &
16nohup ./script.sh > output.log 2>&1 &
17
18# Disown a job
19./script.sh &
20disown#Screen and Tmux
bash
1# Screen - terminal multiplexer
2screen -S session_name # New session
3screen -r session_name # Reattach
4Ctrl+A, D # Detach
5
6# Tmux - modern alternative
7tmux new -s session_name # New session
8tmux attach -t session_name # Reattach
9Ctrl+B, D # Detach#Systemd Services
#Service Management
bash
1# Start/stop/restart
2sudo systemctl start nginx
3sudo systemctl stop nginx
4sudo systemctl restart nginx
5sudo systemctl reload nginx
6
7# Enable/disable at boot
8sudo systemctl enable nginx
9sudo systemctl disable nginx
10
11# Check status
12systemctl status nginx
13systemctl is-active nginx
14systemctl is-enabled nginx
15
16# List all services
17systemctl list-units --type=service
18systemctl list-units --type=service --state=running#Creating Custom Service
ini
1# /etc/systemd/system/myapp.service
2[Unit]
3Description=My Application
4After=network.target
5Requires=postgresql.service
6
7[Service]
8Type=simple
9User=appuser
10Group=appgroup
11WorkingDirectory=/opt/myapp
12Environment="NODE_ENV=production"
13ExecStart=/usr/bin/node /opt/myapp/server.js
14ExecStop=/bin/kill -TERM $MAINPID
15Restart=always
16RestartSec=10
17
18[Install]
19WantedBy=multi-user.targetbash
# Reload and start
sudo systemctl daemon-reload
sudo systemctl enable --now myapp#Viewing Logs
bash
1# Service logs
2journalctl -u nginx
3
4# Follow logs
5journalctl -u nginx -f
6
7# Logs since boot
8journalctl -u nginx -b
9
10# Logs from time
11journalctl -u nginx --since "2024-01-01"
12journalctl -u nginx --since "1 hour ago"
13
14# Kernel logs
15journalctl -k
16dmesg#Resource Limits
#ulimit
bash
1# View limits
2ulimit -a
3
4# Max open files
5ulimit -n
6ulimit -n 65535 # Set for current session
7
8# Max processes
9ulimit -u
10
11# Make permanent in /etc/security/limits.conf:
12# username soft nofile 65535
13# username hard nofile 65535#cgroups
bash
1# View cgroup resources
2cat /proc/1234/cgroup
3
4# Create resource limit with systemd
5[Service]
6MemoryLimit=512M
7CPUQuota=50%#Summary
| Task | Command |
|---|---|
| View processes | ps aux / htop |
| Kill process | kill PID / kill -9 PID |
| Background job | command & |
| Service control | systemctl start/stop service |
| View logs | journalctl -u service |
| Check limits | ulimit -a |
[!TIP] Pro Tip: Always try
kill(SIGTERM) beforekill -9(SIGKILL) to allow graceful shutdown!