I am new Linux and Unix system bash/ksh user. How do I run jobs or scripts in the background on Linux/Unix-like systems? How can I run jobs in the background on bash or ksh or sh shell?
Job control is nothing but the ability to stop/suspend the execution of processes (command) and continue/resume their execution as per your requirements. This is done using your operating system and shell such as bash/ksh or POSIX shell.
|bg command details|
|Description||Runs jobs in|
|Estimated completion time||5m|
The bg command is part of Linux/Unix shell job control. The command may be available as both internal and external command. It resumes execution of a suspended process as if they had been started with &. Use bg command to restart a stopped background process.
Resumes suspended jobs in the current environment by running them as background jobs.
The basic syntax is as follows:
bg jobID1 jobID2 ... jobIDN
Understanding the job number (jobID)
There are a various ways to refer to a job in the shell. The character % introduces a job specification. The JobID can be a process ID (PID) number, or you can use one of the following symbol combinations:
- %Number : Use the job number such as %1 or %2.
- %String : Use the string whose name begins with suspended command such as %commandNameHere or %ping.
- %+ OR %% : Refers to the current job.
- %- : Refers to the previous job.
There are two other commands (key sequence) you need to know before you start using bg command.
Simply use the jobs command as follows to list all active jobs in the current bash/ksh/tcsh shell:
$ jobs -l
 6107 Running gedit fetch-stock-prices.py & - 6148 Running gnome-calculator & + 6155 Stopped ping cyberciti.biz
In this example, three jobs are output: gedit fetch-stock-prices.py (gui based text editor), gnome-calculator, and suspend/stopped ping cyberciti.biz command.
A job is suspended simply by using the Ctrl–Z short cut key sequence or using kill command or pkill command:
kill -s stop PID kill -s stop jobID pkill -stop PID
Putting is all tougher
In this example, you are going to run ping command command in foreground:
To suspend ping command job hit the Ctrl-Z key sequence. To list the active jobs, enter:
$ jobs -l
The output of the jobs command displays the following stopped job:
+ Stopped ping cyberciti.biz
Now, use the job number 3 to resume the ping cyberciti.biz job by entering:
+ ping cyberciti.biz & nixcraft@wks05:~$ 64 bytes from www.cyberciti.biz (220.127.116.11): icmp_req=4 ttl=53 time=264 ms 64 bytes from www.cyberciti.biz (18.104.22.168): icmp_req=5 ttl=53 time=250 ms 64 bytes from www.cyberciti.biz (22.214.171.124): icmp_req=6 ttl=53 time=251 ms 64 bytes from www.cyberciti.biz (126.96.36.199): icmp_req=7 ttl=53 time=251 ms 64 bytes from www.cyberciti.biz (188.8.131.52): icmp_req=8 ttl=53 time=267 ms
Here is another example that run a Unix shell script called update-mutual-fund.sh command:
~/scripts/scripts/financial/update-mutual-fund.sh --all --output=html ### To stop press CTRL + Z ## jobs -l #### ~/scripts/scripts/financial/update-mutual-fund.sh has job id # 6 ### Resume job id #6 bg %6
In this example, run gnome-calculator in background from the Terminal for calculation:
$ gnome-calculator &
To stop job ID # 1 (Or PID # 6517), type the following kill command:
$ kill -s stop %1
$ kill -s stop 6517
+ Stopped gnome-calculator
Once stopped you will not able to punch numbers into gnome-calculator. To resume stopped gnome-calculator, enter:
$ jobs -l
$ bg %1
$ jobs -l
$ bg %gnome-ca
+ gnome-calculator &
From the bash(1) command man page or type the following command:
$ help bg
bg: bg [job_spec ...] Move jobs to the background. Place the jobs identified by each JOB_SPEC in the background, as if they had been started with `&'. If JOB_SPEC is not present, the shell's notion of the current job is used. Exit Status: Returns success unless job control is not enabled or an error occurs.
A note about /usr/bin/bg and shell builtin
Type the following type command to find out whether bg is part of shell, external command or both:
$ type -a bg
bg is a shell builtin bg is /usr/bin/bg
In almost all cases you need to use the bg command that is implemented as a BASH/KSH/POSIX shell built-in. The /usr/bin/bg command can not be used in the current session. The /usr/bin/bg command operates in a different environment and does not share the parent bash/ksh’s shells understanding of jobs.
This tutorial is also available in a quick video format:
Share this tutorial on: