Bg

From Wsms

Jump to: navigation, search

Note: This page should be titled bg (all lowercase). It is Bg due to technical limitations of Mediawiki.

bg let's you return a process to the background. You might want to use this when a background process requires user input.

Common Usage

Here is a trivial example using &, fg, bg, Control-Z and Control-C in the bash shell:

ggeller@roosevelt:~$ cat /dev/random > random.data &
[1] 3812
ggeller@roosevelt:~$ ls -lh random.data 
-rw-r--r-- 1 ggeller ggeller 71 2008-06-05 10:43 random.data
ggeller@roosevelt:~$ ls -lh random.data 
-rw-r--r-- 1 ggeller ggeller 79 2008-06-05 10:43 random.data
ggeller@roosevelt:~$ ls -lh random.data 
-rw-r--r-- 1 ggeller ggeller 79 2008-06-05 10:43 random.data
ggeller@roosevelt:~$ ls -lh random.data 
-rw-r--r-- 1 ggeller ggeller 87 2008-06-05 10:43 random.data
ggeller@roosevelt:~$ fg
cat /dev/random > random.data
(type Control-Z to stop cat)
[1]+  Stopped                 cat /dev/random > random.data
ggeller@roosevelt:~$ bg
[1]+ cat /dev/random > random.data &
ggeller@roosevelt:~$ ls -lh random.data 
-rw-r--r-- 1 ggeller ggeller 184 2008-06-05 10:43 random.data
ggeller@roosevelt:~$ fg
cat /dev/random > random.data
(type Control-C to kill cat)
ggeller@roosevelt:~$ ls -lh random.data 
-rw-r--r-- 1 ggeller ggeller 200 2008-06-05 10:44 random.data

Here is a more realistic example where a process needs to get user input and then go to the background.

ggeller@roosevelt:~$ nohup rsync -av mckinley:arthur.hold .
nohup: appending output to `nohup.out'
ggeller@mckinley's password: 

[1]+  Stopped                 nohup rsync -av mckinley:arthur.hold .
ggeller@roosevelt:~$ bg
[1]+ nohup rsync -av mckinley:arthur.hold . &

The nohup command at the first prompt tells bash to continue the rsync command as a seperate process even after I log out of the system. nohup tells me it is sending the output to nohup.out, a text file in the current directory. rsync sends a password prompt which reaches my terminal. I type my password for the remote machine, then Control-Z. At this point rsync is stopped. So, I issue the bg command to continue it in the background.

Why would I do all that? Because the rsync is going to take a long time copying the files across the network. I don't want to hang around or to leave myself logged into the computer when I have to go home.

see also

man bash

Personal tools