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

To transfer files from remove server I need to first to log in to it with a password and then input the command, to get files to local folder and my terminal should be opened until this transfer will be finished.

Is there a way to pass a public key to scp and start transfer via bash script? Then disown the bash script and turn off terminal?

What others options out there, to turn to it on the background?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

I am not sure to clearly understand what you are looking for. Do you want to run a Bash script on your local computer, that does nothing but call scp (which handles key authentication with ssh internals)? Or is it on the remote server that you want to run your script? Or do you, perhaps, want to use a particular public key?

Answer

To run a command or script in background, whilst preventing its process to stop when it receives a SIGHUP signal from your terminal (ie. when you close it), you may use nohup <command or script> &.


Assuming your script is named script.sh, you could run it with nohup script.sh &. The script could be running the following command (if you want to force a particular public key):

#!/bin/sh
scp -B -i /home/user/.ssh/id_rsa_key.pub /path/to/your/files username@remote.host:.

If not having to use a script file, or simplifying the command, is what you are after, you could always use your ~/.ssh/config file for that:

Host RemoteServer
  Hostname remote.host
  User username
  IdentityFile ~/.ssh/id_rsa_key.pub # If you need to force a particular key
  Port 22 # Not necessary either, port 22 is default

So the command to run could be as “simple” to run as nohup scp /path/to/your/files RemoteServer:. &

Feel free to tell me more about your configuration and/or what you exactly want to achieve, and I shall edit my answer to reflect the changes.

share|improve this answer

The easiest way is to use GNU screen. You don't need to do any configuration:

[local] $ screen -S foo ssh the-remote-host
[remote] $ /your/command

Then the terminal can be safely closed (actually it's hidden :). To get it back:

[local] $ screen -S foo -dr

And you can start the screen session on the remote host.

share|improve this answer
    
For more info about GNU screen, go to gnu.org/software/screen . You'll love it. ^_^ –  whjm Mar 28 '13 at 2:49

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.