Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

AIX - Restarting proccesses and removing files

I'm currently writing a script that stops and starts tomcat, but also check if TomcatApp is still running before it starts it up. Also, removes tomcat tmp files older than 24 hours.

#!/bin/ksh

find /usr/tomcat/TomcatApp/tmp -type f -mtime +1 -print | xargs rm (is this correct?)

sleep 300

/usr/tomcat/TomcatApp/bin/shutdown.sh

sleep 60

Help with the If, Then, Else statement??? ps -ef |grep TomcatApp, if its still running, kill it, if its not running then start it up using /usr/tomcat/TomcatApp/bin/startup.sh

exit

Please help
0
AIX25
Asked:
AIX25
  • 5
  • 4
  • 2
2 Solutions
 
omarfaridCommented:
can you post the output of ps -ef command when the tomcat is running?
0
 
AIX25Author Commented:
servername:/> ps -ef |grep -i TomcatApp
 AppGroup 61603854        1   0 13:57:08  pts/1  0:24 /usr/java5/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/inst/tomcat/TomcatApp/apache-tomcat-5.5.26/conf/logging.properties -server -Xms96m -Xmx512m -XX:MaxPermSize=128m -Djava.awt.headless=true -Dfile_encoding=UTF-8 -Djava.io.tmpdir=/var/tmp -Djava.endorsed.dirs=/inst/tomcat/TomcatApp/apache-tomcat-5.5.26/common/endorsed -classpath :/inst/tomcat/TomcatApp/apache-tomcat-5.5.26/bin/bootstrap.jar:/inst/tomcat/TomcatApp/apache-tomcat-5.5.26/bin/commons-logging-api.jar -Dcatalina.base=/inst/tomcat/TomcatApp/apache-tomcat-5.5.26 -Dcatalina.home=/inst/tomcat/TomcatApp/apache-tomcat-5.5.26 -Djava.io.tmpdir=/inst/tomcat/TomcatApp/apache-tomcat-5.5.26/temp org.apache.catalina.startup.Bootstrap start
    root 16581016 43188236   0 14:47:22  pts/1  0:00 grep -i TomcatApp
servername:/>
0
 
omarfaridCommented:
sorry but I am not tomcat expert. How do you know if it is running or not?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
woolmilkporcCommented:
USERID="AppGroup"
APP="TomcatApp"

echo "Stopping ${APP}"
/usr/tomcat/TomcatApp/bin/shutdown.sh
sleep 60

PID=$(ps -ef|grep ${USERID} |grep -i ${APP}|grep -v grep|awk '{print $2}')

TRIES=10
i=1
while [ ! -z "${PID}" ] ; do
 if [ ${i} -ge ${TRIES} ]; then
     echo "Attempting kill -9"
     kill -9 ${PID} ; sleep 10; break
  fi
  echo "Tomcat still running. Killing the beast. Attempt ${i} of ${TRIES}"
  kill ${PID}
  sleep 10
  ((i+=1))
  PID=$(ps -ef|grep ${USERID} |grep -i ${APP}|grep -v grep|awk '{print $2}')
 done

PID=$(ps -ef|grep ${USERID} |grep -i ${APP}|grep -v grep|awk '{print $2}')
if [ ! -z "${PID}" ] ; then
 echo "Tomcat seems immortal. Exiting"
  exit
fi

echo "Starting ${APP}"
 /usr/tomcat/TomcatApp/bin/startup.sh

Open in new window

I didn't have the chance to test it. Please be careful.

The find/xargs/rm thing is OK, by the way.
0
 
AIX25Author Commented:
Ok, I finally got an approved outage window to test the restart of the application. I'm going to try it today and update my outcome.
0
 
AIX25Author Commented:
I need some clarification on a couple of things.

We run our startups and shutdowns as root like this:
su - AppID -c /usr/tomcat/TomcatApp/bin/startup.sh
su - AppID -c /usr/tomcat/TomcatApp/bin/shutdown.sh

Can I replace line 5 and 31, with the commands above? Will that work fine with the script setup?

Also, on line 1 and 2 for the USERID= and APP=, do I put the real userid and app names? Also, with or without quotes?
0
 
woolmilkporcCommented:
The commands you posted will work, no problem.

USERID must contain what appears in the ps list, as well as APP.

According to the sample output you posted that's "AppGroup" and "TomcatApp".

According to your last post use "AppID" in the USERID variable (the userid you "su" to).
"TomcatApp" in the APP variable seems to be OK, because it appears multiple times in the directory paths and seems to be unique processlist-wide. If it's not unique it's up to you to search for a better suited string.

As long as you don't have spaces or metacharacters in the variable data quotes are a matter of taste. I always use them to be on the safe side.
0
 
AIX25Author Commented:
Ok, great! Thanks for the final detail. Is there a way to output what was ran or what happened during the script execution and output it to /tmp/tomcat.output? Also, anyway you can walk me through your script, so I can get a better understanding??
0
 
woolmilkporcCommented:
Add as line 3:

exec 1>/tmp/tomcat.output 2>&1

This will write all messages and errors of the script to the specified file.

Yes, I can walk you through the script, but please give me some time.
0
 
woolmilkporcCommented:
Ok, here is the walk-through:

Lines 1-6: Variable assignments and call to the application's stop script.

Line 8 tries to find the appication's PID by searching the process list for the USERID/APP combo via grep and, if found, prints it out via awk ($2 = field 2 of the ps list) in order to fill the variable PID with it. PID will be empty if the application has already stopped running.
This command will appear several times, because we have to verify the results of our "kill" attempts.

Line 10-11: set the number of regular "kill" attempts (TRIES) to issue before trying "kill -9" and initialize the respective counter to 1 ("i")

Line 12 starts the "kill" loop, setting the break condition to PID no longer being found in ps.

Lines 13-16 check whether the regular "kill" counter (i) has already reached its maximum (TRIES). If so, issue a message, run "kill -9" and break out of the loop.

Line 17-20:  Write out a message, attempt the regular "kill", sleep for 10 seconds, increment the loop counter.

Line 21 (same as line 8): Recheck for the presence of the application's PID in ps.

Line 22 loop back to line 12.

Line 24: We come here either when PID has indeed gone (kill has worked) or when we have issued the kill -9 and voluntarily left the loop. Thus we must recheck the PID (see line 8) to find out what it's all about.

Lines 25-28: If PID is still present we must assume that even "kill -9" hasn't helped,
so we issue a message and give up (leave the script via "exit").

Line 30: We only come here if we actually succeeded in killing the beast, so we can
happily bring it to life again, so we issue a message, run the application's start script and we're done.

The number of regular "kill" attempts (TRIES=10) and the sleep time inbetween (sleep 10)
are arbitrarily set by me. Please feel free to adjust them if necessary.

Since it might be true that a (Tom)cat has nine lives I set TRIES to 10 ;-))

Btw. I found a minor mistake: Line 18 must read

if [ ${i} -gt ${TRIES} ]; then

instead of "-ge".
0
 
AIX25Author Commented:
Ok, great! Thanks for clearly expalining the script! I wish there were bonuses given as to how good you are and explain things at a true expert level. I have a question that I should be submitting this week, another script. I'm working on it now and trying to understand what is needed exactly. I will definitely need your help on that one! :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now