0

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?

Kenny Rasschaert
  • 9,045
  • 3
  • 42
  • 58
Somebody
  • 364
  • 1
  • 6
  • 17

1 Answers1

0

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.

Diti
  • 116
  • 5