abkrim
abkrim

Reputation: 3692

bash script run over cron fail some commands

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

Script control

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,...)

Upvotes: 0

Views: 1448

Answers (3)

abkrim
abkrim

Reputation: 3692

A lot of thanks @Coroos & @ruifeng

After read your responses, I understand a problem with path on Crontab.

After try several options, the best for me it's add PATH and SHELL variables (same as root user) on top of bash script, run with crontab, and work fine.

!/bin/sh
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/open-xchange/sbin

Work fine

Upvotes: 0

ray
ray

Reputation: 4267

/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

Upvotes: 1

Coroos
Coroos

Reputation: 390

Your cronjob probably does not have all paths setup. If it works from the command line, do

echo $PATH

on that command line and add

PATH=<...>

with <...> replaced by the PATH given in the output of the echo command. Start your script with

<scriptname> 2>/tmp/script.log

and check after the cronjob ran to see what goes on.

By the way, to check the status of open-xchange, it seems you could use

/etc/init.d/open-xchange status

Upvotes: 1

Related Questions