bash job control basics


Quick Notes

cd /; sleep 1001 &
  [1] 733                        # shows job id and PID
cd /tmp; sleep 1002 &
  [2] 1045                       # shows job id and PID
cd /etc; sleep 1003
^Z                               # ctrl+Z sends job to background and stops it
  [3]+Stopped       sleep 1003
jobs                             # shows current jobs and statuses
  [1]   Running                 sleep 1001 &  (wd: /)
  [2]-  Running                 sleep 1002 &  (wd: /tmp)
  [3]+  Running                 sleep 1003 &
  [4]   Done                    jobs
                                 # the "+" indicates this is the last-used job
                                 # When a job is running out of a different directory than the present-working-directory, the 'wd' (working directory) is shown in the jobs output to clarify what/where is being worked on.
pwd
  /etc
fg                               # fg brings a job to the foreground (or %command)
                                 # if no job id is given it uses the most recent job (marked with '+')
  sleep 1003                     # the command for that job is output when a job is brought to the foreground
^Z                               # ctrl+Z sends job to background and stops it
  [3]+ Stopped      sleep 1003
bg                               # bg tells a job to run in the background
                                 # if no job id is given it uses the most recent job (marked with '+')
%3                               # %# is the same as fg # - brings to foreground.
  sleep 1003

wait %-; echo "Job done"         # wait until the last job is completed, then echo "Job done"
      

See also the table here for more Identifiers notation/meaning (e.g., %N %-, etc)

References