LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need help with remote ssh script (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-remote-ssh-script-4175442054/)

imadork8317 12-18-2012 06:14 PM

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.

unSpawn 12-18-2012 08:23 PM

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.

smallpond 12-18-2012 08:34 PM

Quote:

Originally Posted by imadork8317 (Post 4852296)
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.

lleb 12-19-2012 08:36 AM

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.

unSpawn 12-19-2012 09:12 AM

Quote:

Originally Posted by lleb (Post 4852864)
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
}



All times are GMT -5. The time now is 08:34 PM.