Linux: Start Command In Background

by on March 19, 2013 · 6 comments· LAST UPDATED February 13, 2014

in , , ,

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.

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsBash/ksh
Linux
Estimated completion time10m

Syntax

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 &
 

Examples

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:
jobs
Sample outputs:

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

Where,

  • [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:

7307
7324

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:
%JOB-ID
OR
fg JOB-ID
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:
%2
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:
bg
The grep command job is now running in the background.

Summary of all useful commands

DescriptionCommand
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
%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:
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 6 comments… read them below or add one }

1 [r4] March 29, 2013 at 10:11 pm

You can actually use the following form of kill command:
kill %1
to kill the first job in the list.

Reply

2 bkpsusmitaa August 22, 2013 at 5:38 pm

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)’)
fi

Reply

3 mengjia March 19, 2014 at 1:36 am

great, thanks for help

Reply

4 shankar August 25, 2014 at 5:42 am

thank you!!

Reply

5 John Mark Mobley October 24, 2014 at 6:58 pm
pi@raspberrypi ~ $ cat niceTest2
#!/bin/bash
# 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
  clear
  head -n 7 factorTemp.txt
  grep factor factorTemp.txt
  rm factorTemp.txt
  echo
  echo Press q to exit
  #sleep 10
  #CHAR=getkey with 10 second timeout
  read -s -n 1 -t 10 CHAR
  if [ "$CHAR" = "q" ]; then
    break
  fi
done
#sudo killall factor >/dev/null 2>&1
killall factor >/dev/null 2>&1

Reply

6 Vijay Kumar Kanta December 19, 2014 at 7:46 am

Very useful information to BASH users. Thanks Vivek!

Reply

Leave a Comment

Tagged as: , , , , , , , , ,

Previous Faq:

Next Faq: