LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 12-12-2013, 06:38 AM   #1
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Rep: Reputation: 4
Bash script not working properly in cron


we have a JBOSS restart script scheduled in cron. When we execute manually script is working fine. But even in cron, script is getting executed, but JBoss is not getting started properly. I tried by updated Environment variables in /etc/crontab and also updated PATH variable in scripts. But I m unable to trace the issue

Below is the script.
Script1 calls script2

Script1
=======
1 #!/bin/bash
2 export PATH=/usr/java/jdk1.6.0_26/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/dell/srvadmin/bin:/opt/dell/srvadmin/sbin:/root/bin
3
4 application_restart()
5 {
6
7 echo "$runDate";
8
9 if [ $app_url = "https://IP:7443/event-server/xmlrpc" ] ; then
10 echo "URL_CONNECTION_EXCEPTION Occured; so restaring the application. $runDate"
11 MAIL_CONTENT="OpenNms Event Processor URL is available but HQEventServer is not available, hence redeploying HQEventServer application - Date:$(date)"
12 echo "MAIL TO:$MAIL_TO"
13 echo "MAIL_SUBJECT:$MAIL_SUBJECT"
14 echo "MAIL_CONTENT:$MAIL_CONTENT"
15 echo $MAIL_CONTENT | /usr/bin/mutt -F /root/.amuttrc -e 'set content_type="text/html"' "$MAIL_TO" -s "$MAIL_SUBJECT"
16 touch $JBOSS_APP_LOCATION/event-server-avis.war
17 echo "Sleeping for 60 secs to check app availability"
18 sleep 60
19 $JAVA_HOME/bin/java -cp $SCRIPT_HOME JbossPingerUpgraded "https://IP:7443/event-server/xmlrpc" "60000"
20 status=$?
21
22 if [ $status -eq 0 ] ; then
23 MAIL_CONTENT="HQEventServer URL is available post redeployment - Date:$(date)"
24 MAIL_SUBJECT="****** : HQEventServer URL is available post redeployment"
25 echo "Event Server is running"
26 else
27 MAIL_CONTENT="HQEventServer URL is not available post redeployment, please check immediately - Date:$(date)"
28 MAIL_SUBJECT="****** : HQEventServer URL is not available post redeployment"
29 echo "Event Server is not running"
30 fi
31
32 echo $MAIL_CONTENT | /usr/bin/mutt -F /root/.amuttrc -e 'set content_type="text/html"' "$MAIL_TO" -s "$MAIL_SUBJECT"
33
34 exit 0
35 else
36 echo "URL_CONNECTION_EXCEPTION Occured; so restarting the jboss server. $runDate"
37 $JBOSS_SERVER_HOME/bin/jboss_stop_start.sh >> /var/log/jboss/jboss_stop_start.log
38 exit 0
39 fi
40
41 }
42
43 JAVA_HOME=/usr/java/jdk1.6.0_26
44 JBOSS_SERVER_HOME=/mnt/raid5/jboss-4.2.2.GA
45 JBOSS_APP_LOCATION=$JBOSS_SERVER_HOME/server/all/farm
46 SCRIPT_HOME=/home/rapadmin/Dev_Backup/rapadmin-scripts/jboss_pinger
47
48 MAIL_TO="***@****"
49 MAIL_SUBJECT="****** JBOSS : HQ Event Server application restart"
50
51 ### ERRORS ARE DEFINED HERE ###
52 jboss_pinger_connection_exception=1
53 jboss_pinger_general_exception=2
54 jboss_pinger_input_error=3
55
56 ### URL to Check is Assigned HERE ###
57 url_to_check="https://IP:7443/opennms-event-processor/xmlrpc https://IP:7443/event-server/xmlrpc"
58 url_timeout="60000"
59
60 ### LOGIC IMPLEMENTATION BELOW ####
61 jboss_stop_start_Pid=new
62 jboss_stop_start_Pid=`ps -aef |grep -i "jboss_stop_start.sh" | grep -v grep|tr -s " " ":"|cut -d ":" -f2`
63 runDate=`date`
64
65 echo "***********************************************"
66
67 if [ ${jboss_stop_start_Pid}new != new ]
68 then
69 echo "JBoss stop_start script is already running on this machine; so no need to call jboss_stop_start from here. $runDate"
70 exit 0
71 fi
72
73 sleep 3
74
75 failure_count=0
76
77 for app_url in $url_to_check
78 do
79 $JAVA_HOME/bin/java -cp $SCRIPT_HOME JbossPingerUpgraded "$app_url" "$url_timeout"
80 result=$?
81 echo "Result is $result"
82
83 case "$result" in
84 0)
85 echo "No ERROR $runDate";
86 ;;
87 1)
88 failure_count=`expr $failure_count + 1`
89 application_restart
90 ;;
91 2)
92 failure_count=`expr $failure_count + 1`
93 application_restart
94 ;;
95 3)
96 echo "$runDate";
97 echo "JBOSS_PINGER_PROGRAM_INPUT_ERROR occured; Need to pass the complete URL as a argument. $runDate"
98 exit 1
99 ;;
100 esac
101
102 done
103
104 exit 0

