请考虑以下 crontab(root):
SHELL=/bin/bash
...
...
0 */3 * * * /var/maintenance/raid.sh
和 bash 脚本 /var/maintenance/raid.sh
:
#!/bin/bash
echo -n "Checking /dev/md0... "
if ! [ $(mdadm --detail /dev/md0 | grep -c "active sync") -eq 2 ]; then
mdadm --detail /dev/md0 | mail -s "Raid problem /dev/md0" "my@email.com";
echo "ERROR"
else
echo "ALL OK"
fi;
#-------------------------------------------------------
echo -n "Checking /dev/md1... "
...
这就是发生在...
...从 shell 提示符 (bash) 执行:
带有 mdadm --detail/dev/md0
输出的邮件被发送到我的电子邮件(正确的行为)
...由 cron 执行:
空白邮件已发送到我的邮箱(主题有,但没有消息)
为什么会有这样的差异,如何解决?
最佳答案
如图所示in the comments , 请在 crontab 脚本上使用完整路径,因为 crontab 确实具有与普通用户(在本例中为 root)不同的环境变量。
在你的例子中,不是 mdadm
,而是 /sbin/mdadm
。
如何获取 full path of a command ?使用命令 command -v
:
$ command -v rm
/bin/rm
关于linux - 脚本在 Bash 和 cron 执行时产生不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18912018/