tech katta

Processes: ps, top, signals, kill, jobs and nohup

Lesson 08 of 23 · 5 min readFree

In this lesson

See what is running on a server, find the process that is eating CPU, stop it politely or forcefully with signals, and keep a command alive after you log out.

On this page
  1. Why this matters
  2. What a process is
  3. ps: a snapshot of processes
  4. top: a live view
  5. Signals and kill
  6. Jobs, background and foreground
  7. nohup: surviving logout
  8. Try it

A running program is called a process. Your server is running dozens of them: the web server, the database, SSH, your own app. When the server feels slow or an app will not stop, you need to find the right process and deal with it, without touching the wrong one.

Why this matters#

"The site is slow" is often "one process is using all the CPU", and "I cannot restart the app" is often "the old process is still holding the port". Both are solved with the commands in this lesson. You also need to know the difference between asking a process to stop and forcing it, because forcing a database to stop can lose data.

What a process is#

Each process has a numeric PID (process id), a PPID (the PID of the process that started it, its parent), an owner and a state. Every process except the first one was started by another, so processes form a tree. When you run ls from your shell, the shell is the parent and ls is the child.

ps: a snapshot of processes#

ps prints the processes at the moment you run it. With no options it shows only your current terminal. These two forms show everything:

Shell
ps aux
ps -eo pid,ppid,user,%cpu,%mem,etime,cmd --sort=-%cpu | head -4
Text
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1486 97.0  0.0   2716  1628 ?        R    20:48   0:00 yes
  PID  PPID USER     %CPU %MEM     ELAPSED CMD
 1445     1 root     99.0  0.0       00:01 yes

We trimmed both outputs to the one interesting row each. yes is a CPU burner we started for the test, and the real list on a server is much longer. ps aux lists all users' processes in a fixed set of columns. -eo lets you choose the columns yourself, and --sort=-%cpu puts the heaviest CPU user first (the minus means descending). The STAT column shows the state: R running, S sleeping and waiting for something (the normal state), T stopped, and Z zombie, a finished process that its parent has not yet cleaned up. Names in square brackets, such as [kworker/0:1], are kernel threads.

To find a process by name, use pgrep instead of ps aux | grep, which also matches the grep command itself:

Shell
pgrep -fa 'sleep 301'
Text
1358 sleep 301

-f matches against the whole command line and -a shows it. Its sibling pkill sends a signal to every match, so check with pgrep first.

top: a live view#

top refreshes every few seconds, like a task manager. Press q to quit, P to sort by CPU, M to sort by memory and k to kill a process by PID. For a one-time snapshot you can paste into a ticket, use batch mode:

Shell
top -b -n1 -o %CPU | head -9
Text
top - 20:47:53 up 6 min,  0 user,  load average: 0.04, 0.05, 0.01
Tasks:  92 total,   2 running,  90 sleeping,   0 stopped,   0 zombie
%Cpu(s): 11.6 us, 14.0 sy,  0.0 ni, 72.1 id,  0.0 wa,  0.0 hi,  2.3 si,  0.0 st 
MiB Mem :  16087.1 total,  15042.6 free,    559.9 used,    745.3 buff/cache     
MiB Swap:      0.0 total,      0.0 free,      0.0 used.  15527.2 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 1381 root      20   0    3144   1892   1760 R 100.0   0.0   0:01.20 yes
    1 root      20   0   43944   6172   5548 S   0.0   0.0   0:00.56 process_a+

To produce this we ran yes > /dev/null, a program that does nothing but burn one CPU core. It sits at the top with 100% CPU. In the header, id is the idle share of CPU time, wa is time spent waiting on disk, and the load average (three numbers for the last 1, 5 and 15 minutes) roughly counts processes that wanted to run. A load average well above the number of CPU cores, which nproc prints, means the machine is overloaded. In the memory lines, look at avail Mem, not free: Linux uses spare memory as cache, so a small "free" figure is normal.

Signals and kill#

A signal is a small message the kernel delivers to a process. kill is a badly named command: it does not necessarily kill, it sends a signal. The signals you need:

  • SIGTERM (15) asks the process to terminate. The process can catch it, finish what it is doing and exit cleanly. This is the default for kill.
  • SIGKILL (9) cannot be caught or ignored. The kernel removes the process at once with no chance to clean up.
  • SIGINT (2) is what Ctrl+C sends.
  • SIGHUP (1) means "hang up". Many daemons, nginx among them, reread their config when they get it, so you can reload without restarting.
  • SIGSTOP and SIGCONT pause and resume a process.

You can list them or look one up by name or number:

Shell
kill -l TERM
kill -l 9
Text
15
KILL

We wrote a tiny script that handles two signals, started it in the background, and sent it signals:

Shell
kill -HUP 1337
kill 1337
cat /tmp/slow.out
Text
got HUP, reloading
got TERM, cleaning up

The script received HUP and kept running, and then received TERM, ran its cleanup and exited. That is how a well-behaved service shuts down: it flushes buffers, closes connections and releases locks. A process killed with SIGKILL gets none of that:

Shell
sleep 300 &
kill -9 $!
wait $!
echo "exit=$?"
Text
exit=137

Exit code 137 is 128 plus 9, the convention for "died from signal 9". You will see this number in container and CI logs, where it usually means the process was killed, often for using too much memory.

Always try plain kill PID first and give the process a few seconds. Use kill -9 PID only when it ignores TERM. Be careful with pkill, which matches by name and can stop more than you intended. You can only signal your own processes unless you use sudo.

Jobs, background and foreground#

Your shell can run commands in the background so the prompt returns. Add & at the end:

Shell
sleep 300 &
jobs
Text
[1]+  Running                 sleep 300 &

jobs lists background jobs started from this shell, numbered [1], [2]. In an interactive shell:

  • Ctrl+Z suspends the running command and puts it in the background, stopped.
  • bg %1 lets a stopped job continue in the background.
  • fg %1 brings job 1 back to the foreground.
  • kill %1 sends a signal to job 1.

The test machine for this lesson runs commands without a terminal, so Ctrl+Z and fg could not be tried here. A stopped process does show up with state T in ps, which we confirmed by sending SIGSTOP and then SIGCONT. Treat the key sequences above as standard behaviour you should try on your own terminal.

nohup: surviving logout#

A background job started from your SSH session is tied to it. When the session closes, the shell sends SIGHUP to its jobs, and they usually die. nohup makes a command ignore SIGHUP:

Shell
nohup ./long-task.sh > task.log 2>&1 &

The redirection sends output to task.log. If you leave it out and stdout is a terminal, nohup writes output to a file named nohup.out instead. We ran nohup sleep 120 with output redirected and it printed nothing, as expected.

nohup is fine for a one-off. For anything that should keep running, restart after a crash and start at boot, run it as a service. The next lesson covers systemctl. A terminal multiplexer such as tmux is another option for long interactive sessions.

Try it#

  1. Run ps aux | head -5 and ps -eo pid,ppid,cmd | head. Find the PID of your current shell with echo $$ and look at its PPID.
  2. Start sleep 500 &, then run jobs, pgrep -fa 'sleep 500' and kill %1. Confirm it is gone with pgrep.
  3. Start yes > /dev/null &, find it with top (press P to sort by CPU), then stop it with a plain kill.
  4. Write a script that traps TERM and prints a message, as in the lesson. Send it kill PID and read the output, then try kill -9 on a second copy and compare.
  5. Run nproc and uptime. Is the one-minute load average more or less than the number of CPU cores?