LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-10-2017, 07:17 AM   #1
Haalle
LQ Newbie
 
Registered: Jan 2017
Posts: 3

Rep: Reputation: Disabled
Script stops when scp loose connection


Hi all,

I have a small script that copies some files from a local machine to a remote every ten minutes or so. Most of the time it works fine, but once in a while scp looses connection and stops and the script hangs. I am running ssh-agent in bash-mode.

What I do is something like this:

while true; do
scp file user@server: path
sleep 600
done

and I just want scp to close down if there is an error, so that the script can continue.

Any help will be much appreciated!
 
Old 01-10-2017, 07:22 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
probably
scp .... || true
will help

but you may try to use crontab instead of while loop
 
1 members found this post helpful.
Old 01-10-2017, 07:29 AM   #3
Haalle
LQ Newbie
 
Registered: Jan 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
It certainly looks like crontab is what I want to use. I'll give it a try, thanks!
 
Old 01-10-2017, 07:48 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by Haalle View Post
Code:
while true; do
scp file user@server: path
sleep 600
done
and I just want scp to close down if there is an error, so that the script can continue.
Do you want the script to try to copy again immediately if scp loses the connection?

Either way, if the script is copying the same file over and over again, you may want to try rsync instead. The utility rsync runs over SSH but transfers only the changes. So if you are resuming transfer of a file after the connection is lost, it will pick up where you left off. scp cannot do that.

Code:
rsync -a file user@server:path/
 
1 members found this post helpful.
Old 01-13-2017, 04:43 AM   #5
Haalle
LQ Newbie
 
Registered: Jan 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
I is indeed the same files that I transfer, but it is not important that the file transfer is resumed - it is ok to wait for the next transfer after 10 minutes.

I am not sure whether it is scp or ssh-agent that cuts the connection once in a while. If it is ssh-agent, I guess using rsync will not solve the problem. But I will try rsync also and see it there is a difference.

For now, It looks like
Code:
scp -B -o ConnectTimeout=30 file user@server:path
also works. If scp prompts for a password and stops, the connection is cloes after 30 seconds and the script can resume. The -B option stops password prompt. I have not tested yet if it is -B or -o that solves the problem.
 
Old 01-13-2017, 04:53 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
The agent is only used in establishing the initial connection. Specifically it is involved in the authentication. So the disconnection is happening for some other reason, unless the initial connection is not actually happening in the time alloted. You can throw a -v in there temporarily to increase the amount of information it shows you.

Since you are copying the same file again and again, it looks like rsync is the way to go. It actually runs over SSH these days so it can use keys and the agent. If you need to specify some weird options or specific keys then that can be passed to rsync

Code:
rsync -e 'ssh -i ~/.ssh/some_key_rsa' -a -v file user@server:path/
You can also make the configuration options permanent by putting them in a ~/.ssh/config entry for the target host.

Code:
Host server
        Hostname server.example.com
        User user
        IdentityFile ~/.ssh/some_key_rsa
        ConnectTimeout 30
 
Old 01-13-2017, 04:54 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
if it was the same file, I would try rsync instead of scp (too late)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ubuntu Samba File Server - Mapped Windows Drives Loose Connection ablundon Linux - Networking 5 12-30-2014 02:54 PM
CentOS 6.5 wan interface periodically loose connection r1sh Linux - Networking 1 11-06-2014 04:37 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
Loose Connection to Linux Server RHEL 3 justinhickey Linux - Newbie 5 09-17-2009 03:54 PM
Samba - Mapped network drives loose connection dtournas Linux - Networking 1 10-04-2004 01:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration