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 | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | Bash/ksh Linux |
Time | 10m |
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:
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
Description | Command |
---|---|
To see which jobs are still running jobs | jobs jobs -l ps aux |
To put a command / script to the background | command & /path/to/command & /path/to/script arg1 & |
To bring a background job to the foreground | fg n %n |
To send a job to the background without canceling it | bg n %n & |
Note: n == Job id (use jobs command to see job id)..
See also:
- Putting jobs in background from the Linux shell scripting tutorial.
- Command examples pages: jobs command, bg command, and fg command
- Man pages: ksh(1)
🐧 10 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
You can actually use the following form of kill command:
kill %1
to kill the first job in the list.
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
great, thanks for help
thank you!!
Very useful information to BASH users. Thanks Vivek!
Thank you very much,
Really helpful :)
That is what I was looking for. Thanx a lot
Hi,
thanks
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