mongodb monitor and automatically restart the script after the service hang

Today encounter a situation, one for data visualization mongodb server mongodb service when performing data extraction cases hang often, so write a little script. Mongodb monitoring service is started, if the service starts automatically hang restart the service.

This script also apply to other services. Such as: nginx, mysql, redis, etc.

Service startup script code is as follows

#! / bin / bash 
# Check whether the root user 

IF [$ (the above mentioned id -u)! = "0"] 
the then 
echo "Not at The root the User! sudo the Try a using the Command!" 
Exit 1 
fi 
# monitoring service that is alive, here are monitored by monitoring the service port, where you can be replaced with other services 
netstat -anop | grep 127.0.0.1:27017 
[? -ne $ 1] IF 
the then 
Exit 
fi 

echo $ (DATE + T%%% the n-F) " MongoDB services the restart ">> mongodb.log 
# restart the service 
/script/mongodb-27017.sh restart

  


Join the script task. Tested once every 1min

crontab -e

*/1 * * * * sh /sh/mongodb.sh

 

Guess you like

Origin www.cnblogs.com/adjk/p/12145426.html