繁体   English   中英

运行在cron上的bash脚本失败了一些命令

[英]bash script run over cron fail some commands

我有一个简单的bash脚本来监视服务的状态。

脚本控制

  • 如果服务正在运行
  • 如果没有运行,执行init脚本启动服务并获取一些日志行,写一个日志时刻,并发送带有结果的电子邮件。

当脚本在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个选项中的任何一个都将解决您的问题:

  1. 找出start-stop-daemon的位置,在/etc/init.d/open-xchange中,用它的完整路径替换它
  2. export PATH=$PATH:/path/to/start-stop-daemon/directory到您的脚本
  3. source ~/.bash_profilesource ~/.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM