My script stops working when using crontab

I have made a shell script(/bin/sh) that starts a perl script (that I haven't made my self) that's starts a ssh session. The ssh session uses a private/public key to login so no password is needed. The Perl script works perfect.

But when I put it in a cronjob (crontab) the ssh connection asks for password. This makes the scripts fail. Seems like ssh keys are not working while running in cron. Is there a process that I shall start?

This is the reason why I made the shell script to load most of my env variables( I know that cronjob starts with a slim shell) from my normal shell. I thought it was some env variable that I have missed to set. I don't have much Unix experience so not sure how I can describe the problem more. If there anything else that I have to say ask about it.

I tried to search for it but found something about ssh-agent. That should be started but didn't understand it. Also something about a keying.

Is cron being invoked by the same user that has the trused keys?

Yes, the crontab is run by the same user as the script works for. I put:

echo 'whoami' > whoami.txt

in the shell script to check that.

  1. when using cron you have to provide the full path to any executable
  2. provide the path to the keys ssh -i /home/user/.ssh/id_rsa
1 Like

Thanks I will try that:).

However I'm not sure if I can modify the perl script. Will it work if I just put it in my $PATH?

I don't know about putting it in your path, but you could alias "ssh" to "ssh -i /whatever/path". For some reason I never got to the bottom of, ssh doesn't find my keys either on our system, so I added "alias ssh='ssh -i ~/.ssh/identity'" to my bash startup files.

Since you are using cron however, I think you may need to create a small wrapper containing just the alias + call to the perl script, and start *that* from within crontab. Or something like that :wink:

Off topic (maybe).

This will not work from cron because it needs terminal context.