Make scp command work with cronjob
For a couple of days now, I use a bash script to generate an html page and then upload it to my devspace via scp. Today, I decided to put that script to my crontab and stop running it myself every now and then. To my surprise, cron log listed the following error after every execution of my script:
Permission denied (publickey)
However the script runs successfully when I run it myself. Googling around I run into crons’ man page:
Cron is using a very limited enviroment
HOME=user’s-home-directory LOGNAME=user’s-login-id PATH=/usr/bin:/usr/sbin:. SHELL=/usr/bin/sh
Apparently the SSH_AUTH_SOCK variable that was handling my ssh connections, based on PublicKey, was missing.
Solution:
Many of you already know the keychain script . If you don’t, you may seriously consider having a look on its documentation. I wont describe the procedure here though. Adding my ssh key to keychain, a new file is created, including the following variable definitions.
hwoarang@Mystical ~ $ cat ~/.keychain/Mystical-sh SSH_AUTH_SOCK=/tmp/ssh-lKgXVi6837/agent.6837; export SSH_AUTH_SOCK; SSH_AGENT_PID=6838; export SSH_AGENT_PID
The variable I was looking for is here :). Now all I have to do, it to source this file at the very beggining of my bash script ( just below #!/bin/bash ) and then everything works as expected :)









