Skip to content

Instantly share code, notes, and snippets.

@jesty
Last active May 30, 2016 21:06
Show Gist options
  • Save jesty/8c03b619194741b75edfa9f73c268d2b to your computer and use it in GitHub Desktop.
Save jesty/8c03b619194741b75edfa9f73c268d2b to your computer and use it in GitHub Desktop.
Restart service if an url doesn't works (doesn't return 200 http status). In this example I restart an Apache Tomcat service.
#!/bin/bash
#service monitoring
#check if the url responds 200 in 60 seconds
timeout -s SIGTERM 60 curl -I https://youurl.com | grep '200 OK'
#> /dev/null
a=$(echo $?)
if test $a -ne 0
then
echo "tried to restart" > laststatus.txt
echo "restart tomcat service"
echo "restarted at $(date)" >> lastrestarts.log
service tomcat stop
#kill tomcat to be sure, that the service will stop. You can commit this line.
kill -9 $(jps | grep 'Bootstrap' | awk '{print $1}')
#restart the service
service tomcat start
else
echo "tomcat service running"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment