scp as a background process

To execute any linux command in background we use nohup as follows:

$ nohup SOME_COMMAND &

But the problem with scp command is that it prompts for the password (if password authentication is used). So to make scp execute as a background process do this:

$ nohup scp file_to_copy user@server:/path/to/copy/the/file > nohup.out 2>&1

Then press ctrl + z which will temporarily suspend the command, then enter the command:

$ bg

This will start executing the command in backgroud

Advertisements

34 thoughts on “scp as a background process

  1. Hi man,

    I’d like to thank you for your greate help regarding scp with password authentication vs. background copying!!

    Viktor

  2. @c: After you enter the command it should ask you for password.
    For eg:

    $ nohup scp file_to_copy user@server:/path/to/copy/the/file > nohup.out 2>&1

    After this command it should prompt you for password.

  3. If you have more that one job running you can run jobs to see the status

    $ jobs
    $ %job_number_1
    $ %job_number_2
    $ %job_number_3
    $ %job_number_4

    You can run bg %job_number or fg %job_number to swap jobs to the background or foreground

    $ bg %job_number

  4. another variant is the use of the ‘screen’ command:

    $ screen scp file_to_copy user@server:/path/to/copy/the/file

    after that screen opens a new terminal, with a new shell & the command excecuted. now you should press:

    ctrl + a

    and after that you can press

    d

    to detatch the screen session. the session will resists in the background and you can logout. the main advantage of this kind of backgrounding is, that you can reconnect to the session anytime with a:

    $ screen -r

    hope this helps someone πŸ˜‰

  5. another variant is the use of the ‘screen’ command:

    $ screen scp file_to_copy user@server:/path/to/copy/the/file

    after that screen opens a new terminal, with a new shell & the command excecuted. now you should press:

    ctrl + a

    and after that you should press

    d

    to detatch the screen session. the session will resists in the background and you can logout. the main advantage of this kind of backgrounding is, that you can reconnect to the session anytime with:

    $ screen -r

    hope this helps someone πŸ˜‰

  6. I am trying the same but when i exit my putty session or putty session gets closed due to timeout, it stops scping. Also i do not see any jobs once i re-login. But if i do “ps -ef|grep scp” i can see the scp process there but it is hung as it is not scping anything.

    Any suggestion in this case please ?

  7. Kunal,
    This is excellent. I appreciate this very much. I have couple of questions.
    1. I need to move approximately 450GB(10 files 45gb each) from the remote server. How could I run only ONE scp command and get multiple files at a time.
    Note: to accomplish the same result(get in parallel), I had to open several Linux sessions and run commend manually to get one file at a time.
    2. is it possible to run with user name and password? I tried the following but did not work.
    a. /usr/bin/scp oracle:’password’@prodrac01:/datapump/expdp_prod_20160329* . > nohup.out 2>&1
    error: ssh_exchange_identification: Connection closed by remote host
    b. sshpass -p ‘password’ /usr/bin/scp oracle@prodrac01:/datapump/expdp_prod_20160329* . > nohup.out 2>&1
    error: -bash: sshpass: command not found

    Thank you very much.
    Ramesh.

  8. what if I wanted to see the % completion status of the scp when scp is running backgroung using nohup option?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s