Bash + Expect脚本在crontab中无法正常运行

时间:2018-09-06 07:45:28

标签: linux bash shell cron expect

我有一个bash脚本,应该定期运行。该脚本应连接到远程SFTP服务器并从那里获取文件。 由于这是SFTP服务器,因此我必须在bash脚本中使用Expect。 当我手动运行脚本时,脚本运行良好,但通过crontab运行时,脚本运行失败。 有问题的功能是MyClass<T extends T_1 or T_2> 请告知...

这是代码:

get_JSON_file()

3 个答案:

答案 0 :(得分:3)

期望的interact仅在 stdin 在tty / pty上但cron作业不在tty / pty上运行时起作用。因此,将interact替换为expect eof(或在必要时替换expect -timeout 12345 eof)。

答案 1 :(得分:1)

将期望命令传递给期望解释器的方式非常尴尬。请改用(引号)heredoc,然后将-f选项删除为期望值

get_JSON_file(){
    /usr/bin/expect <<'EOF'
        spawn sftp -P port user@ip
        expect "Password:"
        send "password\r"
        expect "$ "
        send "get path/to/file/file.json\r"
        send "exit\r"
        expect eof
EOF
}

调试Expect脚本的最重要技巧是调用Expect的调试输出。在解决问题时,请使用

expect -d <<'EOF'

在crontab中,您希望将stderr重定向到stdout,以便获得调试输出

* * * * * /path/to/script.sh 2>&1

答案 2 :(得分:0)

要在shell脚本中运行函数,请勿使用括号。

您的代码将变为:

Sub Protect()

    Worksheets("Sheet1").Cells.Locked = False
    Worksheets("Sheet1").Range("B1:b10").Locked = True
    Worksheets("Sheet1").Protect Password:="erty", UserInterfaceOnly:=True


End Sub
相关问题