task 5.2 · chapter 5 of 8: processes, environment variables and path · 20 min
Runaway
objective
A process is eating a whole CPU and ignores a polite kill. Find it, stop it for good. Then make a command of your own that runs from any folder, by putting its folder on your PATH.
answers/5.2.yml~/tools/~/.bashrcopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Lesson | Processes, environment variables and PATH |
|---|---|
| Set it up | make break T=5.2 |
| Your command | files/labinfo: a two-line script, not yet runnable |
| Your answers | answers/5.2.yml |
new to this? step by stepbeginner
make break T=5.2, thentop: the busy process is at the top. Note its PID.kill <PID>, check withps -p <PID>. Still there? A signal it can’t ignore.mkdir -p ~/tools,cp files/labinfo ~/tools/,chmod +x ~/tools/labinfo.- Add
export PATH="$HOME/tools:$PATH"to~/.bashrc; in a new terminal,labinfoworks from anywhere. - Fill in answers/5.2.yml, then
make check T=5.2.
more in meet the lab
Signals, and how the shell finds a command
kill sends a signal
kill doesn’t kill. It sends a signal, a short message, and the process decides what to do with it:
| Signal | Number | Means | Can the process ignore it? |
|---|---|---|---|
TERM | 15 | please stop: tidy up and exit. What kill sends by default | yes |
INT | 2 | interrupted: what Ctrl+C sends | yes |
HUP | 1 | the terminal hung up; many servers read it as reload your config | yes |
KILL | 9 | stop now. The kernel ends it without asking | no |
Try TERM first: a program that gets it can save its work. KILL (kill -9) is for the ones that won’t
listen.
in one sentence kill sends TERM, which a process may ignore; KILL can’t be ignored, so save it for last.
PATH
When you type ls, the shell doesn’t search the whole disk. It looks through a list of folders, in order, and
runs the first ls it finds. That list is the environment variable PATH, with a : between folders.
which ls says which one it found.
To make a command of your own runnable from anywhere, put it in a folder and put that folder on PATH. Put
your folder first ($HOME/tools:$PATH) and your command wins over any other with the same name.
in one sentence PATH is the list of folders the shell searches for commands, first match wins.
PATH, one folder per line
$ echo $PATH | tr : '\n'
/home/vscode/.venvs/lab/bin
/home/vscode/.local/bin
/home/vscode/.atuin/bin
/usr/local/python/current/bin
...
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
tr : '\n' swaps each : for a new line, so the list is readable. The first folder is the lab’s Python
venv, which is why which python in task 5.1 found that python and not the system’s. Python 101 explains
venvs.
Make a command of your own
Try the script where it is:
run in the terminalcat files/labinfo files/labinfoPermission denied: task 4.2’s problem.
Give it a home, and make it runnable:
run in the terminalmkdir -p ~/tools cp files/labinfo ~/tools/ chmod +x ~/tools/labinfo ~/tools/labinfoFrom another folder, by name alone, it isn’t found yet:
run in the terminalcd / labinfo cd -bash: labinfo: command not found:~/toolsisn’t on PATH.
in one sentence A command runs by name only if its folder is on PATH, and only if it has x.
Stop the runaway, then fix PATH
make break T=5.2starts a program called labhog that keeps one CPU at 100%. Find its PID (that’shog_pid) and stop it. The polite way first, then, when that doesn’t work, the way that can’t be ignored. The signal that worked goes insignal_that_worked.- Make every new terminal find
labinfofrom any folder.
Then make check T=5.2.
hint
top puts the busiest process first, with its PID in the first column. ps -eo pid,pcpu,comm --sort=-pcpu | head says the same thing in one go.
hint
After a kill, ps -p <PID> shows whether it’s still there. labhog ignores TERM, INT and HUP.
hint
PATH is set the same way as LAB_SITE in task 5.1: one export line at the end of ~/.bashrc, with your
folder in front of the old PATH.
show the solution
ps -eo pid,pcpu,comm --sort=-pcpu | head -n 3 # labhog, at the top
kill 41234 # its PID: yours is different
ps -p 41234 # still running: it ignores TERM
kill -9 41234
ps -p 41234 # gone
echo 'export PATH="$HOME/tools:$PATH"' >> ~/.bashrc
hog_pid: 41234
signal_that_worked: KILL
Open a new terminal and cd /; labinfo. The double quotes in the export line matter: they keep the
whole PATH as one value, even if a folder in it ever has a space in its name.
5 check: run it, then prove it
$ make check T=5.2when it's right, you'll see
╭─ task 5.2 · runaway
│ ✓ the runaway's process ID (challenge): ...
│ ✓ labhog is stopped (challenge)
│ ✓ the signal that stopped it (challenge): ...
│ ✓ a new terminal finds labinfo in ~/tools, from any folder (challenge): /home/vscode/tools/labinfo
│ ✓ and labinfo runs (challenge)
╰─ 5/5 checks passed verified