Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have created a bash script as follows and saved it as /usr/bin/db.sync.sh:

#!/bin/bash

rsync -ar --delete /files/database_backups slyme@192.168.1.4:/users/slyme/Documents/server_backups

I have generated the necessary pair of authentication keys to make this work without the need to enter a password.

When I run the command directly it works (hurrah!).

I have added the following to `/etc/rc.local':

/usr/bin/db.sync.sh >>/files/database_backups/db-sync.log.txt 2>&1

When I restart the machine this script fails and the log file suggests that the process asks for a password which, of course, it doesn't get so it fails. If I run /etc/rc.local from the command line then, sure enough, I get asked for a password and when I enter it the script works.

Anyone got any ideas why the script needs a password when run from rc.local even though it doesn't need a password when run directly?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

The script /etc/rc.local is run as root. Make sure that you have the correct private key for user slyme@192.168.1.4 installed in /root/.ssh/ (and not just in your user's $HOME/.ssh)

Alternatively, to make sure the correct private key is used, you can specify it on the rsync command line:

rsync -e "ssh -i /path/to/ssh_key" ...

Hope this helps.

share|improve this answer
    
Thanks piit79, I shall try this –  Simon Jul 31 at 11:22
    
I copied the key to the root account. It worked. Thank you. This was the last step in what (for me) was a long process of configuring a development server just the way I want it. I am very happy with the outcome. –  Simon Jul 31 at 11:40

If you want to use /etc/rc.local you should run the script as your user:

su -l myuser -c /path/to/the/script

Otherwise, you can use the @startup option of the cron system; this way the job is safely executed as your user and you do not need to edit system files. See for example http://unix.stackexchange.com/questions/57852/crontab-job-start-1-min-after-reboot

share|improve this answer
    
Thanks for your suggestion re user, I shall try it out. I can't use a cron job for this without a lot of fiddling about because a script that this one relies on (to create the files in the first place) needs mysql to be up and running before it executes –  Simon Jul 31 at 11:21

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.