Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a remote machine, with which I have set up public-key ssh auth - that is, I can ssh to it without typing in password each time. And I have a set of actions in a script, which use that ssh connection.

But when I run that script via cron, it keeps asking passphrase for each action - and since I have more that ten of them, it gets quite boring.

I tried running ssh-add before those actions and got:

Could not open a connection to your authentication agent.

What can I do to reduce number of passphrase retypes?

share|improve this question
    
Did you add the cron entry to the crontab of the user that owns the key? That is, you didn't do something silly like add the script call to the global crontab, right? –  Warren Young Feb 27 '12 at 17:59
    
@WarrenYoung - actually, that's exactly what I did - I just put in the name of the user that owns the key as the user to run it from :) What is wrong with it? Crontab treats these two cases different ways? –  Rogach Feb 27 '12 at 18:02
    
Try running eval $(ssh-agent -s) before ssh-add (and ssh-agent -k at end). –  enzotib Feb 27 '12 at 18:08
    
@enzotib - Thanks, it works! I assume that ssh-agent -k should also be placed into eval? –  Rogach Feb 27 '12 at 18:14
    
@enzotib - if you format your comment as an answer, I'll accept it. –  Rogach Feb 27 '12 at 18:14
add comment

1 Answer 1

up vote 5 down vote accepted

Try running

eval $(ssh-agent -s) 

before ssh-add, to export the environment variables that refer to the just started agent, then run

ssh-agent -k

(without eval) at end of script to kill the agent.

share|improve this answer
    
When I run a last statement without eval, it gives me back two unset statements and an echo statement. Isn't it meant to be put into eval? –  Rogach Feb 27 '12 at 18:21
    
@Rogach: it is useful to run it into eval when your script keep doing things and you want to unset the variables, but it is not required. –  enzotib Feb 27 '12 at 18:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.