Blue spheres banner image

benjamin ar

  • /projects
  • /words

Using SSH in a Cron Job

November 20, 2025

Yesterday, I setup a cron job to push my notes directory every two hours. The setup was simple:

Here is the shell script:

#!/bin/zsh
cd ~/notes
git add .
git commit -S -m "[cron/$(date '+%Y-%m-%d %H:%M')] update "
git push origin

To set this up, I just did the following sequence of commands:

crontab -e to edit the crontab.

0 */2 * * * /Users/bjar/notes/push.sh > cronout.txt 2>&1

This will run the script on the first minute of every two hours and direct the output to a file (for debugging) the 2>&1 directs stderr to stdout. More info here.

Issue with signed commits

I have unfortunately setup signed commits with GitHub cause I thought it was cool and nice to have the little verified badge next to my commits. Sadly, this was breaking my cron because it did nothave access to the ssh-keys that I use to sign my commits. To fix this I followed the following tutorial: https://gist.github.com/Justintime50/297d0d36da40834b037a65998d2149ca

You can add the following to your .zlogin file. This will run on login.

# Use keychain to keep ssh-agent information available in a file
keychain "$HOME/.ssh/id_rsa"
. "$HOME/.keychain/${HOST}-sh"

To get this up and running immediately, you can go ahead an execute this in your current shell.

Then in your crontab file crontab -e you can preprend . "$HOME"/.keychain/${HOST}-sh; to your command, resulting in:

0 */2 * * * . "$HOME"/.keychain/${HOSTNAME}-sh; /Users/bjar/notes/push.sh > cronout.txt 2>&1

This works like a charm for me!

Details of my setup:

  • OS: macOS Sequoia 15.5
  • Shell: zsh

benjamin ar © 2025|githubXlinkedin