LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-16-2011, 10:18 AM   #1
Sean1e
LQ Newbie
 
Registered: Sep 2008
Location: Ireland
Distribution: RHEL 5.5, OpenSUSE 11.4
Posts: 5

Rep: Reputation: 0
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

Last edited by Sean1e; 08-25-2011 at 10:25 AM.
 
Old 06-16-2011, 10:30 AM   #2
jason_not
Member
 
Registered: Aug 2010
Location: Beaverton, Oregon, USA
Distribution: Pfsense, Ubuntu, Centos, Fedora, Redhat, Scientfic, MacOS
Posts: 76

Rep: Reputation: 19
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
 
Old 06-16-2011, 10:59 AM   #3
Sean1e
LQ Newbie
 
Registered: Sep 2008
Location: Ireland
Distribution: RHEL 5.5, OpenSUSE 11.4
Posts: 5

Original Poster
Rep: Reputation: 0
Jason - You da man !

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

Thank you sir - Much appreciated.

Cheers

Sean
 
Old 06-16-2011, 11:04 AM   #4
jason_not
Member
 
Registered: Aug 2010
Location: Beaverton, Oregon, USA
Distribution: Pfsense, Ubuntu, Centos, Fedora, Redhat, Scientfic, MacOS
Posts: 76

Rep: Reputation: 19
You are very welcome!

--jason
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell Script Not Working in Cron 3rods Linux - Newbie 6 02-10-2010 08:24 PM
Running shell scripts with cron Lossenelin Linux - General 2 09-24-2009 02:51 PM
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
Running a cron as root koobi Linux - Newbie 7 06-17-2006 09:38 AM
running root-tail from shell script not working Nuk Linux - Newbie 1 04-09-2004 08:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration