Search code examples
shellscriptinglogicshwebsphere-8

Skip the line of execution in shell script if the line take more time to execute


I have a shell script which will stop the JVM in IBM WebSphere

As like below

//Some code 
sh /application/IBM/WebSphere/AppServer/profiles/AppSrv01/bin/stopServer.sh JMS-Server -username admin -password ...... 
echo "Stop xyzServer JVM" 
//Some code

My problem is , when sometimes it take lot of time to stop the server,hence, script will keep on run and will never end

I need a solution to skip this line of execution, if this code waits for the finish of JVM stop, if JVM is not stopped after certain time, execution should move on to the next line

Could you please help?


Solution

  • Use the timeout command:

    //Some code 
    timeout 180 sh /application/IBM/WebSphere/AppServer/profiles/AppSrv01/bin/stopServer.sh JMS-Server -username admin -password ...... && echo "Stopped xyzServer JVM" || echo "xyzServer JVM stop, timeout" 
    //Some code
    

    Run the stopServer.sh script and if it doesn't return an exit code in 180 seconds or the exit code is not 0, print "xyzServer JVM stop, timeout" else print "Stopped xyzServer JVM"