Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Cron job not running - script uses rsync, ssh (with id_rsa)

Does anyone know how to get cron to run a rsync/ssh script which uses id_rsa for passwordless logins?


I have a backup script I have slaved for months over that works wonderfully when running it from the command line.


It uses rsync and connects with SSH (passwordless using ssh keys). I have setup id_rsa and id_rsa.pub for passwordless logins (this is my user account, and connects using root@ to the server)


As stated all works find when run from the command line. But when I setup a cron job to run this file (exact same line I run from the command line) it does not execute. Actually, it does run, as I have an email notifiction which sends to me. It is just the rsync/ssh part that does not run.


I have spent days and days trying to figure it out. No error messages occur, nothing. Took me a while to find out cron is emailing the user account on Lion the messages (not to a proper email address).


The error shows a permission denied, which I believe to be the cron user cannot use the id_rsa/ssh connection.


Cron is running the script as my user, the same I use on the command line. I even tried copying the id_rsa to the root user .ssh folder in case cron is running as root but that does not work either.



So does anyone know how to get cron to run this rsync/ssh script which used id_rsa for passwordless logins?


Thanks a lot

iMac, Mac OS X (10.7.3)

Posted on Apr 26, 2012 9:05 AM

Reply
21 replies

May 1, 2012 4:46 AM in response to Linc Davis

Am trying Lingon. The only issue is Lingon is less flexible with the schedule. I want to run some things every other day, and lingon does not offer it without creating multiple jobs for each day to run it. So that would be 3 or 4 jobs for every backup server I want, and I have a lot.


Going to see if it works first, then use it in the short term, then need to look into something with more flexible scheduling like cron.


I fond Launchd Editor, but it seems its not live any more.

May 1, 2012 4:46 AM in response to Linc Davis

Still get the issue with Lingon...


The console logs shows the same error I get mailed to me when using Cron. That is "Permission Denied" which I believe refers to using SSH with password less keys.


To confirm, the backup script runs fine in terminal. The script is being executed using Cron and Lingon, but rsync via SSH is not being run when using Cron and Lingon.


Thanks

May 1, 2012 5:11 AM in response to amityweb

When I say password less, there is a password I just dont enter it. When I created the keys I entered a password. Then when I connect first time it asks for a password (in a pop up window not terminal) and asks to add it to keychain. Subsequent connects I am never prompted for a password.


Just wanted to mention that in case it is related, maybe cron or launchd cannot access keychain or something?

May 1, 2012 5:20 AM in response to amityweb

I would rather get cron working, or know it cant


From man crontab


(Darwin note: Although cron(8) and crontab(5) are officially supported under Darwin, their functionality has been absorbed into launchd(8), which provides a more flexible way of automatically executing commands. See launchctl(1) for more information.)


Have a look at this page, hope you can find useful tips


http://docstore.mik.ua/orelly/networking_2ndEd/ssh/ch11_01.htm

May 1, 2012 5:33 AM in response to amityweb

Finding it quite unbelievable that we cant use rsync and ssh in a cron/launchd with password protected SSH keys. This must be very common for backups. I have perfected my script which took ages, and I cant automate it.


Seems a lot of websites say create a password-less keys (just hit enter when prompted), but this is less secure, so not acceptable. Have not tried it.

May 1, 2012 5:55 AM in response to amityweb

YES! Got it working, finally!


So the issue is indeed cron/launchd not being able to access the ssh-agent which holds the key.


So this is the solution...


Download keychain for Mac:

http://www.funtoo.org/wiki/Keychain


Then once its installed and configured, the script to run needs this added to the top:

eval `keychain --noask --eval id_rsa` || exit 1


Note I am using id_rsa whereas keychains instructions refer to id_dsa so I just changed it and it works OK.


When the script runs the first time it asks for the key password. Then it works from then on.

What I do not know yet is will I need to re-enter the password when I logoff/logon again. That will cause an issue as I auto restart the Mac every day which means if I am off for a few days backup will stop (can you see the madness in this issue!).

May 4, 2012 2:58 PM in response to Linc Davis

Spoke too soon...


If I am logged in at the computer and set cron to run (using Cronnix) there and then, it runs. But if I then set it to say midnight and leave for the day (screen is locked, I have to log back in when I get back in morning) it is not running I get the Permission Denied error again.


Not tried launchd, so will try that next, but seems this solution does not work when not logged in/locked. Probably something to do with the ssh agent again. OR maybe its because the computer restarted that day and I have not entered the password for cron to access ssh agent.


Its mad this, for what should be an important/common/simple task.

Cron job not running - script uses rsync, ssh (with id_rsa)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.