To create an job, just append a single &
after the command:
$ sleep 10 &
[1] 20024
You can also make a running process a job by pressing Ctrl + z:
$ sleep 10
^Z
[1]+ Stopped sleep 10
To bring the Process to the foreground, the command fg
is used together with %
$ sleep 10 &
[1] 20024
$ fg %1
sleep 10
Now you can interact with the process. To bring it back to the background you can use the bg
command. Due to the occupied terminal session, you need to stop the process first by pressing Ctrl + z.
$ sleep 10
^Z
[1]+ Stopped sleep 10
$ bg %1
[1]+ sleep 10 &
Due to the laziness of some Programmers, all these commands also work with a single %
if there is only one process, or for the first process in the list. For Example:
$ sleep 10 &
[1] 20024
$ fg % # to bring a process to foreground 'fg %' is also working.
sleep 10
or just
$ % # laziness knows no boundaries, '%' is also working.
sleep 10
Additionally, just typing fg
or bg
without any argument handles the last job:
$ sleep 20 &
$ sleep 10 &
$ fg
sleep 10
^C
$ fg
sleep 20
$ sleep 10 &
[1] 20024
$ kill %1
[1]+ Terminated sleep 10
The sleep process runs in the background with process id (pid) 20024
and job number 1
. In order to reference the process, you can use either the pid or the job number. If you use the job number, you must prefix it with %
. The default kill signal sent by kill
is SIGTERM
, which allows the target process to exit gracefully.
Some common kill signals are shown below. To see a full list, run kill -l
.