task 5.1 · chapter 5 of 8: processes, environment variables and path · 25 min
Jobs and variables
objective
Run a command in the background, pause and resume it, find it with ps and top; then set an environment variable, see which programs inherit it, and make it last with ~/.bashrc.
answers/5.1.yml~/.bashrcopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Lesson | Processes, environment variables and PATH |
|---|---|
| Where | your codespace’s terminal |
| Your answers | answers/5.1.yml |
new to this? step by stepbeginner
sleep 300 &,jobs,fg, Ctrl+Z,jobsagain,bg,kill %1.ps, thentop(q quits).echo $0,which python.- Challenge:
LAB_SITE=nyc,bash -c 'echo $LAB_SITE'(empty),export LAB_SITE, again (nyc). Then putexport LAB_SITE=nycin~/.bashrcand open a new terminal. - Fill in answers/5.1.yml, then
make check T=5.1.
more in meet the lab
Programs that run, and what they’re told
Processes and jobs
A process is a program that’s running. Each has a number, its PID (process ID), and a parent: the process that started it. Everything you type in the terminal is started by the shell, so the shell is its parent.
A command normally runs in the foreground: the shell waits for it to finish before it gives you the
prompt back. End a command with & and it runs in the background, and the shell asks for the next one
straight away. The shell calls the commands it’s keeping track of jobs:
| You type | Does |
|---|---|
cmd & | start cmd in the background |
jobs | list this terminal’s jobs, with their numbers |
fg | bring the newest job to the foreground (fg %2 for job 2) |
| Ctrl+Z | pause the job in the foreground |
bg | let a paused job carry on, in the background |
| Ctrl+C | stop the job in the foreground |
kill %1 | stop job 1 (kill 1234 stops PID 1234) |
ps lists processes; top shows them live, the busiest first.
in one sentence A process has a PID; & runs it in the background, and jobs, fg and bg move it around.
Environment variables
Every process carries a set of environment variables: names with values, like HOME=/home/vscode and
USER=vscode, that programs read to know where they are and how to behave. echo $HOME prints one.
A variable you set in the shell, LAB_SITE=nyc, belongs to that shell only. export marks it to be handed
down to every program the shell starts from then on. And nothing you set lasts beyond the terminal it was set
in: a new terminal starts from scratch and runs ~/.bashrc, so that’s where settings you always want go.
in one sentence export hands a variable down to the programs a shell starts; ~/.bashrc sets it in every new terminal.
A job, paused and resumed
$ sleep 300 &
[1] 10638
$ jobs
[1]+ Running sleep 300 &
$ ps
PID TTY TIME CMD
10608 pts/0 00:00:00 bash
10638 pts/0 00:00:00 sleep
10660 pts/0 00:00:00 ps
[1] is the job number and 10638 its PID; yours will differ. sleep 300 does nothing for 300 seconds,
which makes it a good process to practise on. ps with no options lists only this terminal’s processes: the
shell, the sleep, and ps itself.
Jobs, then the shell’s own facts
Start a job in the background, and list it:
run in the terminalsleep 300 & jobs psBring it to the foreground. The prompt doesn’t come back: the shell is waiting for it.
run in the terminalfgPress Ctrl+Z. The prompt comes back and the job is paused. Ask about it:
run in the terminaljobsThe one word
jobsuses for its state goes instopped_word.Let it carry on in the background, then stop it for good:
run in the terminalbg kill %1 jobsEvery process on the machine, live, the busiest at the top. Watch it for a few seconds, then find the key that quits (it’s in
top’s help: press h). That key goes intop_quit_key.run in the terminaltopTwo facts about your shell:
run in the terminalecho $0 which python$0is the name of the program you’re typing into: that’sshell_name.whichsays where a command lives; copy its answer intopython_path. Chapter 5.2 explains why it’s that one.
in one sentence Ctrl+Z pauses, bg resumes in the background, fg brings it back; top shows every process live.
A variable that sticks
- Set
LAB_SITEtonycin your terminal, and check thatecho $LAB_SITEprints it. - Now ask a child process, a second bash started by this one, what it sees:
bash -c 'echo $LAB_SITE'. Make the child see it too. - Open a new terminal (the + in the terminal panel):
LAB_SITEis gone. Make every new terminal start with it, exported.
Then run make check T=5.1. It opens a fresh bash of its own, the way a new terminal would.
hint
The single quotes in bash -c 'echo $LAB_SITE' stop your shell from filling in $LAB_SITE, so the child
does it. That’s the point.
hint
~/.bashrc is a file of commands that every new terminal runs first. Add one line to the end of it. >>
appends; one > would replace the whole file, so be careful.
show the solution
LAB_SITE=nyc
bash -c 'echo $LAB_SITE' # prints an empty line: not exported
export LAB_SITE
bash -c 'echo $LAB_SITE' # nyc
echo 'export LAB_SITE=nyc' >> ~/.bashrc
tail -n 1 ~/.bashrc
Or open ~/.bashrc in nano and add the line at the end. The change only applies to terminals opened after it;
source ~/.bashrc applies it to the one you’re in.
5 check: run it, then prove it
$ make check T=5.1when it's right, you'll see
╭─ task 5.1 · jobs and variables
│ ✓ the shell you're typing into (follow along): bash
│ ✓ which python a new terminal runs (follow along): ...
│ ✓ what jobs says after Ctrl+Z (follow along): ...
│ ✓ the key that quits top (follow along): q
│ ✓ a new terminal has LAB_SITE=nyc, from ~/.bashrc (challenge): nyc
│ ✓ and the programs it starts see it too: it's exported (challenge)
╰─ 6/6 checks passed verified