|Tux: The Linux Penguin|
Before we get started, allow me to briefly explain cron for those of who don't know. Cron is a daemon (a program that runs in the background and is not controlled by a user) that kicks off periodically as defined by your crontab settings. Each user has
their own individual crontab which specifies what cron should do and when cron should do it. Commands listed in a user's crontab run under that user.
Now that we are cron experts (sort of), let's consider what happens when we specify an ssh (or scp) command in crontab. Since ssh/scp requires a password, cron will fail if you specify an ssh/scp command. The solution? Set up a trust between your client host and the host you are trying to connect to.
To setup this trust, create an ssh key by executing ssh-keygen at the command line of the host you are connecting from. See the example below.
Next, copy the key you just created to the remote host's (host you are connecting to) authorized key store by using as shown below.
You will have to enter your password for the remote host, and then you should be able to ssh into the remote host without supplying a password.
Once you can ssh without supplying a password then you can add an ssh or scp command into your crontab. The format of a crontab entry is as follows:
min hr mo wkdy command-to-execute
There is a space between each field. For example, if you wanted some command to be executed every hour at 5 minutes past the hour, the crontab line entry would be as follows:
5 * * * * command-to-execute
Get the idea?
To open crontab, simply execute the command crontab -e in the terminal. This will bring up your crontab editor. I use vi as my default editor for crontab. Without going into the specifics of vi, if you want to type, hit
esc then type i. If you want to save and exit then hit esc then type :wq.
Once crontab is open, lets say we want to copy a file via ssh to the remote host every hour on the hour. To do that, enter the following line in crontab:
Save and exit. Now you have a cron job that kicks off every hour to copy files to a remote host via ssh.