online posts 16
linux and the command line 101

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.

you edit, in the labanswers/5.2.yml~/tools/~/.bashrc

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
LessonProcesses, environment variables and PATH
Set it upmake break T=5.2
Your commandfiles/labinfo: a two-line script, not yet runnable
Your answersanswers/5.2.yml
new to this? step by stepbeginner
  1. make break T=5.2, then top: the busy process is at the top. Note its PID.
  2. kill <PID>, check with ps -p <PID>. Still there? A signal it can’t ignore.
  3. mkdir -p ~/tools, cp files/labinfo ~/tools/, chmod +x ~/tools/labinfo.
  4. Add export PATH="$HOME/tools:$PATH" to ~/.bashrc; in a new terminal, labinfo works from anywhere.
  5. Fill in answers/5.2.yml, then make check T=5.2.

more in meet the lab

1 explain

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:

SignalNumberMeansCan the process ignore it?
TERM15please stop: tidy up and exit. What kill sends by defaultyes
INT2interrupted: what Ctrl+C sendsyes
HUP1the terminal hung up; many servers read it as reload your configyes
KILL9stop now. The kernel ends it without askingno

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.

2 show

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.

3 follow along

Make a command of your own

  1. Try the script where it is:

    run in the terminal
    cat files/labinfo
    files/labinfo
    

    Permission denied: task 4.2’s problem.

  2. Give it a home, and make it runnable:

    run in the terminal
    mkdir -p ~/tools
    cp files/labinfo ~/tools/
    chmod +x ~/tools/labinfo
    ~/tools/labinfo
    
  3. From another folder, by name alone, it isn’t found yet:

    run in the terminal
    cd /
    labinfo
    cd -
    

    bash: labinfo: command not found: ~/tools isn’t on PATH.

in one sentence A command runs by name only if its folder is on PATH, and only if it has x.

4 challenge

Stop the runaway, then fix PATH

  1. make break T=5.2 starts a program called labhog that keeps one CPU at 100%. Find its PID (that’s hog_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 in signal_that_worked.
  2. Make every new terminal find labinfo from 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.2

when 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