Linux: Start Command In Background

last updated in Categories , , ,

I am a new Linux command line user. How do I start or run command in the background so that I can access command prompt immediately?

A command that has been scheduled nonsequentially is called background process. You can not see the background processes on screen. For example, Apache httpd server runs in background to serve web pages. You can put your shell script or any command in background.




You can put a task (such as command or script) in a background by appending a & at the end of the command line. The & operator puts command in the background and free up your terminal. The command which runs in background is called a job. You can type other command while background command is running. The syntax is:

command &
script-name &
/path/to/command arg1 arg2 &
command-1 | command-2 arg1 &
command-1 | command-2 -arg1 -arg2 >/path/to/output &


Put the ls command in the background, enter:
$ ls *.py > output.txt &
Put the following find command in a background by putting a ‘&’ at the end of the command line:

find . -iname "*.mp3" > myplaylist.txt &

Sample outputs:

Fig.01: Linux background job in action (click to enlarge)
Fig.01: Linux background job in action (click to enlarge)

How do I see jobs running in the background?

Type the following command:
Sample outputs:

[1]-  Running                 find / -iname "*.c" 2> /dev/null > /tmp/output.txt &
[2]+  Running                 grep -R "hostNamed" / 2> /dev/null > /tmp/grep.txt &


  • [1] and [2] are job IDS.

To see process IDs for JOB IDs in addition to the normal information pass the -l option:
jobs -l
Sample outputs:

[1]-  7307 Running                 find / -iname "*.c" 2> /dev/null > /tmp/output.txt &
[2]+  7324 Running                 grep -R "hostNamed" / 2> /dev/null > /tmp/grep.txt &

To see process IDs only, enter:
jobs -p
Sample outputs:


How do I kill the jobs running in the background?

Use the kill command to kill process either gracefully or forcefully. The syntax is:

kill PID
kill -15 PID
kill -9 PID
killall process-Name-Here
killall -15 process-Name-Here
killall -9 process-Name-Here

See how to use killall command under Linux operating system for more information.

How do I bring process running in the background to the foreground?

The syntax is:
First, list the current jobs with jobs command, enter:
jobs -l
Sample outputs:

[1]-  7307 Running                 find / -iname "*.c" 2> /dev/null > /tmp/output.txt &
[2]+  7324 Running                 grep -R "hostNamed" / 2> /dev/null > /tmp/grep.txt &

To bring the job id #2 to the foreground, enter:
OR use fg command:
fg 2
Sample outputs:

grep -R "hostNamed" / 2> /dev/null > /tmp/grep.txt

To send back this job in the background hit CTRL-Z i.e. while holding the CTRL key, press z key. This will suspend the current foreground job. Type the following command to send back the job in the background:
%2 &
OR use bg command:
The grep command job is now running in the background.

Summary of all useful commands

To see which jobs are still running jobsjobs
jobs -l
ps aux
To put a command / script to the backgroundcommand &
/path/to/command &
/path/to/script arg1 &
To bring a background job to the foregroundfg n
To send a job to the background without canceling itbg n
%n &

Note: n == Job id (use jobs command to see job id)..

See also:


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

10 comment

  1. How can I send the message box generated from the following code to the background:
    title=$(gettext ‘SCANNING DEVICE’)
    text=$(eval_gettext ‘Looking for PPPoE Access Concentrator on $iface…’) &
    if test -n “$mmm” ; then
    mmode=$(gettext ‘(multi-modem mode)’)

  2. pi@raspberrypi ~ $ cat niceTest2
    # By:......... John Mark Mobley
    # Date:....... 10-23-2014
    # Purpose:.... Test the nice command.
    # Description: Start a number of jobs at different priorities and monitor
    # ............ how much %CPU each job gets.
    #sudo nice -n -20 factor 1111111111111111111 >/dev/null &
    #sudo nice -n -10 factor 1111111111111111111 >/dev/null &
    nice -n 0 factor 1111111111111111111 >/dev/null &
    nice -n 10 factor 1111111111111111111 >/dev/null &
    nice -n 19 factor 1111111111111111111 >/dev/null &
    #top -b -n 1|grep factor
    while true; do
      top -b -n 1 >factorTemp.txt
      head -n 7 factorTemp.txt
      grep factor factorTemp.txt
      rm factorTemp.txt
      echo Press q to exit
      #sleep 10
      #CHAR=getkey with 10 second timeout
      read -s -n 1 -t 10 CHAR
      if [ "$CHAR" = "q" ]; then
    #sudo killall factor >/dev/null 2>&1
    killall factor >/dev/null 2>&1
  3. My program MyCmd is designed to run indefinitely.
    Why doesn’t this work as expected, killing it after 60 secs?
    The output file never correctly contains the date+output+date

    date >>1.txt
    MyCmd >>1.txt &
    sleep 60
    pkill -USR1 MyCmd
    Date >>1.txt

    Still, have a question? Get help on our forum!