【发布时间】:2018-09-07 00:00:54
【问题描述】:
我有一个应该定期运行的 bash 脚本。该脚本应连接到远程 SFTP 服务器并从那里获取文件。
由于这是一个 SFTP 服务器,我不得不将期望与 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
【问题讨论】:
标签: linux bash shell cron expect