gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:54:54 25 4
gpt4 key购买 nike

我有一个应该定期运行的 bash 脚本。该脚本应连接到远程 SFTP 服务器并从那里获取文件。由于这是一个 SFTP 服务器,我必须将 expect 与 bash 脚本一起使用。该脚本在我手动运行时运行良好,但在通过 crontab 运行时失败。有问题的函数是 get_JSON_file()请指教...

这是代码:

#!/bin/bash

export xxxxx
export xxxxx
export PATH=xxxxx

check_if_file_is_open(){
while :
do
if ! [[ `lsof | grep file.txt` ]]
then
break
fi
sleep 1
done
}


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


get_JSON_file
check_if_file_is_open
cp file.txt /path/to/destination/folder

最佳答案

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

关于linux - Bash+Expect 脚本在 crontab 中无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52198987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com