Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On a CentOS setup I have a script like this:

#! /bin/bash
mysqldump --all-databases | gzip > /dumps/sql`date +"%Y-%m-%d-%$
scp `ls -t | head -1`  root@backup:/media/storage/backup/

The machine is configured with an authorized ssh key so a password is not necessary. If I run it (/bin/bash it works as intended, but in a cronjob only the mysqldump is carried out. The scp command fails.

Where do I look to fix this?

share|improve this question
up vote 1 down vote accepted

It sounds like scp doesn't pick up your key pair from ~/.ssh directory. Try this:

env HOME=/path/to/your/home scp -v $(ls -t | head -1) root@backup:/media/storage/backup/ > /tmp/scp.log 2>&1

If it doesn't work, post the scp.log here.

share|improve this answer
Thx, I'll look into that. – Lennart Sep 6 '12 at 13:49
the scp.log says: debug1: Sending command: scp -v -t /media/storage/backup/ dumps: not a regular file Sink: ^Ascp: dumps: not a regular file Where 'dumps' is the folder the script is located in. Apparently the 'ls -t | head -1' part doesn't work. Authentication works ok. – Lennart Sep 12 '12 at 8:30
Try using scp -r to copy a folder. – quanta Sep 12 '12 at 8:47
There's about 30 backup files in the folder, so I'd prefer to just copy 1 :-) Problem was the path; if I specify the full path it works. Not the cleanest solution but for now it works. – Lennart Sep 12 '12 at 9:02
It does pick up the key btw, even without home folder specified. – Lennart Sep 12 '12 at 9:03

In many cases scripts which work from the command line but not from cron fail due to environment issues. If you are using ssh agent or otherwise setting information scp needs to function in your environment, you'll need to explicitly set them in your script or source your .bashrc (or whatever file sets your variables.); cron does not use your rc files by default, so your environment is quite bare.

share|improve this answer

I think that, first of all, you should check your cron's logs for errors, maybe you can copy a little part of your logs here to have a better troubleshooting.

You might try to change the "date" command with this:

$(date +\%F)

And than try again to set the cronjob ;-)

share|improve this answer
The problem is not the date part; that works ok. There is a c/p error in the OP, but the file is created correctly. Just scping that doesnt work. – Lennart Sep 6 '12 at 13:51
I think that scp can't find the ssh key. maybe you should check your key location – Andrea Giardini Sep 6 '12 at 14:06

Your Answer


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.