Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This is the output:

[USER@SERVER ~] ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.037 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.024 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.026 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.026 ms
^Z
[1]+  Stopped                 ping localhost
[USER@SERVER ~] jobs
[1]+  Stopped                 ping localhost
[USER@SERVER ~] bg %1
[1]+ ping localhost &
64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.034 ms
[USER@SERVER ~] 64 bytes from localhost (127.0.0.1): icmp_seq=7 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_seq=8 ttl=64 time=0.032 ms

[USER@SERVER ~] ^C
[USER@SERVER ~] ^C
[USER@SERVER ~] 64 bytes from localhost (127.0.0.1): icmp_seq=9 ttl=64 time=0.031 ms
^C
[USER@SERVER ~] 64 bytes from localhost (127.0.0.1): icmp_seq=10 ttl=64 time=0.031 ms
64 bytes from localhost (127.0.0.1): icmp_seq=11 ttl=64 time=0.028 ms
ki64 bytes from localhost (127.0.0.1): icmp_seq=12 ttl=64 time=0.030 ms
ll %64 bytes from localhost (127.0.0.1): icmp_seq=13 ttl=64 time=0.031 ms
1
[1]+  Terminated              ping localhost
[USER@SERVER ~] 

of:

1) I start to ping localhost
2) CTRL+Z
3) bg %1
4) CTRL+C doesn't work.
5) I have to type "kill %1" to kill it..


What is the real-life use of the "bg" command? Where is it used in the real world?

share|improve this question
    
My most common real world use is 1 $emacs file.txt ::blink blink $@#*(&%#$@(*%@$(*%&@#:: $^Zbg ::aahhhh...:: –  dmckee Mar 6 '12 at 17:58
1  
Note that you don't necessarily have to type kill %1; if you prefer, you could type fg %1 to bring it back to the foreground, and then use Ctrl+C. (Not that there's much reason to prefer one over the other; both just send an "interrupt" signal.) –  ruakh Mar 6 '12 at 19:27

2 Answers 2

up vote 13 down vote accepted

You use bg normally to run programs in the background, which has no console interaction, like most program with a graphical user interface.

Example: You wanted to run xterm & but forgot the & to run the terminal emulator in the background. So you stop the (blocking) foreground xterm process with Ctrl-Z and continue it in the background with bg.

If you want to send Ctrl-C to a background process, put it first with fg in the foreground again (or use kill -2 %1).

share|improve this answer
    
Not only forgetting, you could even have originally wanted to peek at some graphical document you had lying around (let's say an OpenDocument Text file), you fire LibreOffice for the short job, but meanwhile you start working on some report that you want to write using LibreOffice and, even if you start it from another terminal, the first one, where you only wanted to peek at a file, will remain "blocked" by LibreOffice until the report is closed — so, here, you can use SIGTSTP (Ctrl+Z) and bg to add the & after having started the process! –  njsg Mar 6 '12 at 15:35

I often change directory to where a lot of files, which belong together, and open all of them with one program:

cd /a/b/c eog . eog is Eye of Gnome, a picture viewer

cd /x/b/c gedit *.html

While running, I recognize that I have a question which could be better answered from the commandline. So I interrupt, and bring the program into the background:

Ctrl+Z

bg 1

Now I can invoke the command from the shell without opening a new shell and navigating again to that directory.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.