Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

Linux Signals and kill Command: kill, pkill, killall, SIGTERM vs SIGKILL and trap

A signal is a short message the kernel delivers to a process: please stop, stop now, reload your config, pause, continue. kill, pkill and killall send signals; Ctrl+C and Ctrl+Z send them from the keyboard; timeout sends one after a time limit; and trap lets your own scripts react to them. You also learn why zombies and D-state processes do not die.

In this chapter

  • List signals and know the common ones
  • Use kill with a PID and a signal
  • Choose SIGTERM before SIGKILL
  • Use SIGHUP to reload, SIGSTOP/SIGCONT to pause
  • Use pkill, killall and timeout
  • Write trap handlers in bash; handle zombies and D-state

1. An everyday example: the PMT bus conductor

On a Pune PMT bus, the conductor has a few fixed signals. One bell: next stop, please get ready (polite). Two quick bells: stop right now. A long whistle: driver, wait here (pause). Another whistle: go again (continue). If the route changes, the depot radios: 'take the new route sheet' without stopping the bus. If a passenger is asleep and does not respond, the conductor cannot throw him out while the bus crosses a bridge; he waits.

One bell = SIGTERM (15, polite stop, can clean up). Two quick bells = SIGKILL (9, cannot be refused). Long whistle = SIGSTOP. Go again = SIGCONT. New route sheet = SIGHUP (reload config). Passenger on the bridge = D-state: even SIGKILL waits until the IO finishes.

How a signal is handledYou (or the kernel) send a signal to a PIDThe kernel marks it pending on that processThe process has a handler? It runs its own code (cleanup, reload)Ignored? Nothing happens (not possible for KILL and STOP)Default? Usually terminate, sometimes core dump, stop or ignoreThe parent gets the exit status (128 + signal number in shells)
Exit code 137 = 128+9 (killed), 143 = 128+15 (terminated), 130 = 128+2 (Ctrl+C).

2. kill -l: the list of signals

You must know signal names and numbers to pick the right one and to read exit codes like 137.

kill -l lists all signals. kill -l 9 prints the name for a number. You can use names (TERM, SIGTERM) or numbers (15). Common ones are below; numbers can differ on non-x86 systems, so names are safer.

SignalNo.DefaultTypical use
SIGHUP1TerminateReload config / terminal closed
SIGINT2TerminateCtrl+C
SIGQUIT3Core dumpCtrl+\
SIGKILL9Terminate, cannot catchLast resort
SIGTERM15TerminatePolite stop (default of kill)
SIGSTOP19Stop, cannot catchPause
SIGTSTP20StopCtrl+Z
SIGCONT18ContinueResume
SIGUSR1/210/12TerminateApp-defined (log reopen, dump stats)
  1. List them.
kill -l | head -2

Output:

 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1

64 signals in total on Linux.

  1. Number to name.
kill -l 15; kill -l 137

Output:

TERM
KILL

kill -l understands exit codes too (137-128=9).

3. kill PID: send a signal

Read kill as “send a signal.” A signal is a short notification from the operating system or another process. Its name describes the request. A program can handle TERM and use time to finish work before exiting. Use a harmless background sleep from your own shell to see the mechanism. Read its PID from $! just after starting it, send TERM to that PID, and use wait or jobs to check that it ended. The send command returning successfully confirms delivery to a valid target. Check the process separately to see whether it exited.

To stop or control one exact process by its PID.

kill PID sends SIGTERM. kill -SIGNAL PID or kill -s SIGNAL PID sends another. Several PIDs at once are allowed. kill %1 targets a shell job. kill -0 PID sends nothing but checks if the PID exists and you may signal it. You can only signal your own processes unless you are root.

  1. Polite stop.
kill 9601; sleep 1; ps -p 9601 || echo gone

Output:

  PID TTY  TIME CMD
gone

Default SIGTERM worked.

  1. By name of signal.
kill -s TERM 9702 9703

Output:

(no output)

Two processes at once.

  1. Is it alive?
kill -0 2201 && echo alive

Output:

alive

Common in scripts and pid-file checks.

  1. Someone else's process.
kill 1420

Output:

bash: kill: (1420) - Operation not permitted

mysql owns it; use sudo.

