LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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
  Search this Thread
Old 03-28-2012, 11:40 AM   #1
effazero
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: Disabled
problem with crontab


Hello,

I have this script :

Code:
#!/bin/sh

 #
 #
 #
 # cmk

 # shutdown tomcat6 and wait


 ####### Stop des services tomcat #########################

 # get the tomcat pid
tomcat_pid=`ps -ef | grep tomcat | cut -c10-14`

 if [ -n "$tomcat_pid" ];   then
   echo "arret service tomcat6"
   # arret IA et Web Service
   service tomcat6 stop
echo "service tomcat6 arrete"
 fi


 ############## Arret par des kill des process s'ils existent encore #############

 # if tomcat_pid exists, kill the process (service tomcat)
 tomcat_pid=`ps -ef | grep tomcat | cut -c10-14`

 if [ -n "$tomcat_pid" ] ;  then
echo "kill tomcat_pid 1"
 kill $tomcat_pid
echo "tomcat_pid killed"
 sleep 60
 fi

 # if tomcat_pid still exists, really kill the process
 if [ -n "$tomcat_pid" ] ; then
echo "kill tomcat_pid"
 kill -n 9 $tomcat_pid
echo "tomcat_pid killed"
 sleep 10
 fi



######## Arret APACHE ############
echo "arret d'apache"
 service apache2 stop
echo "apache arrete"
 sleep 10



 ##### Relance des services ###############
echo "redemarrage d'apache"
 service apache2 start
echo "apache demarre"
 sleep 5

 # clear log
 #rm -f /usr/local/tomcat6/logs/catalina.out

 # restart  tomcat
echo "start service tomcat6"
 service tomcat6 start
echo "service tomcat6 demarre"
for this file : chmod 755

The script works perfectly fine when I call it manually from
Quote:
root@myserver:~#recharge_tomcat.sh
But it stops when I try to call if from a cron :

Quote:
/etc/cron.daily/recharge_tomcat:
arret service tomcat6
service tomcat6 arrete
/etc/cron.daily/recharge_tomcat: 21: service: not found
kill tomcat_pid 1
run-parts: /etc/cron.daily/recharge_tomcat exited because of uncaught signal 15
I have tested several solutions for the call :
- via crontab -e : m h * * * /the/complete/path/to/the/script
- via cron.daily, having the script into the repository /etc/cron.daily, without the .sh extension and by calling it via crontab -e : m h * * * cd / && /etc/cron.daily
same error....

I don't think the mistake comes from the call in the crontab.
But I do not understand why it does work when I call it manually, and stops in the middle when I try an automatic call via a crontab....

I assume that the following was due to the same reason : the script worked when I tried to call the script via
Quote:
root@myserver:~#./recharge_tomcat.sh
, but stopped at the same place when calling via
Quote:
root@myserver:~#bash recharge_tomcat.sh
 
Old 03-28-2012, 11:43 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
cron has a limited PATH, you should hardcode the paths to any programs you use inside the script. In this case, it's your call to "service" that's failing. Use the full path to service instead.
 
Old 03-28-2012, 12:06 PM   #3
effazero
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks for the answer.

However, the script stops AFTER stopping tomcat.

I edited it with the complete path to the service : /usr/sbin/service tomcat6 restart

now it stops at the same place, without telling why.

Quote:
/etc/cron.daily/recharge_tomcat:
arret service tomcat6
Stopping Tomcat servlet engine: tomcat6.
service tomcat6 arrete
kill tomcat_pid 1
run-parts: /etc/cron.daily/recharge_tomcat exited because of uncaught signal 15
 
Old 03-28-2012, 12:40 PM   #4
effazero
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
ok my mistake, i am so stupid sorry,

i have changed ALL pathes....
works fine now, thank you
 
Old 03-28-2012, 12:44 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
Edit: nevermind, looks like you fixed it.
 
Old 03-28-2012, 01:56 PM   #6
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636
In addition, instead of retrieving the pid from the ps statement, you could use: pkill -f tomcat. Using pkill like this, it does exactly the same as grepping the process name from the process list and kill the pid. pkill -9 likewise.

jlinkels
 
Old 03-29-2012, 08:48 AM   #7
effazero
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
yes thanks, it was not fixed afterall....
I am trying to use pkill instead of kill with a ps -ef that interacts with my own script...
 
Old 03-29-2012, 09:27 AM   #8
effazero
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Here is my script, please tell me if I am right...thanks a lot ....

Code:
#!/bin/sh
#!/usr/sbin
#!/bin
 #
 #
 #
 # cmk

 # shutdown tomcat6 and wait


 ####### Stop des services tomcat #########################

 # get the tomcat pid
tomcat_pid=`ps -ef | grep tomcat | cut -c10-14`

 if [ -n "$tomcat_pid" ];   then
   echo "arret service tomcat6"
     /usr/sbin/service tomcat6 stop
echo "service tomcat6 arrete"
 fi


  # if tomcat_pid exists, kill the process (service tomcat)
 tomcat_pid=`ps -ef | grep tomcat | cut -c10-14`

 if [ -n "$tomcat_pid" ] ;  then
/usr/bin/pkill tomcat6
echo "tomcat_pid killed"
 sleep 60
 fi

 # if tomcat_pid still exists, really kill the process
 if [ -n "$tomcat_pid" ] ; then
/usr/bin/pkill tomcat6
echo "tomcat_pid killed"
sleep 10
 fi


######## Arret APACHE ############
echo "arret d'apache"
 /usr/sbin/service apache2 stop
echo "apache arrete"
 sleep 10


 ##### Relance des services ###############
echo "redemarrage d'apache"
 /usr/sbin/service apache2 start
echo "apache demarre"
 sleep 5

 # restart  tomcat
echo "start service tomcat6"
 /usr/sbin/service tomcat6 start
echo "service tomcat6 demarre"
 
  


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] Crontab Problem kowsi Linux - General 1 12-15-2010 03:40 AM
crontab problem rammu_sivraj Red Hat 2 01-22-2007 12:07 AM
Crontab Problem smus Linux - Newbie 4 12-10-2005 09:52 AM
crontab problem BlackRabbit Linux - General 9 06-17-2004 08:38 AM
crontab problem TomTheNewbie Slackware 1 07-09-2003 08:12 AM


All times are GMT -5. The time now is 10:55 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration