LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   automating server startup tomcat in bash (http://www.linuxquestions.org/questions/linux-newbie-8/automating-server-startup-tomcat-in-bash-4175459856/)

figure20012 04-28-2013 03:41 AM

automating server startup tomcat in bash
 
while ["true" ]; do tail -n 3 catalina.out; sleep 1 ; if [ `tail -n 3 catalina.out | sed -n '/startup/p'` ] then break; echo "tomcat server started";fi clear; done

Can some please suggest why the bash command is not working please?

unSpawn 04-28-2013 05:04 AM

By the looks of it: missing space, missing semicolon, relative file path, unknown log file contents.
Run your script as
Code:

/bin/bash -xv /path/to/script 2>&1 | tee /tmp/script.txt
That should give you debug output in "/path/to/script" to figure things out.
If unsure attach it and add a description of where you think it fails.

shivaa 04-28-2013 05:10 AM

Just try this, and let's know the error, if any:
Code:

while true; do tail -n 3 catalina.out; sleep 1; if [ `tail -n 3 catalina.out | sed -n '/startup/p'` ]; then; break; echo "tomcat server started"; fi; clear; done
Or create a script and invoke it:
Code:

#!/bin/bash
while true
do
tail -n 3 catalina.out
sleep 1
  if [ `tail -n 3 catalina.out | sed -n '/startup/p'` ]; then
  break
  echo "tomcat server started"
  fi
clear
done


unSpawn 04-28-2013 05:51 AM

Shame, really. Doesn't teach him anything...

shivaa 04-28-2013 07:47 AM

Quote:

Originally Posted by unSpawn (Post 4940397)
Shame, really. Doesn't teach him anything...

In what sense you're saying this... Did we miss anything?

chrism01 04-28-2013 08:32 AM

I think UnSpawn meant that by telling the OP to use '-xv' switches, he would be likely to be able to figure it out for himself, as well as learning how to debug bash.

figure20012 04-28-2013 10:49 AM

Thanks for helping me.

#!/bin/bash
while [ true ]
do
res=`tail -n 1 catalina.out`
if [ `echo $res | grep start ` ];then
break
echo "tomcat server startup"
fi
sleep 2
clear
done

I am having error when server startup appears at the end of catalina.out.If i put only startup the loop breaks.

I am getting the following error
script.sh: line 6: [: server: unary operator expected

figure20012 04-28-2013 11:00 AM

it works now
the problem was with the if condition

if [ "`echo $res | grep start`" ]

thank you all :)

shivaa 04-28-2013 11:54 AM

Good. In case if it has solved, you can mark the thread as solved (option is available under Thread Tools on top menu).

Here is a guide which will be helpful for you: http://tldp.org/LDP/abs/abs-guide.pdf

unSpawn 04-28-2013 03:04 PM

Quote:

Originally Posted by chrism01 (Post 4940479)
I think UnSpawn meant that by telling the OP to use '-xv' switches, he would be likely to be able to figure it out for himself, as well as learning how to debug bash.

Indeed, because being able to debug them should be a basic skill anyone who handles scripts should have.


People ignoring debug mode (or not pointing them to it) leads to the OP posting stuff like this:
Quote:

Originally Posted by figure20012 (Post 4940560)
Code:

script.sh: line 6: [: server: unary operator expected

as debug mode would actually show how BASH parses things (efficiency thing).


Quote:

Originally Posted by shivaa (Post 4940595)
Good. In case if it has solved

Not so fast.

You're an admin so you should understand the implications of greedy matches:
Code:

if [ "`echo $res | grep start`" ]
and while the 'sleep' slows searching down "start" still matches any of:
Code:

^.*org\.apache\.catalina\.startup.*$
(which can be quite a lot) and the only full message the OP should be looking for is:
Code:

^INFO:.Server.startup.in.*ms$

This means the script could look like this (no grep reqqed):
Code:

#!/bin/bash
while [ true ]; do sleep 2s
 OUTPUT=$(tail -n 1 catalina.out); REGEX="INFO: Server startup in"
 [[ $RES =~ $REGEX ]] && { echo "Tomcat started."; break; }
done
exit 0


Some BASH scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.gnu.org/software/bash/man...ode/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls


All times are GMT -5. The time now is 12:01 AM.