Move A Job Into The Background

Jobs run in the foreground unless otherwise specified. To launch a job in the background end the command line with an ampersand (&).

Example…
Run apropos shell > shell-commands in the background…
af@gaga:~$ apropos shell > shell-commands & <Enter>
[1] 18721
af@gaga:~$

The shell outputs the job number (here it is 1) and process ID (here it is 18721) and then returns the shell prompt. When the background job finishes it will return the job number, the command and the text “Done” indicating that the job has finished.

[1]+  Done                    apropos shell > shell-commands

To move a foreground job into the background one must suspend it (<Ctrl>-<Z>) and then type bg.

Example…
Start the text editor vi in the foreground, suspend it and then move it into the background…
af@gaga:~$ vi <Enter> <Ctrl>-<Z>

[1]+ Stopped vi
af@gaga:~$ bg <Enter>
[1]+ vi &

[1]+ Stopped vi
af@gaga:~$ <do something>
af@gaga:~$ fg <Enter>

This could be useful while editing a file using vi.

If one has many jobs suspended then specify the job to be put in in the background by giving its job number as an argument to bg

Example…
To run job 5 in the background type…
$ bg %5 <Enter>

This entry was posted in BSD, Bash, GNU/Linux, UNIX and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>