Cron - 调用外部脚本失败(环境变量初始化)

时间:2011-08-17 18:13:44

标签: cron pexpect

一个脚本,其部分目标是定期连接到一系列主机,rsync一些日志返回到中央服务器,在手动调用时可以完美地工作,

python ./apex2zabbix.py --sync="Client_Service_Platform Emtity"    

然而,当通过cron调用时,在远程rsync步骤中使用.EOF死掉,

command = "/usr/bin/rsync -e ssh -a %s --compress=9 -pgtov %s %s --exclude='*' %s@%s:%s%s %s" % (remote_rsync_binary, excluded_expression, filters_expression, user, ip, source_path, file_filter, target_path)

p = pexpect.spawn(command, timeout=360)

i = p.expect([ssh_new_conn,'[pP]assword:',pexpect.EOF])
print 'Initial pexpect command output: ', i
if i == 0:
    # send 'yes'
    p.sendline('yes')
    i = p.expect(['[pP]assword:',pexpect.EOF])
    if i == 0:
        # send the password
        p.sendline(passwd)
        p.expect(pexpect.EOF)
elif i == 1:
    # send the password
    p.sendline(passwd)
    p.expect(pexpect.EOF)
elif i == 2:
    print "key or connection timeout"
    pass

返回

Initial pexpect command output: 2

可能是什么造成的? 感谢

1 个答案:

答案 0 :(得分:0)

...叹息 如果没有脑力停止,我会记得添加

print 'output:', p.before

并注意到它正在返回

output: rsync: Failed to exec ssh: No such file or directory (2)

与我的观点相反,rsync -e中的ssh不是一个选项,而是二进制, 因此我应该添加

PATH=/sbin:/bin:/usr/sbin:/usr/bin

到cron或指定完整路径表单, / usr / bin / rsync -e / usr / bin / ssh

command = "/usr/bin/rsync -e /usr/bin/ssh -a %s --compress=9 -pgtov %s %s --exclude='*' %s@%s:%s%s %s" % (remote_rsync_binary, excluded_expression, filters_expression, user, ip, source_path, file_filter, target_path)