Linux on call · Chapter 19 of 30
Processes and signals
By the end of this chapter you can list what is running, find a program's process ID, and stop it politely, or for good when it will not listen. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Processes and the system
- 4 tasks
- Linux
- In the all labs plan
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.
Why you need this
The server feels slow and the logs keep growing. Ravi's note says why: a report worker that should have finished at 6 am is still adding up yesterday's orders, and a sync job is still waiting for a menu server that was switched off last month. Both belong to the ordering app, which must keep running. Every running program is a process, and ps lists them all:
ps -ef --forest
UID PID PPID C STIME TTY TIME CMD
student 1895 0 1 10:07 ? 00:00:00 script -qc bash -li /dev/null
student 1901 1895 1 10:07 pts/0 00:00:00 \_ sh -c bash -li
student 1902 1901 4 10:07 pts/0 00:00:00 \_ bash -li
student 1941 1902 99 10:07 pts/0 00:00:00 \_ ps -ef --forest
student 1 0 0 01:09 ? 00:00:02 /usr/bin/tini -- /opt/agent/entrypoint.sh
student 7 1 0 01:09 ? 00:00:04 node --no-opt -r /proc/.reset /opt/agent/agent.m
student 120 1 0 01:09 ? 00:00:13 /bin/bash /workspace/chapters/19-processes/bin/t
student 122 120 0 01:09 ? 00:00:09 \_ /bin/bash /workspace/chapters/19-processes/b
student 123 120 0 01:09 ? 00:00:06 \_ /bin/bash /workspace/chapters/19-processes/b
Your list will differ: other process IDs and times, and your own shell (here the first four lines) sits under node, the lab's agent. ps also cuts each line at the terminal's width, so you cannot even tell the two workers apart. Restarting the server would stop the workers, and the ordering app with them. You need to pick out single processes and tell each one exactly what to do.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Save the ordering app's PID in orders.pid, and keep the app running
Ravi wants to watch the ordering app, so save its PID in a file.
1. List the processes with their full command lines. Find the one that runs bin/tapri-orders.sh.
2. Use pgrep to print the PID of the ordering app only.
3. Save that number in orders.pid, in the chapter folder.
To pass: orders.pid has the ordering app's PID and nothing else, and exactly one ordering app is running when you press Check. Do not stop the ordering app.
- Task 2
Stop the report worker politely, with SIGTERM
Stop the report worker (bin/tapri-report.sh) with SIGTERM, so it has time to save its report.
1. Find the PID of the report worker.
2. Send it SIGTERM, with kill or pkill. Send it to the report worker only.
3. Run tail on logs/report.log. The last line says goodbye.
To pass: no report worker is running, and logs/report.log has the line report: got SIGTERM, saved the report, bye. The ordering app must keep running (task 1 checks it).
- Task 3
Stop the sync job, which ignores SIGTERM
Stop the sync job (bin/tapri-sync.sh). It ignores SIGTERM.
1. Send it SIGTERM first. Then run tail on logs/sync.log to see what it did.
2. Send it the signal that no program can catch. Send it to the sync job only.
3. List the processes again. The sync job is gone.
To pass: no sync job is running. The ordering app must keep running (task 1 checks it).
- Task 4 · interview question
Interview: the numbers of SIGTERM and SIGKILL
Interview question: what are the numbers of SIGTERM and SIGKILL?
1. Use kill -l to list every signal with its number.
2. Find TERM and KILL in the list.
3. Save both numbers in signals.txt, in the chapter folder, each next to its signal's name.
To pass: signals.txt has the number of SIGTERM and the number of SIGKILL.
What you will learn
- PID and PPID: every process has a number, and the number of the process that started it.
- pgrep: find processes by their command line, with whole lines.
- Signals: kill sends one; a program may catch it, or ignore it.
- The signal no program can catch, for a process that ignores the polite one.
More in Processes and the system
Build "Processes and signals" yourself
Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.