LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-18-2012, 07:14 PM   #1
imadork8317
LQ Newbie
 
Registered: Dec 2012
Posts: 5

Rep: Reputation: Disabled
need help with remote ssh script


I have a Tomcat service running on a remote server. What i would like to do is run a script so that it will stop the tomcat service, check to make sure it is stopped, then remove my war file and replace it with the latest version.

I have this working for the most part, my struggle is verifying that the service is stopped.

My initial thought was to do if then else, but i'm not sure how to send that command through ssh. i dont think its possible.

so here is what i do now.

Code:
ssh admin@$BOX.com "/bin/Tomcat7.sh stop"
ssh admin@$BOX.com "ps auxww | grep $TOMCAT
ssh admin@$BOX.com "rm /webapps/console.war"
scp -p /builds/$BUILD/console.war admin@$BOX.com:/webapps/console.war
ssh admin@$BOX.com "bin/Tomcat7.sh start; tail -100f /logs/catalina-daemon.out"
I have ssh password-less setup, so i need a way to verify that the tomcat service is stopped before trying to remove and copy the new file.
 
Old 12-18-2012, 09:23 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
How about using Inotify to watch the directory where you copy the .war file to and make it execute a script that stops Tomcat, replaces the .war file, starts Tomcat and greps last 500 log lines for common errors? That way the only thing you need to do to trigger a replacement is send the file over.
 
Old 12-18-2012, 09:34 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,668

Rep: Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711Reputation: 711
Quote:
Originally Posted by imadork8317 View Post
ssh admin@$BOX.com "ps auxww | grep $TOMCAT
This probably won't do what you want because it may find the grep command itself.

stop should wait for the service to end. If you do need a loop, wait on the pid file being removed instead.
 
Old 12-19-2012, 09:36 AM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
you could also do something along this line:

Code:
killProcs ()

{

>/tmp/proclist
for i in `ps -e | grep Tomcat7.sh | grep -v grep | awk -F" " '{print $4}'`
do
    if [ $i != Tomcat7.sh ]
    then
        echo $i >>/tmp/proclist
    fi
	sleep 1
	echo "Killed proc $i" >>${logname}
    killall $i
done

}
and then to restart you could do something like this:

Code:
startProcs()
{

for i in `cat /tmp/proclist`
do
    echo "Starting $i" >>${logname}
    $HOME/$i &
    sleep 1
    ps -ef | grep -v grep | grep $i
    if [ $? -eq 0 ]
    then
        echo "Process $i - started ok" >>${logname}
    fi
done

}
that might help.
 
Old 12-19-2012, 10:12 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
Quote:
Originally Posted by lleb View Post
for i in `ps -e | grep Tomcat7.sh | grep -v grep | awk -F" " '{print $4}'`
If I may suggest "while" loops should be preferred over "for" loops and the whole "ps|grep" line can be replaced with a single 'pgrep':
Code:
killProcs() { 
pgrep -f 'Tomcat7.sh' | while read PID; do
 # Allow to write out data
 kill -15 $PID >/dev/null 2>&1 && sleep 2s
 # ...and then kill it good
 kill -9 $PID >/dev/null 2>&1; done
}
 
1 members found this post helpful.
  


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] my shell Script stops after doing ssh to remote server Shasank Linux - Newbie 14 05-11-2012 11:17 AM
How to use ssh and run command in the remote machine using shell script salmanmanekia Linux - Newbie 8 04-28-2011 08:41 AM
ssh - using variables in call to start remote script from local script babag Linux - Networking 2 06-03-2008 05:50 PM
shell script for remote ssh login sefaklc Programming 5 08-28-2007 10:03 PM
exacuting remote comands over SSH with a shell script? dasy2k1 Linux - Software 1 09-05-2006 08:01 PM


All times are GMT -5. The time now is 12:22 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