LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 08-11-2008, 02:24 PM   #1
Sanford Stein
Member
 
Registered: Jun 2008
Location: Evanston, Illinois
Distribution: RHEL 6.4
Posts: 118

Rep: Reputation: 17
Cannot stop/start services in cron job


I want to stop and restart tomcat5 every night. I wrote a shellscript to do this and it runs fine from the shell, but when put in cron it fails to either stop or restart the service, and it does not issue any error or trace messages.

Is it not legal to stop and start services in a cron?

Thanks,
Sanford Stein
CyberTools Inc.
 
Old 08-11-2008, 02:46 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
In root's crontab, you can do anything. Show root's crontab entry that fails.
 
Old 08-11-2008, 04:11 PM   #3
Sanford Stein
Member
 
Registered: Jun 2008
Location: Evanston, Illinois
Distribution: RHEL 6.4
Posts: 118

Original Poster
Rep: Reputation: 17
Cannot stop/start services in cron job

Mr. C,

Here is the line from the cron file:

20 5 * * * /root/tcrb %

And the script tcrb follows below.

But I can write a script with just one line, either
'service tomcat5 start' or
'service tomcat5 stop'
and it will fail as a cron job.

Thanks for your help,
SS

----------------------------------------------------------------------

#!/bin/bash
#
export JAVA_HOME=/usr/bin/javadir
export slog='/root/tcrb.log'
echo `date` >> $slog
export tpid=`/bin/ps -ef | /bin/grep -m 1 "tomcat5" | /bin/grep -v "grep" | /bin
/awk '{print $2}'`
export tpid=`expr $tpid + 1 - 1`
export cnt=0
while [[ $tpid -gt 0 && $cnt -lt 11 ]]
do
if [ "$cnt" -eq 0 ]
then
echo -n "Trying to shutdown Tomcat" >> $slog
fi
service tomcat5 stop
export tpid=`/bin/ps -ef | /bin/grep -m 1 "tomcat5" | /bin/grep -v "grep" | /b
in/awk '{print $2}'`
export tpid=`expr $tpid + 1 - 1`
export cnt=`expr $cnt + 1`
sleep 10
done
echo -e "\n" >> $slog
if [ "$tpid" -eq 0 ]
then
echo "Tomcat shutdown script worked." >> $slog
fi
#
#
#
if [ "$tpid" -gt 0 ]
then
echo "Tomcat shutdown script didn't work." >> $slog
echo "Sending SIGTERM..." >> $slog
while [[ $tpid -gt 0 ]]
do
echo $tpid >> $slog
kill -9 $tpid
export tpid=`/bin/ps -ef | /bin/grep -m 1 "tomcat5" | /bin/grep -v "grep
" | /bin/awk '{print $2}'`
export tpid=`expr $tpid + 1 - 1`
sleep 5
done
fi
#
#
#
export oap=`/bin/ps -ef | /bin/grep -m 2 "/usr/sbin/httpd" | /bin/grep -v grep |
/bin/grep -v root | /bin/awk '{print $2}'`
export oap=`expr $oap + 1 - 1`
export nap=`expr $oap + 1 - 1`
echo -n "Restarting Apache" >> $slog
export cnt2=0
while [[ "$oap" -eq "$nap" && cnt2 < 31 ]]
do
echo -n "." >> $slog
/etc/init.d/httpd restart >> $slog
export nap=`/bin/ps -ef | /bin/grep -m 2 "/usr/sbin/httpd" | /bin/grep -v gr
ep | /bin/grep -v root | /bin/awk '{print $2}'`
export nap=`expr $nap + 1 - 1`
export cnt2=`expr $cnt2 + 1`
sleep 10
done
echo -e "\n" >> $slog
echo "Apache Restarted." >> $slog
echo "Starting Tomcat..." >> $slog
service tomcat5 start >> $slog
exit 0
 
Old 08-11-2008, 04:13 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
CRON's default PATH is very restrictive. Use full path names to commands such as "service".
 
Old 08-11-2008, 04:41 PM   #5
Sanford Stein
Member
 
Registered: Jun 2008
Location: Evanston, Illinois
Distribution: RHEL 6.4
Posts: 118

Original Poster
Rep: Reputation: 17
Cannot stop/start services in cron job

Mr. C,

Great hint -- I have to make sure it runs at the appointed time overnight, but I was able to make the program run as a cron simply by calling '/sbin/service tomcat5 stop'.

Thank you so much!

SS
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
start, stop services johnkalikavunkal Solaris / OpenSolaris 1 07-14-2008 04:43 AM
Cron Job: Start it Only once on specified time jCash Linux - Newbie 2 12-07-2007 07:35 PM
Start/Stop Services Verbal Kint Linux - General 4 06-21-2007 06:56 AM
stop and start services hoover93 Suse/Novell 5 12-29-2004 02:48 PM
start and stop services log Linux - General 7 03-21-2003 08:29 PM


All times are GMT -5. The time now is 03:43 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration