【发布时间】:2019-05-13 01:29:45
【问题描述】:
在我的 Webinoly Ubuntu 18 服务器上列出站点的简单脚本在 CLI 中工作,但在 cron 中失败。 Webinoly 是一个站点管理脚本,有一个命令可以列出它正在管理的站点:
site -list
此命令在 CLI 中的输出如下所示:
- catalyst.dk39ecbk3.com
- siteexample3.com
- webinoly.dk39ecbk3.com
我遇到问题的脚本(如下)应该删除控制字符、每行开头的“-”以及使用 sed 的空行:
#!/bin/bash
# create array of sites using webinoly 'site' command
# webinoly site -list command returns lines that start with hyphens and spaces, along with control chars, which need to be removed
# SED s/[\x01-\x1F\x7F]//g removes control characters
# SED s/.{7}// removes first seven chars and s/.{5}$// removes last 5 chars
# SED /^\s*$/d removes blank lines
# removing characters http://www.theunixschool.com/2014/08/sed-examples-remove-delete-chars-from-line-file.html
# removing empty lines https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed
SITELIST=($(/usr/bin/site -list | sed -r "s/[\x01-\x1F\x7F]//g;s/.{7}//;s/.{5}$//;/^\s*$/d"))
#print site list
for SITE in ${SITELIST[@]}; do
echo "$SITE"
done
这是我在 CLI 中看到的所需输出:
root@server1 ~/scripts # ./gdrive-backup-test.sh
catalyst.dk39ecbk3.com
siteexample3.com
webinoly.dk39ecbk3.com
脚本在 cron 中运行时会出现问题。这是 cron 文件:
root@server1 ~/scripts # crontab -l
SHELL=/bin/bash
MAILTO=myemail@gmail.com
15 3 * * 7 certbot renew --post-hook "service nginx restart"
47 01 * * * /root/scripts/gdrive-backup-test.sh > /root/scripts/output-gdrive-backup.txt
这里是 cron 命令生成的 output-gdrive-backup.txt 文件:
root@server1 ~/scripts # cat output-gdrive-backup.txt
lyst.dk39ecbk3
example3
noly.dk39ecbk3
每行的前三个字符缺失,后四个字符(.com)也缺失。
我已经研究并确保在 cron 文件以及脚本开头强制使用 bash。
【问题讨论】:
-
-> 你能解释一下为什么你需要删除前 7 个字符和后 5 个字符吗?这背后的逻辑是什么?
-
这是我需要删除的数量才能在 CLI 中实现所需的输出。我一定没有正确剥离控制字符。
-
我无法理解的是为什么脚本在 CLI 中运行时有效,但在通过 cron 运行时无效。这是最大的挫败感。
-
好的,您可以执行以下 2 个故障排除步骤吗: (1) 当 cron 运行时,将站点命令的输出保存在一个文件中,我们需要检查输出。 (2) 显示cron在另一个文件中使用的
sed的版本 -
我发现 cron 没有指定 TERM -- 查看答案。
标签: linux bash ubuntu sed cron