2

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.

4 Answers 4

6

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.

1
9

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

2
  • 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
    – user274
    Oct 21, 2009 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, 2009 at 16:14
1

To background commands just add & after the command.

2
  • 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, 2009 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, 2009 at 16:44
1

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.

2
  • I like it however i am not allowed to install it (work servers)
    – user274
    Oct 21, 2009 at 16:12
  • I'm surprised it isn't already installed. Incredibly useful. Oct 21, 2009 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy