Skip to content
Laviyx Learn

Try

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.

Intermediate6 min readUpdated
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#

Terminal
ps
Output
    PID TTY          TIME CMD
   2658 ?        00:00:00 timeout
   2659 ?        00:00:00 bash
   2660 ?        00:00:00 ps

ps aux shows every process on the system, with their owners and resource use. Most people use it with grep or sort:

Terminal
ps aux | head -n 3 | cut -c1-70
Output
USER         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 [kt
ColumnMeaning
USEROwner
PIDProcess ID
%CPU, %MEMShare of CPU and memory used
STATState: R running, S sleeping, D waiting on disk, Z zombie
COMMANDWhat was run

To see the biggest users of memory, sort the list:

Terminal
ps -eo pid,user,%mem,comm --sort=-%mem | head -n 5

-o chooses the columns, and it also works for chosen programs:

Terminal
ps -C cron -o pid,user,comm
Output
    PID USER     COMMAND
    882 root     cron

top#

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:

Terminal
top -bn1 | head -n 3 | cut -c1-70
Output
top - 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 si

Load 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:

Terminal
sleep 300 &
echo "Started process $!"
jobs
Output
Started 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:

Terminal
pid=$(pgrep -n sleep)
kill "$pid"
sleep 0.2
pgrep sleep | wc -l
Output
0
SignalNumberMeaning
SIGTERM15Please stop; the program can clean up
SIGKILL9Stop now; it cannot be caught
SIGINT2Interrupt, sent by Ctrl+C
SIGHUP1The terminal closed; often means "reload settings"

Try 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:

Terminal
sleep 200 &
sleep 200 &
pkill sleep
sleep 0.2
pgrep -c sleep
Output
0

Keep 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:

Terminal
nohup sh -c 'sleep 1; echo finished' > job.log 2>&1 &
sleep 1.5
cat job.log
Output
finished

For 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?#

Terminal
sudo ss -ltnp | grep :80      # what listens on port 80
sudo lsof /var/log/syslog     # who has this file open

Finding a slow server#

  1. uptime: is the load high?
  2. top: which process is using the CPU or memory?
  3. free -h: is memory low?
  4. df -h: is the disk full?
  5. Read the logs of the process.
Terminal
uptime | cut -d, -f3-
free -h | head -n 2 | cut -c1-48
Output
  load average: 0.82, 0.43, 0.17
               total        used        free    
Mem:           7.7Gi       937Mi       5.1Gi

Common 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
Terminal
sleep 100 &
pgrep -c sleep
kill $!
sleep 0.2
pgrep -c sleep
Output
1
0

Key takeaway#

Next: Services and Logs with systemd.

Finished this lesson?