restart process based on file

Hi all.
I do have a script "startApp.sh" (app result is a file /opt/extract/appextract.txt)

I have no problems with stopping app

var1=`ps -ef | grep -v grep | grep MyApp | awk '{print $2}'`
kill -9 $var1

What I want to achieve is:

I start app, app is doing some extraction, after extraction is done i would like to stop app (kill app if/when) wait some time (~ 30 sec. different processes/scripts taking place) and start app again (until next extract)

I will try to be more schematic

  • start App
  • app is running, doing stuff, and creates a file
  • when file is created I have to stopApp
  • wait ~30sec
  • start app

Could you please guide me how to create some script that will be in "constant loop" or a service and will watch for that file, if exist do somethinh (kill my app)

thank you very much
e-l-diablo

Use CODE-tags when displaying code, data or logs to enhance readability and to preserve formatting like indention etc., ty.

Script could look like:

#!/bin/sh

./app start

while :; do
     sleep 1 # just to give it short breaks
     if [[ -e /<path-to-your-output-file>/filename ]]; then
          ./app stop
          sleep 30
          break
     fi
done

./app start

exit 0

perfect.
Wht I dod not think about that.
That is what I was searching for. I will give a try and let you know if it works as it should (there are more scripts in that process)
very, very much thank you (i will do some modifications, i already see some options)

Hi again. After testing it does not work

What i have done:
I have script called startMe.sh. It starts app and should kill when file in specific directory exists.

#!/bin/sh
rm /opt/tmp/restart.txt *that removes restart file if exist

./startApp.sh

while :; do
     if [[ -e /opt/tmp/restart.txt ]]; then
          ./killapp.sh
          sleep 30
          break
     fi
done

./startApp.sh
exit 0

killapp.sh looks like

var1=`ps -ef | grep -v grep | grep java | grep username | awk '{print $2}'`
kill -9 $var1

Where am i doing something wrong?

No idea if this is a typo:

... *that removes restart file if exist

Comments should start with a # nor a *.

Is ./startApp.sh touching that flag file?
Also what is not working? A bit more details could help, ty.

Hi.

*that removes restart file if exist 

That was FYI
Few more details. I am running Solaris 10 on SUN x4450 box (really nice work horse) with 128GB RAM.
when I run ./startApp.sh all works as it should: my application starts
and i get messages (all the time)

./startApp.sh: [[: not found
./startApp.sh: [[: not found
./startApp.sh: [[: not found
./startApp.sh: [[: not found
./startApp.sh: [[: not found
./startApp.sh: [[: not found
./startApp.sh: [[: not found
./startApp.sh: [[: not found
./startApp.sh: [[: not found
./startApp.sh: [[: not found
./startApp.sh: [[: not found
./startApp.sh: [[: not found
./startApp.sh: [[: not found
./startApp.sh: [[: not found

when i place killapp.txt under /opt/tmp nothing, just nothing. That is strange becaus in theory should work.

So your script ./startApp.sh seems to have a problem.

OK I have scripts (i renamed them and done some maintenance)
a) startTOMCAT.sh
b) startApp.sh
c) killapp.sh

startTOMCAT.sh

#!/bin/csh
echo $CATALINA_HOME
$CATALINA_HOME/bin/catalina.sh start

startApp.sh

#!/bin/sh
rm /opt/tmp/restart.txt     

./startTOMCAT.sh            

while :; do
     if [[ -e /opt/tmp/restart.txt ]]; then
          ./killapp.sh            
          sleep 30               
          break
     fi
done

./startTOMCAT.sh

exit 0

and killapp.sh

#!/bin/sh
var1=`ps -ef | grep -v grep | grep java | grep username| awk '{print $2}'`
kill -9 $var1

Looks flawless to me. I checked it and it works.
Do you still get that

all the time?

Try single instead of double [] and see if something changes. What shell is your system's default? bash? ksh? ...?

That is what i do not understand. It shouldd work
my default is bash (i like it)
I removed "[[" and left "[". It is better now.
that is the output

/opt/tmp/restart.txt: No such file or directory
Using CATALINA_BASE:   /opt/apps/tomcat
Using CATALINA_HOME:   /opt/apps/tomcat
Using CATALINA_TMPDIR: /opt/apps/tomcat/temp
Using JRE_HOME:       /opt/java/1.6/
./startApp.sh: test: argument expected

That is what i get. I have just copied restart.txt to /opt/tmp/ and app is still working - no restart.

Hm, change the #!/bin/sh to #!/usr/bin/bash and make sure your bash is located there.
Try again.

You are THE MAN. I always used #!/bin/sh or #!/bin/csh and had never problems before
That fixed it. Additionally I had to change that sctipt (small loop) because it did the stop/start/restart 1only. If i want to doit more than 1 it will not work. I have to call startApp.sh from startApp.sh (i know i should not)
startApp.sh

#!/bin/bash
rm /opt/tmp/restart.txt
./startTOMCAT.sh

while :; do
     if [ -e /opt/tmp/restart.txt ]; then
          ./killapp.sh
          rm /opt/tmp/restart.txt   
          sleep 30
          break
     fi
done
./startApp.sh
exit 0

Thank you