Script2
========
1 #!/bin/bash
2 export PATH=/usr/java/jdk1.6.0_26/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/dell/srvadmin/bin:/opt/dell/srvadmin/sbin:/root/bin
3 export JBOSS_HOME=/mnt/raid5/jboss-4.2.2.GA
4 export APPSERVER_HOME=$JBOSS_HOME/server/all/conf/
5 export JAVA_HOME=/usr/java/jdk1.6.0_26
6
7
8 LOG_FILE_LOCATION=/var/log/jboss
9 MAILTO="***@***"
10 JBOSSIP="IP"
11
12 if [ -d $LOG_FILE_LOCATION ]
13 then
14 echo "$LOG_FILE_LOCATION Directory already exists"
15 else
16 echo "$LOG_FILE_LOCATION doesn't exists, Creating now"
17 mkdir -p $LOG_FILE_LOCATION
18 fi
19
20 jboss_Pid=`ps -aef | grep -i "jboss.Main" | grep -i "program.name=run.sh" | grep -v grep | tr -s " " ":" | cut -d":" -f2`
21
22 if [ ${jboss_Pid}new = new ] ; then
23 echo "Jboss is not running"
24 else
25 echo "Jboss is running with PID::$jboss_Pid"
26 echo -en "Stopping JBOSS process started at $(date)"
27 $JBOSS_HOME/bin/shutdown.sh -S
28 echo "Sleeping for 20 secs"
29 sleep 20
30 fi
31
32 jboss_Pid=new
33 jboss_Pid=`ps -aef | grep -i "jboss.Main" | grep -i "program.name=run.sh" | grep -v grep | tr -s " " ":" | cut -d":" -f2`
34
35 echo "The jbossPid is $jboss_Pid"
36
37 if [ ${jboss_Pid}new != new ]
38 then
39 echo "Jboss is not shutdown yet using Kill option"
40 kill -9 $jboss_Pid
41 fi
42
43 echo "Deleting tmp and work directory of JBoss"
44 rm -rf $JBOSS_HOME/server/all/tmp
45 rm -rf $JBOSS_HOME/server/all/work
46
47 #while loop
48 STOP_FLAG=0
49 COUNTER=0
50 while [ $COUNTER -lt 5 ]; do
51 if [ $(/usr/bin/nc -zv 127.0.0.1 8080 2>&1 | grep -ic succeeded) -eq 0 ]; then
52 echo -e "Jboss process is not available"
53 STOP_FLAG=1
54 break;
55 fi
56 let COUNTER=$COUNTER+1
57 sleep 10
58 done
59
60 COUNTER=0
61 START_FLAG=0
62 SCRIPT_RETURN_STATUS_FLAG=0
63
64 if [ $STOP_FLAG -eq 1 ]; then
65
66 $JBOSS_HOME/bin/run.sh -b 0.0.0.0 -c all >> $LOG_FILE_LOCATION/jboss_startup_$(date -I).log 2>&1 &
67 echo -en "Starting JBOSS service..."
68
69 while [ $COUNTER -lt 5 ]; do
70 if [ $(/usr/bin/nc -zv 127.0.0.1 8080 2>&1 | grep -ic succeeded) -eq 1 ]; then
71 echo -e "Port 8080 is listening"
72 START_FLAG=1
73 break;
74 fi
75 let COUNTER=$COUNTER+1
76 sleep 10
77 done
78
79 if [ $START_FLAG -eq 1 ]; then
80 echo -e "Jboss startup has initiated, application deployment is going on"
81 MAIL_SUBJECT="****** JBOSS($JBOSSIP) Start Stop Script Successfull - $(date '+%d.%m.%Y %H:%M')"
82 MAIL_BODY="<strong>****** JBOSS($JBOSSIP) Service was Stopped and Started Successfully</strong>"
83 echo "JBOSS started sucessfully"
84 else
85 echo -e "Jboss startup has Failed"
86 MAIL_SUBJECT="****** JBOSS($JBOSSIP) Start Stop Script Failed - $(date '+%d.%m.%Y %H:%M')"
87 MAIL_BODY="<strong>****** JBOSS($JBOSSIP) Service Starting Failed</strong>"
88 fi
89
90 else
91 MAIL_SUBJECT="****** JBOSS($JBOSSIP) Start Stop Script Failed - $(date '+%d.%m.%Y %H:%M')"
92 MAIL_BODY="<h3>****** JBOSS($JBOSSIP) Service Failed to STOP. Now running in a Unstable state</h3>"
93 echo -e "Failed"
94 echo -e "****** JBOSS($JBOSSIP) Service Failed to STOP. Now running in a Unstable state"
95
96 fi
97
98 echo $MAIL_BODY | /usr/local/bin/mutt -e 'set content_type="text/html"' "$MAILTO" -s "$MAIL_SUBJECT"
99
100
101 exit;

