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.

I know there are two or more ways to run a job and place it in the background.

Right now I used ctrl-z to put a scp transfer in the background because I noticed it was going to take a while. How do I run the process in the background so I can write other commands while I wait until it's done?

Note: I can create a new terminal however in this case I SSH'd into 2 different servers so it's inconvenient to do this for each concurrent job I want in the background.

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

After you've used ^Z to STOP something, type "bg" to let it run in the background. "fg" will bring it back to front, as long as you haven't logged out.

share|improve this answer
add comment

It's important to note that while some processes take CTRL+Z to mean background, the default behavior is STOP, which does not continue execution. The safest way is to run the command with an & at the end, then run "disown" to force it to the background completely. You will not be able to foreground it again, however.

If you want an easy way to keep an eye on background tasks, consider learning about screen. As a basic lesson, type screen scp myfile server:/mydest, then type CTRL+A D to "detach" the screen session. You can recall this session by typing "screen -r", even after logging off the machine! :)

screen has many more advanced features, such as multiple windows, an active taskbar, etc. Here's a guide: link

share|improve this answer
    
I like this answer and i used screens in the past. However these are work servers and i am not allowed to install anything. They dont have it installed. What if i want to use & and not disown it? I am unsure how to bring it back to fg (i tried fg %1 and varients). I am testing with command man man –  acidzombie24 Oct 21 '09 at 16:11
1  
Most applications that support fg are in a STOP state, not a running state, when they are put into the background. That's not ideal for scp. :) Have you considered asking your sysadmins really nicely to install screen? We like coffee, it's a good bribe tactic. –  Kyle Smith Oct 21 '09 at 16:14
add comment

To background commands just add & after the command.

share|improve this answer
    
This is the method I use if i know i am just going to push something to the bg anyways. saves me some time. –  Grishanko Oct 21 '09 at 20:02
    
I know this is closed, but I just found out that you can redirect STD Error, and that allows commands to run in the background. This doesn't leave a process open, like screen would. Try this experiment, run this command: "nice -n 19 find / -type f -exec ls -al {} \; 2>&1 &" (whatever command that takes a long time) and then kill your session. Then log back in and check 'ps', it will still be running. –  CosmicQ Oct 26 '09 at 16:44
add comment

I would recommend running programs like scp in screen. The reason is that by putting the command in the background, it will dump output into your current terminal and you can't easily disconnect from an ssh session and reconnect to it in the middle of your job.

Run screen. You'll get new terminal in front of you. Run your process. Now press "ctrl-a d" and you should get a line saying

[Screen detached]

You can reconnect to your screen by running:

# screen -r

You should get your original terminal back.

share|improve this answer
    
I like it however i am not allowed to install it (work servers) –  acidzombie24 Oct 21 '09 at 16:12
    
I'm surprised it isn't already installed. Incredibly useful. –  David Pashley Oct 21 '09 at 16:17
add comment

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.