[英]bash script run over cron fail some commands
我有一个简单的bash脚本来监视服务的状态。
脚本控制
当脚本在cron作业上运行时,除init脚本之外的所有工作fin都会启动服务。 如果手动执行此脚本可以正常工作。
#!/bin/bash
estado=$(/etc/init.d/open-xchange status)
echo $estado
if [ "$estado" != "Checking for Open-Xchange: running." ]; then
hora=$(date +%F-%T)
tail -n 1000 /var/log/open-xchange/open-xchange.log.0 > /tmp/open-xchange.log.$hora
cat /tmp/open-xchange.log.$hora |mail -s "Reinicio en OX $hora" xxxxxx@gmail.com
rm -f /tmp/open-xchange.log.$hora
echo $hora >> /root/caidas-ox.txt
/etc/init.d/open-xchange start # The problem. This command not work when scripts its executed form crond
sleep 10
/opt/open-xchange/sbin/showruntimestats -d 'java.util.logging:type=Logging!setLoggerLevel!!ALL!'
fi
除了/etc/init.d/open-xchange start之外,所有关于条件的命令都可以在shell和cron上正常工作(尝试使用/ bin / bash /etc/init.d/open-xchange start,service open-xchange start ,. ..)
您的cronjob可能没有设置所有路径。 如果它从命令行工作,请执行
echo $PATH
在该命令行上添加
PATH=<...>
将<...>
替换为echo
命令输出中给出的PATH。 用脚本启动脚本
<scriptname> 2>/tmp/script.log
然后检查cronjob后看看发生了什么。
顺便说一句,要检查open-xchange的状态,似乎你可以使用
/etc/init.d/open-xchange status
/opt/open-xchange/lib/oxfunctions.sh:line 109:start-stop-daemon:找不到命令
start选项是调用命令start-stop-daemon
来启动服务,3个选项中的任何一个都将解决您的问题:
export PATH=$PATH:/path/to/start-stop-daemon/directory
到您的脚本 source ~/.bash_profile
或source ~/.bashrc
到您的脚本中 非常感谢@Coroos和@ruifeng
阅读完回复后,我了解Crontab上路径的问题。
尝试了几个选项之后,对我来说最好的是在bash脚本之上添加PATH和SHELL变量(与root用户相同),使用crontab运行,并且正常工作。
!/bin/sh
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/open-xchange/sbin
工作正常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.