Last edited by Iyyappan; 12-12-2013 at 06:42 AM.
 
Old 12-12-2013, 06:58 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
please use [code]here comes your script[/code] tags to keep formatting
you can try to save stdout and stderr of your scripts and set -xv to see what's happened
 
1 members found this post helpful.
Old 12-12-2013, 07:28 AM   #3
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Original Poster
Rep: Reputation: 4
I do not get any error. I receive message that Script is started successfully, but the applications inside is not getting deployed fully when executed over cron alone.
 
Old 12-12-2013, 07:42 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
That's why you should try insert set -xv at the beginning of the scripts.
 
Old 12-12-2013, 10:10 PM   #5
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Original Poster
Rep: Reputation: 4
Can you let me know how to set it.... is there any format to set
 
Old 12-12-2013, 10:37 PM   #6
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Original Poster
Rep: Reputation: 4
Should i do like this

I have updated script with below

. ~/.bash_profile

set > /tmp/myEnvrionrment.$$
 
Old 12-13-2013, 12:19 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
no, you need to insert it set -xv as second line in your script:
Code:
#!/bin/bash
set -xv
....
 
Old 12-13-2013, 12:21 PM   #8
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
or just call it manually bash -xvv /path/to/script.sh
 
1 members found this post helpful.
  


Reply



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
Bash script not performing a check properly da_667 Programming 15 08-06-2012 03:16 PM
[SOLVED] Script not working in cron but working fine manually jasperux Linux - Newbie 2 07-04-2012 11:28 PM
Script not running properly in cron though runs well if run manually. linuxlover.chaitanya Linux - Newbie 4 01-15-2009 03:31 AM
help: using cron + my bash script --> don't want mail from script beeblequix Linux - General 7 11-23-2007 09:25 PM
bash script from cron kubla Programming 3 01-22-2004 04:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:12 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
Open Source Consulting | Domain Registration