Job control
In the terminalela --spec job-control
The prompt runs each command in front in a process group of its own, which has the terminal
while it runs. Ctrl-Z stops that command, its whole pipeline, and gives the prompt back; the
stopped command becomes a job in the list run jobs are in, numbered with them, with the same
notices. wait, fg and bg reach it by its number (wait 1, fg 1), and kill by
job(1), since a bare number to kill at the prompt is a process id (LANGUAGE.md, kill).
ela> vim notes.md^Zrun: 1 stopped vim notes.md — fg 1 to go backela> make test...ela> fgvim notes.md
- The line the command was on ends there, with status 148, as Ctrl-C ends it with 130: a
stopped command in a loop or a proc leaves the rest of the line undone. Ctrl-Z reaches a
command only: ela's own work (
time.sleep, a loop of code) and a command in$( )or underspingo on. - A job is stopped with the terminal's modes as it left them, which come back with it, and the prompt gets its own back at once, so an editor or a pager stopped in raw mode leaves a prompt that echoes.
fg 1,fg job(1),fg $x, or a barefgfor the job stopped, sent on or started last, brings a job to the front. A stopped one says its command, gets the terminal back and carries on (CONT); when it ends, its status is the line's, and a failure is said as a failed command's,exit 2. Ctrl-C reaches it, and Ctrl-Z stops it again.fgof arunjob prints the output held so far, then the rest as the job writes it, until it ends, and then its result aswaitgives it. Its output goes on to its file and its input stays/dev/null, so the shell keeps the terminal: Ctrl-C ends the job, and Ctrl-Z leaves it running behind the prompt,run: x running make test — fg $x to go back, its output held again from there. Whatfgshowed is not printed again bywait;$x.outputis all of it.bg 1, or a barebgfor the job stopped last, lets a stopped job carry on behind the prompt, as in every shell: its output goes straight to the terminal, among the prompt's lines, and a line says when it ends, as for arunjob. A job behind the prompt that reads the terminal, or changes its modes, is stopped by the system, and the next prompt says so:run: 1 stopped sudo apt update (wants the terminal) — fg 1.wait 1of a stopped job is an error, since it would never end.killtakesjob(1)for a job (LANGUAGE.md, kill).
jobs lists every job of the session not yet waited for: its number, running, stopped, done or
failed with its exit code, its command, the variable holding it, and how long it has run. A job
is stopped when a command of it is, one that made a process group of its own (setsid) too, as
the bar counts it; (wants the terminal) follows when it stopped as it read or set the terminal.
ela> jobs 1 stopped vim notes.md 0:12 2 running make test x 0:42 3 failed npm run lint lint 0:03 (exit 2, not waited)
fg, bg and jobs work at the prompt only.
- In a script. A script has no job control: Ctrl-Z stops the whole script, as it stops any
program.
fg,bgorjobsthere runs a program of that name onPATH, and with none it is an error that says to start the work withx = run cmdand wait for it withwait $x. - At the prompt. The three come before a program of the same name on
PATH; its path runs the program:/usr/local/bin/jobs. jobsas a name.jobsalone on a line, or followed by its arguments, is the command.jobs = ...is an assignment and$jobsa variable, at the prompt and in a script alike:
jobs = []for host in $hosts jobs = list.append($jobs, run deploy($host))end forwait $jobs