这里的文档没有在crontab的子shell中执行

时间:2013-01-11 10:17:41

标签: bash cron sftp heredoc

我有一个脚本在提示符下运行良好,但不是从crontab运行。唯一被破坏的是脚本中子shell中忽略的here文档。该脚本使用psftp。这是代码:

/home/batch/myscript.sh:

(
cd /home/downloaded/csv/
psftp -pw passw0rd -P 1234 user@someftp.com << EOF
cd outgoing
mget *.csv
EOF
) | tee /home/batch/logs/mylog.log | mail -s "Mail subject" "me@mycompany.com"

crontab -e:

45 10 * * * /bin/sh /home/batch/myscript.sh

确定 - 从提示符启动时发送邮件:

Remote working directory is /
psftp> Remote directory is now /outgoing
psftp> *.csv: nothing matched
psftp> quit

KO - 从crontab启动邮件:

Remote working directory is /
psftp> quit

Google没有提供太多帮助。我已经尝试了几种EOF语法,因为它似乎是这里文档的常见问题,但我不认为它来自于此。它可能与crontab的某种嵌套级别有关,是否有这样的事情?

祝你好运

1 个答案:

答案 0 :(得分:1)

好的,问题解决了:

用户crontab用于执行脚本的方式不同。发生的事情是sftp的密钥没有为这个用户注册。这会导致提示要求注册密钥,但我的脚本无法解答。

我以其使用的用户身份连接,psftp'd并注册了密钥。