SSH Public-key Auth in a Cron Job

I wanted to run a simple automated backup script that would copy local files to a remote server. I used ‘scp’ to copy those files. Now, I have setup scp as needed to enable public key authentication with the server so that I would not be asked for a password. However, the authentication would work only if I — personally — ran the backup script and not when cron ran it with my user id. I was perplexed.

After quite a bit of digging, I figured it out: I used the Seahorse program to create my public–private key pair. Now, Seahorse automates the entire key setup process, even on the remote machine. It then asks Gnome to cache (using gnome-ssh-askpass probably) the private-key unlock password used by the user and make it available to Gnome applications when necessary. However, cron is not a gnome application, so when it ran the backup script using my user id, it still needed that password. Since there was no one there to supply that private-key unlock password, my backup script would fail.

The solution: use ssh-agent. You can make the unlock password available to cron by doing the following:

$ exec /usr/bin/ssh-agent $SHELL
$ ssh-add

for more details on why and how this works, go here. Now in System->Preferences->Startup Applications (or Sessions in Ubuntu), create an entry for the following command:

/usr/bin/ssh-agent;/usr/bin/ssh-add

This should do the trick!

Note: Portions of this solution were taken from here and here.

WordPress ABSPATH Issue

Every time I upgrade WordPress, I need to make a change to wp-load.php. In the first line of the file (where ABSPATH) is being set, I need to change the second argument manually to the full path of the current WordPress directory. This is annoying but somehow their automatic path setup does not work in my environment.