Processes and signals

INPUT · Slides

Run it in the background (`&` and `jobs`)

01 / 08

The trouble of nothing coming back

You have met this already.

~ $ sleep 60

From the moment you type it, the prompt does not come back. For sixty seconds you can type nothing.

Nothing is broken. Type one command and the shell is built to wait until it finishes. That state of being in front is called the foreground.

The trouble comes with work that takes time.

  • packing up a big file
  • watching a log go by
  • running a server

Losing a terminal for the duration is a waste.

What this lesson does is line up jobs in one terminal.

02 / 08

Send it behind with &

Put & at the end of the line and the shell stops waiting.

~ $ sleep 60 &[1] 83~ $

The prompt came straight back, and sleep is still running. That state is called the background.

The [1] 83 that appeared is two numbers.

The numberNameWho gave it
[1]the job numberyour shell
83the PIDthe whole of Linux

This is today's crux. The two are different numbers.

The job number means "the first job this shell is holding", counting up from 1. Open another terminal and it has a [1] too.

The PID is last lesson's number, unique across the machine.

03 / 08

See what you handed over with jobs

What you sent behind you is listed by jobs.

~ $ sleep 60 &[1] 83~ $ sleep 90 &[2] 84~ $ jobs[2]+  Running                    sleep 90[1]-  Running                    sleep 60

The newest comes first, the opposite way round from ps.

The word in the middle is the state.

ShownMeaning
Runningrunning
Stoppedpaused (not gone)
Donefinished

The mark after the number means something too.

  • + — the job currently pointed at (chosen when you leave the number out)
  • - — the next in line

When you want the PIDs too, add -l.

~ $ jobs -l[2]+  84 Running                 sleep 90[1]-  83 Running                 sleep 60

The good thing about jobs is that only what you handed over is listed, with no hunting in ps.

04 / 08

%1 is a number inside the shell only

To point at a job number, put a % in front.

~ $ kill %1     stop job 1~ $ kill 83     stop PID 83

Both stop the same thing, and they point differently.

%1  -> ask your shell to turn it into a PID83  -> tell Linux directly

So %1 means nothing outside that shell. Type kill %1 in another terminal and your job does not stop.

%1 is easier to type: no looking up a PID, and short.

Who you wantHow to point
something you sent behind%1, %2
something in another terminalthe PID (found with ps)
everything of a namekillall name

Use %number for what you threw with &, and the PID for everything else.

05 / 08

Pause with Ctrl-Z, then bg

The awkward case is typing it and forgetting the &. Typing it again is a waste.

Then you press Ctrl-Z.

~ $ sleep 60^Z[1]+  Stopped                    sleep 60~ $

The prompt came back, and sleep is paused and still there (Stopped). It has not gone.

From there it forks two ways.

bg %1    keep it running behind you (as if you had typed &)fg %1    bring it back in front and wait for it

bg is background and fg is foreground.

~ $ bg %1[1] sleep 60~ $ jobs[1]+  Running                    sleep 60

Stopped became Running.

Ctrl-Z then bg, two moves, sends it behind you without typing it again. That one really does save you.

06 / 08

Ctrl-C and Ctrl-Z are different

Let us put the two similar ones side by side, in the language of signals from last lesson.

KeySignal sentWhat happens
Ctrl-CINTit finishes (gone)
Ctrl-ZTSTPit pauses (stays)

Ctrl-C cancels, Ctrl-Z pauses.

While paused, the process makes no progress at all. A program counting stops counting. A clock stops ticking.

Stopped   -> breathing, and not moving

When you decide you no longer want it, do not leave it sitting paused; get rid of it.

~ $ jobs[1]+  Stopped                    sleep 60~ $ kill %1

kill reaches paused things too.

And to bring it back, use bg or fg. kill -CONT makes it move as well, and then the shell's list stays saying Stopped, which no longer matches.

07 / 08

Voices from behind cut in

Something sent behind you still writes to the screen without apology.

~ $ sh chatty.sh &[1] 90~ $ lshello             <- cut in from behindmemo.txt

Characters appear while you type, which is hard to read and a source of mistyping.

So the etiquette is this.

~ $ sh chatty.sh > voice.txt 2>&1 &

Catch it in a file first, then send it behind you. You can read it calmly afterwards with cat voice.txt. You used this shape last lesson too.

When you want to wait for it to finish, there is wait.

~ $ sh heavy.sh > log.txt &~ $ wait~ $ echo finished

wait waits for everything behind you. "Throw them all and wait at the end" is much used in the scripts of chapter 11.

08 / 08

Now have a go

This is everything for the lesson.

command &         run it behind youjobs              list what you handed overjobs -l           with the PIDsfg %1             bring it in front and waitbg %1             keep it running behind youkill %1           stop it by job numberwait              wait for everything behind you

And there are two keys.

Ctrl-Z   pause what is running (it stays)Ctrl-C   finish what is running (it goes)

You use counting tools alongside.

~ $ jobs | wc -l > count.txt

The output of jobs is just lines too, so you can hand it to wc or grep. Chapters 4 and 5 apply as they are.

Now, practice getting yourself out of a terminal that will not come back.