guest@linuxthings:~/guides$ man processes
Process Management
A process is a running program, identified by a numeric process ID (PID). Jobs are different: they are the shell's own record of the commands you started from it.
Monitoring (ps, top)
ps # Processes in current shell
ps ux # Processes for current user with details
ps -ef # All processes on system (standard syntax)
ps aux # All processes on system (BSD syntax)
ps -efH # All processes as an indented parent/child tree
ps -C nginx # All processes running a given program
ps -U smith # All processes owned by user smith
ps -o pid,ni,%cpu,%mem,cmd -p 1234 # Choose the columns to show
ps aux --sort=-%mem | head # Biggest memory users first
pstree -p # Process tree with PIDs
top # Real-time interactive process viewer
htop # Friendlier top, if installed
uptime # System load averagesIn top, press M to sort by memory, P to sort by CPU, k to kill a process, 1 to show each CPU, and q to quit. top -b -n1 > snapshot.txt saves a single snapshot.
The three load averages from uptime cover the last 1, 5 and 15 minutes. They count processes that are running or waiting to run, so compare them with your number of CPU cores (nproc).
Finding PIDs (pgrep, pidof)
pgrep firefox # PIDs of processes matching a pattern
pgrep -u smith # PIDs of processes owned by smith
pgrep -f "python app.py" # Match against the full command line
pgrep -c python # Count matching processes
pidof sshd # PIDs for an exact program name
ps -fp $(pgrep nginx) # Full details for those processespgrep avoids the classic mistake of ps aux | grep name matching the grep itself.
Control (kill, pkill)
kill 1234 # Terminate PID 1234 (sends TERM)
kill -9 1234 # Force kill (KILL signal)
kill -HUP 1234 # Ask a daemon to reload its configuration
kill -STOP 1234 # Pause a process
kill -CONT 1234 # Resume a paused process
kill -l # List signal names and numbers
killall firefox # Kill processes by name
pkill -u smith # Kill all processes by user smith
pkill -f "python app.py" # Kill by full command line
timeout 10 cmd # Kill cmd if it runs longer than 10 secondsSignal | Number | Meaning |
|---|---|---|
HUP | 1 | Hang up; many daemons reload their config |
INT | 2 | Interrupt (what Ctrl+C sends) |
KILL | 9 | Kill immediately; cannot be caught or ignored |
TERM | 15 | Polite request to exit (the default) |
STOP | 19 | Pause (cannot be caught) |
CONT | 18 | Continue after a stop |
Try plain kill first and use -9 only if the process ignores it, because a killed process gets no chance to clean up.
Priorities (nice, renice)
nice -n 10 command # Start command with lower priority
sudo nice -n -5 command # Higher priority (root only)
renice -n 5 -p 1234 # Change priority of running process
renice -n 5 -u smith # Change all of a user's processes
ionice -c3 command # Run with idle-only disk priorityNice values run from -20 (highest priority) to 19 (lowest); normal processes start at 0. Ordinary users can only make their processes nicer (a higher number).
Job Control
command & # Run in background
Ctrl+Z # Suspend current foreground job
bg # Resume suspended job in background
fg # Bring job to foreground
fg %2 # Bring job number 2 to the foreground
jobs # List jobs in current shell
kill %1 # Kill job number 1
disown %1 # Detach a job so it survives closing the shell
nohup command & # Run immune to hangups; output goes to nohup.outServices (systemd)
Most distributions use systemd to start and supervise background services.
systemctl status nginx # Is it running? Recent log lines
sudo systemctl start nginx # Start now
sudo systemctl stop nginx # Stop now
sudo systemctl restart nginx # Stop then start
sudo systemctl reload nginx # Re-read configuration without restarting
sudo systemctl enable --now nginx # Start at boot, and start right away
sudo systemctl disable nginx # Don't start at boot
systemctl list-units --type=service # Loaded services and their state
systemctl --failed # Units that failed to startLogs (journalctl)
journalctl -u nginx # Logs for one service
journalctl -u nginx -f # Follow new entries live
journalctl -b # Everything since the last boot
journalctl -b -1 # The previous boot
journalctl -p err -b # Errors and worse, this boot
journalctl --since "1 hour ago" # Time-limited view
journalctl -r # Newest entries first
journalctl --list-boots # Recorded bootsScheduling (cron, at)
crontab -e # Edit your scheduled jobs
crontab -l # List them
at 18:00 # Run commands once at 6 PM (finish with Ctrl+D)
atq # List pending at jobs
watch -n 5 df -h # Re-run a command every 5 seconds
sleep 30; echo done # Wait before running the next commandA crontab line has five time fields, then the command: minute, hour, day of month, month, day of week (0 = Sunday).
# m h dom mon dow command
30 2 * * * /usr/local/bin/backup.sh # Every day at 02:30
*/15 * * * * /usr/local/bin/check.sh # Every 15 minutes
0 9 * * 1-5 /usr/local/bin/report.sh # Weekdays at 09:00Use full paths in crontab entries, since cron runs with a minimal PATH. For long or complex commands, put them in a script and call that.