Cel
Cel

Reputation: 6649

restart service crontab problem

if i run the script below manually from putty (as root) then the service is restarted no problem

manual command

sh /home/auser/server/reService.sh >> /var/log/reService.log

But if the same script is scheduled as a cronjob, also under root:

crontab command

00 07 * * * /home/auser/server/reService.sh 2>&1 >> /var/log/reService.log

then the 'service myService restart' is not executed successfully: service is not restarted, and no command output in scheduled log compared to manual log. I can also tell you that nothing after the restart line executes if scheduled.

Why, am I missing some privileges when going through crontab? How can i successfully schedule restarting of myService?

thanks ahead.

reService.sh

#!/bin/bash
clear
echo "============================================================================="
echo "Daily restarting of my service"
date
echo "============================================================================="
service myService restart
exit 0

manual log

 [H [J=============================================================================
Daily restarting of my service
Thu May 19 04:51:12 EDT 2011
=============================================================================
Stopping myService   ..done
Starting myService   ..done

scheduled log

=============================================================================
Daily restarting of my service
Thu May 19 07:00:02 EDT 2011
============================================================================= 

Upvotes: 1

Views: 4044

Answers (1)

trojanfoe
trojanfoe

Reputation: 122381

The use of sh in your manual example implies that the script doesn't have execute permission. Try setting that or calling sh in the same way within the crontab (I prefer the former).

EDIT: OK, it's not permissions. Next thing to look at is $PATH; echo that to the log file and compare them.

EDIT2: If PATH is the issue, then just set it within crontab:

PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin/:/usr/local/sbin:/usr/bin/X11:/usr/‌​openwin/bin:/usr/java/bin:/root/bin 

00 07 * * * /home/auser/server/reService.sh 2>&1 >> /var/log/reService.log

Upvotes: 3

Related Questions