Lessons learned wrestling with cron and ssh

Well these last three days feels like a hard slog. Here are some of the tips for Linux geek, so hopefully you can avoid problems when you are just trying to get a regular job that will pull from a git repo and run an automated build.

There are many systems available to do this, so this more educational than anything else, but it helped me learn quite a few things about ubuntu 14.04:

  1. Crontab is a really nice utility for running user jobs at regular intervals. Like editing of system files where you want some protection it is a little strange. You run crontab -l to see your system jobs. If you don’t have anything listed, it returns none found and it isn’t clear how to do the equivalent of a touch. The answer is echo "" | crontab -` which says put a newline into the crontab

  2. Crontab is also strange in that it needs a blank line at the end otherwise it will not process things!

  3. Crontab has an interesting syntax. Basically an asterisk means run it all the time. If you put a number in a column, it means run it when that column is equal to the number. So if the minute = 0 for instance, it means run  the line everytime you are at 0 minutes (that is on the hour). The syntax “*/30” means that you should run it every 30 units (for instance 30 minutes if it is the first).

  4. Finally if you are trying to run ssh in a cron job all kinds of strange things happen. You can’t put in a password for your ssh key because it is in the background, but you can use keychain to add it and have it remember your password (so you only have to type your password in every time the machine reboots. But in order for this to work, you have to rememeber to source a magic file in .keychain which is named $hostname-sh. This just sets the SSH_AUTH_SOCK variable correctly for you.

  5. You can actually manually figure out where the ssh-agent is by looking through /tmp and seeing if the a magic file has your ssh key in it using a find command and then grepping the name of the pipe.