Processes
ps, pstree, pgrep, kill/signals, nice/renice, job control, cgroups, ulimit, top/htop/btop.
ps
All processes (BSD style):
ps auxAll processes (POSIX style):
ps -efTree view:
ps auxf/ps -ejHBy user:
ps -u joBy name (exact):
ps -C nginxBy PID with custom columns:
ps -p 1234 -o pid,ppid,user,%cpu,%mem,stat,start,time,commCustom columns + sort (desc by mem):
ps -eo pid,ppid,user,%cpu,%mem,comm --sort=-%mem | head -20Top CPU:
ps aux --sort=-%cpu | head -20All threads:
ps -eLfThreads for one process:
ps -T -p 1234
pstree
Full tree:
pstreeInclude PIDs:
pstree -pShow users:
pstree -uCommand-line args:
pstree -aTree rooted at PID:
pstree -p 1234
Finding processes
By name (PID only):
pgrep nginxMatch full command line:
pgrep -f "python script.py"With command line:
pgrep -a nginxWith name:
pgrep -l nginxBy user:
pgrep -u joCount matches:
pgrep -c nginxWhat’s using a port:
ss -tlnp sport = :80/fuser 80/tcpWhat’s using a file:
fuser /var/log/syslog/lsof /var/log/syslogOpen files of process:
lsof -p 1234Network sockets:
lsof -i/lsof -i :80/lsof -i tcp
Kill
Graceful (SIGTERM):
kill 1234Force (SIGKILL):
kill -9 1234Reload config (SIGHUP):
kill -HUP 1234By exact name:
killall nginxBy pattern (full command):
pkill -f "python script.py"All processes of a user:
pkill -u baduserKill on port:
fuser -k 80/tcpKill process group (negative PID):
kill -TERM -1234
Signals
| Signal | # | Default | Use |
|---|---|---|---|
SIGHUP | 1 | terminate | reload config / terminal hangup |
SIGINT | 2 | terminate | Ctrl+C |
SIGQUIT | 3 | core dump | Ctrl+\ |
SIGKILL | 9 | terminate | force kill (uncatchable) |
SIGTERM | 15 | terminate | graceful (default kill) |
SIGSTOP | 19 | stop | pause (uncatchable) |
SIGCONT | 18 | continue | resume |
SIGUSR1 | 10 | terminate | app-specific |
SIGUSR2 | 12 | terminate | app-specific |
List signals:
kill -lSend by name or number:
kill -SIGTERM 1234/kill -15 1234Trap in shell script:
trap 'echo "Caught SIGINT"; cleanup; exit' INT TERM
nice / renice
Range -20 (highest) to 19 (lowest). Default 0. Negative needs root.
Start lower priority:
nice -n 10 tar czf backup.tar.gz /dataStart higher (root):
sudo nice -n -5 important-processChange running process:
renice 10 -p 1234For all of a user:
sudo renice 15 -u baduserFor a process group:
sudo renice 10 -g 5678Check value:
ps -o pid,ni,comm -p 1234
Job control
Background:
long-command &List jobs:
jobs/jobs -l(with PID)Suspend foreground:
Ctrl+ZResume in background:
bg/bg %2Resume in foreground:
fg/fg %1Wait for jobs:
wait/wait %1/wait 1234Detach from shell:
disown %1/disown -aImmune to hangup, output redirected:
nohup long-command > output.log 2>&1 &Fully detached (new session):
setsid long-command &
cgroups (systemd)
View hierarchy:
systemd-cglsLike top for cgroups:
systemd-cgtopSet memory limit on service:
sudo systemctl set-property nginx.service MemoryMax=512MSet CPU quota (100% = 1 core):
sudo systemctl set-property myapp.service CPUQuota=50%Persistent via override (
systemctl edit myapp.service):[Service] MemoryMax=1G MemoryHigh=800M CPUQuota=200% TasksMax=100 IOWeight=50Transient scope with limits:
sudo systemd-run --scope -p MemoryMax=256M -p CPUQuota=50% ./heavy-taskCurrent cgroup of process:
cat /proc/1234/cgroupAvailable controllers:
cat /sys/fs/cgroup/cgroup.controllers
ulimit
All limits:
ulimit -aOpen files:
ulimit -nMax user processes:
ulimit -uVirtual memory:
ulimit -vStack:
ulimit -sLimits of running process:
cat /proc/1234/limitsSet soft (session):
ulimit -n 65535Set hard (root):
ulimit -Hn 65535Persistent (
/etc/security/limits.conf):jo soft nofile 65535 jo hard nofile 65535 @developers soft nproc 4096Systemd service limit (in unit):
[Service] LimitNOFILE=65535 LimitNPROC=4096
Real-time monitors
top — classic:
top(M=sort mem, P=sort cpu, k=kill, r=renice, 1=per-CPU, c=full cmd)htop — improved:
htop(F5 tree, F6 sort, F9 kill, F2 setup)btop — modern:
btopiotop — per-process disk I/O:
sudo iotop -oiftop — per-connection bandwidth:
sudo iftop -i enp0s3nethogs — per-process bandwidth:
sudo nethogs enp0s3