Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Ctrl+z sends the current job to background, but stops it.

And how to resume a stopped job?

share

locked by Chris S Jan 13 at 5:57

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

comments disabled on deleted / locked posts

5 Answers 5

up vote 17 down vote accepted

you can run "bg" to run it in the background.

"fg" moves it to the foreground

Note that bg and fg take job #s instead of PIDs, so if you've got multiple jobs running at once, use the "jobs" command to get the job numbers.

share
add comment

you can also start a program as a background job with an "&" on the command line.

e.g.

myprogram &

note that output (both stdout and stderr) will still go to the current tty, so it's generally a good idea to redirect to /dev/null or to a log file, like so:

myprogram > ~/program.log 2>&1 &

in either example, it's a background job like any other, so you can still bring it back to the foreground with 'fg' (but if you've redirected output you won't see much).

share
add comment

In bash, entering a "bg" puts the job into the background until it blocks needing input. It will continue to output to STDERR and STDOUT which might be unhelpful. You can enter "fg" to bring the job back to the foreground.

[adjuster@mx02 ~]$ cp ~/crap/* ~/crap2 
(Ctrl-Z)
[1]+  Stopped                cp ~/crap ~/crap2 

[adjuster@mx02 ~]$ cp ~/crap3/* ~/crap4
(Ctrl-Z)
[2]+  Stopped                cp ~/crap3/* ~/crap

[adjuster@mx02 ~]$ jobs
[1]+  Stopped                cp ~/crap ~/crap2 
[2]+  Stopped                cp ~/crap3/* ~/crap

[adjuster@mx02 ~]$ bg 1
[1]- cp ~/crap ~/crap2 &
[1]-  Exit 1                  cp ~/crap ~/crap2 

[adjuster@mx02 ~]$ fg 2
[adjuster@mx02 ~]$

So, that was starting a long copy job and suspending it, starting a second long copy job and suspending it, then putting the first copy job into the background and letting it run, followed by that first copy job exiting. Then I put the last copy job into the foreground and let it finish.

share
    
bg 1 and fg 1 don't work for me, but bg %1 and fg %1 do. –  Alexej Magura Nov 26 '13 at 17:11
add comment

nohup task.sh &

Runs in background, output goes to nohup.out in current directory. Continues to run when you log out.

share
add comment

Another option is the excellent screen utility, which can be used to run many processes at the same time, without having to keep a terminal open. It also allows for much easier interactivity than bg and fg.

share
add comment

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