我有一个cronjob:
0 9 * * * rsync -a mydir remote_machine:
我用’crontab -e’安装了它.我有一个ssh-agent运行,当我执行rsync命令本身它没有任何用户交互或密码输入,但cronjob失败,并显示以下消息:
Date: Wed,9 Dec 2009 11:11:00 -0600 (CST) From: Cron Daemon <me@my_machine.my_domain> To: me@my_machine.my_domain Subject: Cron <me@my_machine> rsync -a /home/me/mydir remote_machine: Permission denied,please try again. Permission denied,please try again. Permission denied (publickey,gssapi-with-mic,password). rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: unexplained error (code 255) at /SourceCache/rsync/rsync-35.2/rsync/io.c(452) [sender=2.6.9]
为什么这不起作用?我知道cronjobs作为用户运行w / me(如果我运行’* * * * * touch / tmp / a’我拥有该文件)所以我假设rsync使用我的私钥登录我…
解决方法
您的cron会话shell不知道ssh代理,因此无法与之通信.
启动代理程序后,您可以将代理程序所需的信息放在cron会话中的某个位置以进行选择.
例:
AGENT="ssh-agent -s" if [ ! -d $HOME/.ssh/agent ]; then mkdir -p $HOME/.ssh/agent fi # # Start an agent if there isn't one running already. # pid=`ps -u$LOGNAME | grep ssh-age | awk '{print $1}'` if [ -z "$pid" ]; then $AGENT | grep -v echo > $HOME/.ssh/agent/$HOST & pid=$! sleep 1 # Let it fork and stuff fi
然后将密钥添加到代理.
ssh-add $HOME/.ssh/id_dsa
现在你的cron作业应该在尝试使用ssh之前执行此操作:
# # Get our parent to pick up the required SSH env vars. # . $HOME/.ssh/agent/$HOST
…之后,ssh会话应该正常进行.