4. SIGTERM vs SIGKILL

In a real service, a graceful stop might finish a file write and release a lock before exiting. A forced stop can leave the next run with partial output. For the lab, write the two expected results down first. After TERM, the script should do its cleanup and leave its “stopped” evidence. After KILL, the kernel ends the process without giving that cleanup code time to run. Inspect the lab files after both runs so you can explain why the two commands can produce the same disappearance in ps but different results on disk.

kill -9 first is a common habit and it causes corrupt files, lost data and leftover locks.

SIGTERM (15) asks the program to finish: save data, close connections, remove pid files. SIGKILL (9) is handled by the kernel: the process is removed at once, with no cleanup. Order: TERM, wait 10-30 s, then KILL only if needed. systemd does the same on stop (TimeoutStopSec).

SIGTERM 15SIGKILL 9
Can be caughtYesNo
CleanupYesNo
UseFirst, alwaysOnly if TERM fails
  1. Ask first, wait, then force.
kill 7710; for i in $(seq 10); do kill -0 7710 2>/dev/null || break; sleep 1; done; kill -0 7710 2>/dev/null && kill -9 7710

Output:

(no output)

Graceful stop with a 10 second limit.

  1. What KILL looks like to the parent.
sleep 100 & kill -9 $!; wait $!; echo $?

Output:

[1]+  Killed    sleep 100
137

137 = 128 + 9.

  1. OOM killer uses SIGKILL too.
sudo journalctl -k | grep -i 'killed process' | tail -1

Output:

Out of memory: Killed process 8812 (python) total-vm:9.1G

Exit code 137 in Docker/K8s often means OOM.

5. SIGHUP: reload without restart

Many daemons re-read their config on SIGHUP, so you can apply changes with no downtime.

For daemons (nginx, sshd, rsyslog, haproxy), SIGHUP means reload. For a normal terminal program it means 'your terminal closed' and it exits (that is why nohup exists). systemctl reload usually sends SIGHUP or runs ExecReload. Always test config first (nginx -t, sshd -t).

  1. Test and reload nginx.
sudo nginx -t && sudo kill -HUP $(cat /run/nginx.pid)

Output:

nginx: configuration file /etc/nginx/nginx.conf test is successful

Workers restart gracefully; connections keep working.

  1. The systemd way.
sudo systemctl reload sshd && systemctl status sshd --no-pager | grep -i reload

Output:

Oct 25 12:01:10 web1 systemd[1]: Reloaded OpenSSH server daemon.

Same effect, logged by systemd.

  1. Tell rsyslog to reopen logs.
sudo pkill -HUP rsyslogd

Output:

(no output)

Used after manual log rotation.

6. SIGINT, SIGSTOP, SIGCONT and keyboard signals

Pause a heavy job for a few minutes during peak, then let it continue; or interrupt a script cleanly.

Ctrl+C sends SIGINT, Ctrl+Z SIGTSTP, Ctrl+\ SIGQUIT to the foreground job. SIGSTOP pauses any process and cannot be caught. SIGCONT resumes it (bg/fg also send CONT). A stopped process shows T in ps.

  1. Pause a backup.
sudo kill -STOP 9500 && ps -o pid,stat,cmd -p 9500

Output:

  PID STAT CMD
 9500 T    tar czf /backup/www.tgz /srv/www

T = stopped, using no CPU.

  1. Resume it.
sudo kill -CONT 9500 && ps -o stat= -p 9500

Output:

R

Continues exactly where it was.

  1. Interrupt from a script.
kill -INT 9810

Output:

(the program prints ^C-like cleanup)

Same as pressing Ctrl+C in its terminal.

7. pkill: signal by name or pattern

When there are many workers or you do not know PIDs, sending by name is faster.

pkill uses the same matching as pgrep: NAME pattern, -f full command line, -u USER, -x exact, -t TTY, -n newest, -o oldest. Default signal TERM; -HUP, -9 etc. Always run pgrep -a with the same options first to see what would match.

  1. Preview, then stop.
pgrep -af 'celery worker'; pkill -f 'celery worker'

Output:

5101 celery worker -A shop
5102 celery worker -A shop

Both workers get TERM.

  1. All processes of a user.
sudo pkill -u intern1

Output:

(no output)

Ends a leaving user's sessions.

  1. Kick a terminal session.
who; sudo pkill -KILL -t pts/3

Output:

guest pts/3 2026-10-25 11:40 (203.0.113.9)

Logs that user out.

  1. Danger of loose patterns.
pgrep -l ssh

Output:

800 sshd
8700 sshd
9050 ssh-agent

pkill ssh would hit sshd too. Use -x.

8. killall: signal by exact program name

Stop every copy of one program with a clear exact-name match.

killall NAME matches the exact process name (not a pattern). -s SIGNAL, -i asks for each, -u USER, -w waits until they die, -o/-y older/younger than a time. Installed by psmisc. On Solaris killall kills everything, so be careful on other Unix systems.

  1. Stop all copies and wait.
killall -w php-fpm

Output:

(returns when all are gone)

-w is good in scripts before a restart.

  1. Ask before each.
killall -i python3

Output:

Kill python3(8812) ? (y/N) y
Kill python3(8820) ? (y/N) n

Safe interactive use.

  1. Old processes only.
killall -o 2h -u ravi sleep

Output:

(no output)

Kills ravi's sleeps older than 2 hours.

9. timeout: limit how long a command runs

A hanging curl, backup or test should not block a cron job or CI pipeline forever.

timeout 30s CMD sends TERM after 30 seconds. -s KILL picks another signal. -k 5s sends KILL 5 s after the first signal if still alive. Exit code 124 means it timed out. Units: s, m, h, d.

  1. Limit a health check.
timeout 5s curl -s https://api.example.com/health; echo $?

Output:

124

124 = timed out; the API is slow.

  1. TERM, then KILL.
timeout -k 10s 1h ./nightly-report.sh

Output:

(no output)

Up to 1 hour, then TERM, then KILL after 10 s.

  1. In cron.
*/5 * * * * timeout 4m /usr/local/bin/sync.sh

Output:

(crontab line)

Next run never overlaps the last.

10. trap: handle signals in bash scripts

When a script is stopped (Ctrl+C, TERM from systemd), it should remove temp files and locks instead of leaving a mess.

trap 'COMMANDS' SIGNALS runs COMMANDS when a signal arrives. EXIT runs on any exit, ERR on a failing command. trap '' INT ignores Ctrl+C. trap - INT restores default. trap -p shows current traps. KILL and STOP cannot be trapped.

  1. Clean up temp files on exit.
cat clean.sh
#!/bin/bash
tmp=$(mktemp)
trap 'rm -f "$tmp"; echo cleaned' EXIT
echo working > "$tmp"; sleep 30

Output:

(press Ctrl+C)
^Ccleaned

Runs on normal end and on Ctrl+C.

  1. Graceful stop on TERM.
trap 'echo stopping; kill $child; wait $child; exit 0' TERM; ./worker & child=$!; wait

Output:

(after kill -TERM of the script) stopping

Passes the stop to the child and waits.

  1. Reload on HUP.
trap 'source /etc/myapp.conf; echo reloaded' HUP

Output:

(after kill -HUP) reloaded

Your script acts like a daemon.

  1. Show active traps.
trap -p

Output:

trap -- 'rm -f "$tmp"; echo cleaned' EXIT

Useful while debugging.

11. Zombies and D-state: processes that will not die

Check the STAT column before repeating a stop command. A row starting with Z represents a process that already exited. Its parent still needs to collect the exit status. A row starting with D usually means the process is in an uninterruptible wait, often for a disk or remote filesystem. Ask a different first question in each case. For Z, find the parent with ps -o pid,ppid,stat,comm. For D, check the storage or network dependency and related logs to understand the wait.

kill -9 does not remove them, and people waste hours. You must know the real fix.

A zombie (Z) has already exited; only its exit record waits for the parent. Fix the parent: send it SIGCHLD or restart it; if the parent dies, systemd adopts and reaps it. A D-state process is inside the kernel waiting for IO (dead NFS, failing disk); signals wait until IO returns. Fix the storage, use umount -f/-l for NFS, or reboot.

  1. Find zombies and their parents.
ps -eo pid,ppid,stat,cmd | awk '$3 ~ /Z/'

Output:

 9901  9900 Z    [worker] <defunct>

PPID 9900 must reap it.

  1. kill -9 on a zombie does nothing.
kill -9 9901; ps -o stat= -p 9901

Output:

Z

It is already dead.

  1. Nudge or restart the parent.
kill -CHLD 9900; sleep 1; ps -o stat= -p 9901 || echo reaped

Output:

reaped

If not, restart the parent service.

  1. D-state from a dead NFS.
ps -eo pid,stat,wchan:20,cmd | awk '$2 ~ /D/'

Output:

7001 D    nfs_wait_bit_killable ls /mnt/nfs

Waiting on NFS; fix the mount.

12. Real examples from many industries

The same Linux skill shows up in every kind of company. Seeing it in many places helps you remember it.

Each row is a made-up team. It shows who uses this and how.

Team and cityHow they use itCommand
Rahul, payments, PuneGraceful TERM then KILL in deploy scriptskill $PID; sleep 15; kill -9 $PID
Elena, CDN, MadridReloads nginx with HUP after cert renewalnginx -t && kill -HUP $(cat /run/nginx.pid)
Joseph, ISP NOC, NairobiLimits monitoring probes with timeouttimeout 10s ping -c3 10.0.0.1
Hiro, robotics, Nagoyatrap cleans lock files in control scriptstrap 'rm -f /run/arm.lock' EXIT
Ananya, data team, BengaluruPauses ETL during business hourspkill -STOP -f etl.py
Lucas, hosting, Sao PauloLogs out idle SSH userspkill -KILL -t pts/5

13. Common errors and how to fix them

Everyone hits these errors. Knowing the cause saves hours.

Read the exact message first. Then match it to a row below.

Error you seeWhy it happensHow to fix
bash: kill: (1420) - Operation not permittedNot your processUse sudo.
sudo kill 1420
bash: kill: (99999) - No such processPID already gone or typoRe-check with pgrep.
pgrep -a myapp
Process ignores kill (SIGTERM)App handles TERM slowly or is stuckWait, then use KILL.
kill -9 PID
kill -9 does not remove <defunct>It is a zombieFix or restart the parent.
ps -o ppid= -p PID
kill -9 has no effect, STAT DStuck in kernel IOFix storage/NFS or reboot.
ps -eo pid,stat,wchan:20,cmd
Container exited with code 137SIGKILL, often the OOM killerCheck memory limits and kernel log.
journalctl -k | grep -i oom
Script exits with 143 under systemdSIGTERM on stop; maybe no trapAdd trap or SuccessExitStatus=143.
systemctl show -p ExecMainStatus app
pkill killed the wrong processesLoose pattern matched morePreview with pgrep -a and use -x or -f carefully.
pgrep -ax nginx
killall: command not foundpsmisc not installedInstall psmisc or use pkill.
sudo dnf install -y psmisc
timeout: exit 124 every runThe command is slower than the limitFind why or raise the limit.
time ./job.sh

14. Troubleshooting step by step

If a process will not stop: identify it, try TERM, check its state, then KILL, and if it still stays, look at zombies or D-state.

Go top to bottom. Stop at the first step that shows the problem.

  1. Confirm the exact process.
pgrep -a myapp
  1. Ask it to stop.
kill PID
  1. Wait and check the state.
sleep 10; ps -o pid,stat,cmd -p PID
  1. Force only if needed.
kill -9 PID
  1. Zombie? Look at the parent.
ps -o ppid= -p PID
  1. D-state? Check storage.
dmesg -T | tail -20

15. Lab 1: Graceful vs forced stop

Write a small script that cleans up on TERM, then compare TERM and KILL.

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

  1. Create the script.
cat > ~/svc.sh <<'X'
#!/bin/bash
touch /tmp/svc.lock
trap 'rm -f /tmp/svc.lock; echo clean exit; exit 0' TERM INT
while true; do sleep 1; done
X
chmod +x ~/svc.sh

Output:

(no output)
  1. Run and stop with TERM.
~/svc.sh & sleep 1; kill $!; wait $!; echo code=$?; ls /tmp/svc.lock

Output:

clean exit
code=0
ls: cannot access '/tmp/svc.lock': No such file or directory
  1. Run and stop with KILL.
