SSHFS and Cron

I wanted to run some new scripts in some cron jobs. The cron jobs depended on SSHFS. The cron jobs failed but the scripts ran fine when run directly in a terminal window. The scripts also ran fine using the at scheduler.

Some digging revealed the difference was a missing SSH_AUTH_SOCK environment variable. That variable is not defined inside a cron environment. To resolve the problem I added the following snippet in my scripts:

    # The SSH_AUTH_SOCK environment variable is needed for sshfs. In a
    # typical cron environment this environment variable does not exist.
    if [ "$SSH_AUTH_SOCK" = "" ]; then
      echo "SSH_AUTH_SOCK is not defined. Attempting to define."
      if [ -d /run/user/${UID}/keyring ]; then
        echo "Found /run/user/${UID}/keyring."
        export SSH_AUTH_SOCK="/run/user/${UID}/keyring/ssh"
      elif [ -d /tmp/runtime-${USER}/keyring ]; then
        echo "Found /tmp/runtime-${USER}/keyring."
        echo "Unable to determine the location of \$SSH_AUTH_SOCK."
        exit 1
    echo "SSH_AUTH_SOCK is defined: $SSH_AUTH_SOCK."

The scripts then ran fine in cron.

Posted: Category: Usability Tagged: General

Next: A New Tracfrone

Previous: Magnificent Desolation