Linux and Bash course menu
Linux Processes: ps, top, kill and Background Jobs
See running programs with ps and top, understand PIDs, stop processes with kill and signals, run jobs in the background, keep them alive with nohup, and find high CPU use.
On this page
A process is a running program. Each has a unique number, the PID, an owner, and a parent process that started it.
See your processes#
ps PID TTY TIME CMD
2658 ? 00:00:00 timeout
2659 ? 00:00:00 bash
2660 ? 00:00:00 psps aux shows every process on the system, with their owners and resource use. Most people use it with grep or sort:
ps aux | head -n 3 | cut -c1-70USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COM
root 1 1.9 0.1 22936 14380 ? Ss 06:26 0:03 /sb
root 2 0.0 0.0 0 0 ? S 06:26 0:00 [ktTo see the biggest users of memory, sort the list:
ps -eo pid,user,%mem,comm --sort=-%mem | head -n 5-o chooses the columns, and it also works for chosen programs:
ps -C cron -o pid,user,comm PID USER COMMAND
882 root crontop#
top shows a live view. Press P to sort by CPU, M by memory, and q to quit. htop is a friendlier version that you can install.
-b prints a single snapshot, which suits scripts:
top -bn1 | head -n 3 | cut -c1-70top - 06:29:00 up 2 min, 0 user, load average: 0.89, 0.44, 0.17
Tasks: 156 total, 1 running, 155 sleeping, 0 stopped, 0 zombie
%Cpu(s): 4.5 us, 4.5 sy, 0.0 ni, 90.9 id, 0.0 wa, 0.0 hi, 0.0 siLoad average is the average number of tasks running or waiting over the last 1, 5 and 15 minutes. On a 4-core machine, a load of 4 means that all cores are busy.
Background jobs#
Add & to run a command in the background, and keep using the terminal:
sleep 300 &
echo "Started process $!"
jobsStarted process 2668
[1]+ Running sleep 300 &$! is the PID of the last background process. In an interactive terminal, Ctrl+Z pauses the current program, bg continues it in the background, and fg brings it back to the foreground.
Stopping processes#
kill sends a signal to a process. The default is SIGTERM, a polite request to stop:
pid=$(pgrep -n sleep)
kill "$pid"
sleep 0.2
pgrep sleep | wc -l0Try SIGTERM first. kill -9 gives a program no chance to save its data, so use it only when the normal way has failed.
pkill and killall stop processes by name:
sleep 200 &
sleep 200 &
pkill sleep
sleep 0.2
pgrep -c sleep0Keep a program running after you log out#
Closing a terminal normally stops its jobs. nohup protects a command from that, and saves the output to a file:
nohup sh -c 'sleep 1; echo finished' > job.log 2>&1 &
sleep 1.5
cat job.logfinishedFor anything long-lived on a server, use a systemd service or tmux, which keeps whole terminal sessions alive.
What is using a port or a file?#
sudo ss -ltnp | grep :80 # what listens on port 80
sudo lsof /var/log/syslog # who has this file openFinding a slow server#
uptime: is the load high?top: which process is using the CPU or memory?free -h: is memory low?df -h: is the disk full?- Read the logs of the process.
uptime | cut -d, -f3-
free -h | head -n 2 | cut -c1-48 load average: 0.82, 0.43, 0.17
total used free
Mem: 7.7Gi 937Mi 5.1GiCommon mistakes#
Practice#
Practice exercise
Start sleep 100 in the background, print whether it is running by counting pgrep -c sleep, stop it, and count again.
Show solution
sleep 100 &
pgrep -c sleep
kill $!
sleep 0.2
pgrep -c sleep1
0Key takeaway#
Finished this lesson?