Search code examples
bashshellcronstatus

bash script run over cron fail some commands


I have a simple bash script to monitor the status of a service.

Script control

  • If service its running
  • If not running, execute init script start of service and get some lines of log, write a log time of moment and it send email with result.

All working fin except init script start service when script run on a cron jobs. If execute manually this script work fine all.

#!/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" [email protected]
    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

All commands on conditional working fine on shell and with cron, except /etc/init.d/open-xchange start (try using /bin/bash /etc/init.d/open-xchange start, service open-xchange start,...)


Solution

  • /opt/open-xchange/lib/oxfunctions.sh: line 109: start-stop-daemon: command not found

    start option is calling command start-stop-daemon to start the service, any one of 3 options will solve your issue:

    1. Find out where is start-stop-daemon, in /etc/init.d/open-xchange, replace it with its full path
    2. Add export PATH=$PATH:/path/to/start-stop-daemon/directory to your script
    3. Add source ~/.bash_profile or source ~/.bashrc to your script