~/svc.sh & sleep 1; kill -9 $!; wait $!; echo code=$?; ls /tmp/svc.lock

Output:

code=137
/tmp/svc.lock
  1. See the difference.
echo 'KILL left the lock file behind'

Output:

KILL left the lock file behind
  1. Clean up.
rm -f /tmp/svc.lock ~/svc.sh

Output:

(no output)

16. Lab 2: Pause, resume, reload and time-limit

Use STOP/CONT, pkill with preview, and timeout.

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

  1. Start two workers.
(sleep 500 &) ; (sleep 501 &) ; pgrep -a sleep

Output:

9950 sleep 500
9951 sleep 501
  1. Pause and resume one.
kill -STOP 9950; ps -o pid,stat -p 9950; kill -CONT 9950; ps -o stat= -p 9950

Output:

  PID STAT
 9950 T
S
  1. Preview then pkill.
pgrep -af 'sleep 50'; pkill -f 'sleep 50'; pgrep -c sleep

Output:

9950 sleep 500
9951 sleep 501
0
  1. Time-limit a command.
timeout 2s sleep 10; echo $?

Output:

124
  1. Reload a daemon safely.
sudo sshd -t && sudo systemctl reload sshd && echo reloaded

Output:

reloaded

Ravindra Bagale's Tip

Make 'kill, wait, then kill -9' a habit, never the other way round. And before any pkill, run the same pattern with pgrep -a to see exactly what you are about to stop.

17. Frequently asked questions

Q. Which signal does kill send by default?

SIGTERM (15).

Q. Why avoid kill -9 first?

The program cannot save data or clean up, so you may get corrupt files and stale locks.

Q. pkill or killall?

pkill matches patterns and options like pgrep; killall matches exact names. Both are fine; preview first.

Q. What does exit code 130 mean?

Stopped by Ctrl+C (128 + 2).

Q. Can I trap SIGKILL?

No. KILL and STOP cannot be caught or ignored.

Q. Does SIGHUP always reload?

Only for programs written to do so (most daemons). For others it ends them.

Q. How do I kill a whole process group?

kill -TERM -PGID (negative number), or systemctl stop for services.

Q. Why does my container not stop quickly?

PID 1 in a container ignores TERM unless it handles it; use exec in entrypoints, tini, or a trap.

18. Interview questions and answers

Beginner level

Q. What is a signal?

A short message from the kernel to a process, like stop, pause or reload.

Q. SIGTERM vs SIGKILL?

TERM asks politely and can be handled; KILL ends at once and cannot be caught.

Q. What does Ctrl+C send?

SIGINT.

Q. How do you list signals?

kill -l.

Mid level level

Q. How do you reload nginx config without downtime?

nginx -t, then systemctl reload nginx or kill -HUP master PID.

Q. What does exit code 137 mean?

Killed by SIGKILL (128+9), often OOM.

Q. How do you limit a command to 30 seconds?

timeout 30s CMD; exit 124 if it timed out.

Q. How do you clean up temp files when a script is interrupted?

trap 'rm -f "$tmp"' EXIT INT TERM.

Senior level

Q. A process is in D state and ignores kill -9. Explain.

It is blocked inside a kernel IO call; signals are delivered only when it returns. Fix the device or NFS (umount -f/-l), or reboot.

Q. Zombies are piling up. Root cause and fix?

The parent does not call wait(). Fix the code (handle SIGCHLD), restart the parent, or use an init like tini in containers.

Q. How does systemd stop a service?

Sends KillSignal (TERM) to the cgroup, waits TimeoutStopSec, then SIGKILL; KillMode controls which processes.

Q. Why use exec in a Docker entrypoint script?

So the app becomes PID 1 and receives SIGTERM directly instead of the shell swallowing it.

Try at home

  1. Write a script that prints 'tick' every second, prints 'bye' on Ctrl+C or TERM, and prints 'reloaded' on HUP. Test all three with kill.
  2. Run 'sleep 100' and find out the exit code when it is stopped by TERM, KILL and Ctrl+C. Explain each number.

Practise this chapter

Interview questions and answers

What may differ after TERM and KILL even though both make a process disappear?

A process can handle TERM and flush output, finish a transaction or remove its temporary file. KILL gives its code no opportunity to handle that signal. Verify that the target really exited, then compare log and data state before restarting it.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. Which signal gives a program an opportunity to run its own shutdown handler?

Answer and explanation

B. SIGTERM

TERM can be handled by the program. KILL and STOP cannot be caught for program-controlled cleanup.

2. A process row starts with Z. What happened?

Answer and explanation

A. It exited and its parent has not collected the status yet

Z means zombie, an exited child waiting for status collection. Reinspect the parent rather than trying to stop the child again.

3. kill returns status zero after you send TERM. What should you check next?

Answer and explanation

C. Whether the target process actually exited or handled the request differently

The send succeeded, but a program can handle TERM. Use a process or service check to see what happened afterward.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Ahmednagar Sugar Co-op is a sugar co-operative business in Ahilyanagar. Balasaheb Pawar looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 17

Easy Number of a signal

Know the names.

Print the number of the TERM signal by its name.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Polite stop

Let the app clean up.

Send the default terminate signal to PID 5520.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy All copies by name

Restart gunicorn cleanly.

Stop every process named exactly gunicorn and wait until they are gone.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Limit a command

Health check hangs.

Run curl -s https://example.com with a 5 second limit.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Name for a number

Exit code 137 seen.

Print the signal name for number 2.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Force kill

TERM did not work after 30 s.

Send SIGKILL to PID 5520.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Is it alive?

pid file check.

Check whether PID 3307 exists without sending a real signal.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Reload nginx by PID

New certificate at BIZ.

Send SIGHUP to PID 980 (the nginx master).

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Pause a process

Peak hour; pause the ETL.

Pause PID 9611 with a signal that cannot be caught.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Resume a process

Peak is over.

Resume the paused PID 9611.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Stop by full command

Celery workers have no fixed PIDs.

Send TERM to every process whose full command line matches 'rq worker'.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium End a user's processes

Intern leaving BIZ.

Send TERM to all processes of user trainee7.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Clean up on exit

Leftover temp files.

In a bash script, set a trap that runs rm -f /tmp/job.lock on EXIT.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Kick a terminal

Unknown user on pts/5.

Send KILL to everything on terminal pts/5.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard TERM then KILL

Nightly job must never hang.

Run ./report.sh with a 1h limit, and send KILL 10s after the first signal if still alive.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Ignore TERM

A critical step must not be interrupted.

Make the script ignore SIGTERM.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Find zombies

Defunct entries are piling up.

List stat, pid, ppid and cmd of all processes, then filter rows whose stat contains Z with awk on field 1.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

In short

  1. kill -l lists; names are safer than numbers.
  2. kill sends TERM; -9 only as the last step.
  3. HUP reloads daemons; STOP/CONT pause and resume.
  4. pkill/killall by name; preview with pgrep -a.
  5. timeout limits time (exit 124); trap cleans up.
  6. Zombies: fix the parent. D-state: fix the IO.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Stop a process right. Let apps clean up first. kill sends TERM by default. Use -9 only as last step. Polite then force.
Revision card: Stop a process right · Download PNG · Open card page
trap cleanup. Scripts must clean up always. trap runs code on a signal. EXIT covers normal and error exits. Remove the lock file.
Revision card: trap cleanup · Download PNG · Open card page
kill vs pkill. One PID you know. / Processes matching a pattern.
Comparison: kill vs pkill · Download PNG · Open card page
pkill vs killall. Pattern, user, terminal. / Exact process name.
Comparison: pkill vs killall · Download PNG · Open card page
TERM vs KILL?. TERM asks, app can clean up. KILL cannot be caught. Force after waiting.
Interview card: TERM vs KILL? · Download PNG · Open card page
Exit code 137?. 128 plus 9 means SIGKILL. Often the OOM killer. Name the signal.
Interview card: Exit code 137? · Download PNG · Open card page
Why send HUP to nginx?. Many daemons reload on HUP. No downtime for new config. Reload by PID.
Interview card: Why send HUP to nginx? · Download PNG · Open card page
Can you kill a zombie?. It is already dead. Fix or restart the parent. Find the parent.
Interview card: Can you kill a zombie? · Download PNG · Open card page

More cards: Ravi Sir's Cards.