online posts 16
linux and the command line 101

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.

you edit, in the labanswers/5.1.yml~/.bashrc

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

given
LessonProcesses, environment variables and PATH
Whereyour codespace’s terminal
Your answersanswers/5.1.yml
new to this? step by stepbeginner
  1. sleep 300 &, jobs, fg, Ctrl+Z, jobs again, bg, kill %1.
  2. ps, then top (q quits). echo $0, which python.
  3. Challenge: LAB_SITE=nyc, bash -c 'echo $LAB_SITE' (empty), export LAB_SITE, again (nyc). Then put export LAB_SITE=nyc in ~/.bashrc and open a new terminal.
  4. Fill in answers/5.1.yml, then make check T=5.1.

more in meet the lab

1 explain

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 typeDoes
cmd &start cmd in the background
jobslist this terminal’s jobs, with their numbers
fgbring the newest job to the foreground (fg %2 for job 2)
Ctrl+Zpause the job in the foreground
bglet a paused job carry on, in the background
Ctrl+Cstop the job in the foreground
kill %1stop 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.

2 show

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.

3 follow along

Jobs, then the shell’s own facts

  1. Start a job in the background, and list it:

    run in the terminal
    sleep 300 &
    jobs
    ps
    
  2. Bring it to the foreground. The prompt doesn’t come back: the shell is waiting for it.

    run in the terminal
    fg
    
  3. Press Ctrl+Z. The prompt comes back and the job is paused. Ask about it:

    run in the terminal
    jobs
    

    The one word jobs uses for its state goes in stopped_word.

  4. Let it carry on in the background, then stop it for good:

    run in the terminal
    bg
    kill %1
    jobs
    
  5. Every 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 in top_quit_key.

    run in the terminal
    top
    
  6. Two facts about your shell:

    run in the terminal
    echo $0
    which python
    

    $0 is the name of the program you’re typing into: that’s shell_name. which says where a command lives; copy its answer into python_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.

4 challenge

A variable that sticks

  1. Set LAB_SITE to nyc in your terminal, and check that echo $LAB_SITE prints it.
  2. 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.
  3. Open a new terminal (the + in the terminal panel): LAB_SITE is 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.1

when 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