LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   CRON : running shell script as root (https://www.linuxquestions.org/questions/linux-software-2/cron-running-shell-script-as-root-886715/)

Sean1e 06-16-2011 10:18 AM

CRON : running shell script as root
 
Hi - I'm trying to stop / start Apache using a bash script with CRON. The script needs to run as root, and works when I run it from a terminal while signed in as root.

When CRON runs it apache doesn't restart. I see no messsages in syslog, and cron log shows that the script has run.

Script /usr/local/sbin/skdailyA06 :

#!/bin/bash

echo '--- CronRoot test' >> /etc/httpd/logs/cronroot.txt

service httpd stop >> /etc/httpd/logs/cronroot.txt

service httpd start >> /etc/httpd/logs/cronroot.txt

exit

crontab -u root -e & /var/spool/cron/root has :

09 16 * * * /usr/local/sbin/skdailyA06

Cron log :

Jun 16 16:09:01 hostname crond[17834]: (root) CMD (/usr/local/sbin/skdailyA06)

/etc/httpd/logs/cronroot.txt contains just the line : "--- CronRoot test", and Apache is running with the same task number as before cron ran the script.

I'm using RHEL5.5, and ( as you can probably see ) this is my first 'serious' attempt at cron.

Thanks in advance,

Sean

jason_not 06-16-2011 10:30 AM

Restarting daemons with cron
 
Hi,

I will bet cron isn't finding the service command. Cron itself runs under a very limited environment. I would recommend for that service command that you make the path explicit: /sbin/service (or whatever the output of `which service` says). Also, I recommend you pause between stop and start.

Another way to do it would be to call the rc script directly (This is pretty much what the service command does):

Code:

/etc/init.d/httpd stop  >> /etc/httpd/logs/cronroot.txt

sleep 5

/etc/init.d/httpd start >> /etc/httpd/logs/cronroot.txt


Sean1e 06-16-2011 10:59 AM

Jason - You da man !
 
Hi Jason - Your bet was 100 % right - Followed your suggestion which worked perfectly !

Thank you sir - Much appreciated.

Cheers

Sean

jason_not 06-16-2011 11:04 AM

You are very welcome!

--jason


All times are GMT -5. The time now is 10:27 AM.