0

i made a really simple script to ensure my important services are active.

#!/bin/bash
services=(apache2 sendmail mysql)
for service in "${services[@]}"
do
        if [[ $(service $service status | grep running) ]]; then
                echo $service " is active" >> /home/user/logging.txt
        else
                echo "!!!!" $service " is not active!!!!" >> /home/user/logging.txt
        fi
done

When i run the script manually, everything works fine, active services get logged as active and inactive services logged as inactive.

For automating the script i made an entry in crontab:

0 10 * * * /usr/bin/service-check

The script runs but the output generated in the logging file is not correct! The output in the loggingfile says for every Service: !!! servicename is not active !!!

i repeated the steps several times same result over again...

Output of the logging file:

!!!! apache2  is not active!!!!
!!!! sendmail  is not active!!!!
!!!! mysql  is not active!!!!
apache2  is active
sendmail  is active
mysql  is active

The first three lines are from crontab, the last three from manual start... This really grind my gears and i can't figure out whats wrong... any idea?

Oli
  • 293,335
moe
  • 1

1 Answers1

0

It may not be finding service.

You may need to specify SHELL and PATH variables like

SHELL=/bin/bash; 
PATH=<copy from your current $PATH that works>

before you add e.g

0 10 * * * /usr/bin/service-check
derHugo
  • 3,356
  • 5
  • 31
  • 51