0

shell script:

#!/bin/sh
services=( httpd named proftpd mysqld dovecot postfix webmin)

for service in ${services[@]}
do

if ps ax | grep -v grep | grep $service > /dev/null
then
    echo "$service service running, everything is fine"     
else
    echo "$service is not running"
    service $service start
fi

done 

file executable, running from root user

command:

bash /etc/mycron/checkServices.sh

tried sh and just /etc/mycron/checkServices.sh

doesn't run

3
  • Did you set a schedule for the cron job? Post the full crontab comm Commented Jun 14, 2011 at 0:25
  • 1
    Make the second line "printenv > /tmp/crontest" and tell us what UID the file is and what your PATH is. Commented Jun 14, 2011 at 0:30
  • @Seth is right. cron jobs run with a very limited environment. "service" command might be under /sbin or something. Commented Jun 14, 2011 at 0:38

2 Answers 2

5
#!/bin/sh
services=( httpd named proftpd mysqld dovecot postfix webmin)

for service in ${services[@]}; do
    if ps ax | grep -v grep | grep $service > /dev/null; then
        echo "$service service running, everything is fine";
    else
        echo "$service is not running";
        service $service start;
    fi;
done;

Works fine here... maybe you want to add after #!/bin/sh PATH="/bin:/sbin:/usr/bin:/usr/sbin:/opt/usr/bin:/opt/usr/sbin:/usr/local/bin:/usr/local/sbin"

You could also do chmod 775 /etc/mycron/checkServices.sh to make it executable, which is needed for cron. Then you would also not need to call bash /etc/mycron/checkServices.sh and can just call /etc/mycron/checkServices.sh the #!/bin/sh tells the executable loader to load the file with /bin/sh if you invoke bash /etc/mycron/checkServices.sh you will start bash which on his turn would start /bin/sh to finally execute your script.

Since the for loop in bash / sh uses the IFS variable ($IFS) as delimiter, you could also make the line services=(httpd named proftpd mysqld dovecot postfix webmin) as services="httpd named proftpd mysqld dovecot postfix webmin" since this is more general

Sign up to request clarification or add additional context in comments.

1 Comment

@lesyk In response to your edit suggestion: Please comment here, and say way more than "doesn't work".
1

Just as a general diagnostic process, it's sensible to insert trace statements into the script such as:

  • echo "Starting..."
  • echo "Checking for running service '$service'..."
  • which/whereis service
  • echo "Service has been started."
  • temporarily remove the > /dev/null for ps.

Then execute under cron with stdout and stderr redirected to a log file.

That allows you to identify the exact line that's failing. As others have said, it looks likely to be the "service" or "$service" commands aren't found because the PATH isn't set to include them. You do realise "service" itself is being sought as an external command that presumably launched $service in turn? Also keep an eye on root's mail, as cron sometimes sends error reports via mail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.