Linux administration

Job Control

Nguyen Hai Chau
Vietnam National University

Job Control

  • Most shells offer job control
    • The ability to stop, restart, and background a running process
  • The shell lets you put & on the end of a command line to start it in the background
  • Or you can hit Ctrl+Z to suspend a running foreground job
  • Suspended and backgrounded jobs are given numbers by the shell
  • These numbers can be given to shell job-control built-in commands
  • Job-control commands include jobs, fg, and bg

jobs

  • The jobs builtin prints a listing of active jobs and their job numbers:
$ jobs
[1]- Stopped
[2]
Running
[3]+ Stopped

vim index.html
firefox &
man ls

jobs

  • Job numbers are given in square brackets
    • But when you use them with other job-control builtins, you need to write them with percent signs, for example %1
  • The jobs marked + and - may be accessed as %+ or %- as well as by number
    • %+ is the shell's idea of the current job — the most recently active job
    • %- is the previous current job

fg

  • Brings a backgrounded job into the foreground
  • Re-starts a suspended job, running it in the foreground
  • fg %1 will foreground job number 1
  • fg with no arguments will operate on the current job

bg

  • Re-starts a suspended job, running it in the background
  • bg %1 will background job number 1
  • bg with no arguments will operate on the current job
  • For example, after running gv and suspending it with Ctrl+Z, use bg to start it running again in the background

Exercise

  • a. Start a process by running man bash and suspend it with Ctrl+Z.
  • b. Run xclock in the background, using &.
  • c. Use jobs to list the backgrounded and stopped processes.
  • d. Use the fg command to bring man into the foreground, and quit from it as normal.
  • e. Use fg to foreground xclock, and terminate it with Ctrl+C.
  • f. Run xclock again, but this time without &. It should be running in the foreground (so you can't use the shell). Try suspending it with Ctrl+Z and see what happens. To properly put it into the background, use bg.