मुझे अपनी जरूरतों के अनुरूप कुछ भी नहीं मिला, इसलिए मैंने थोड़ा पावरहेल स्क्रिप्टिंग सीखा और एक समाधान लुढ़का जो दूसरों के लिए भी उपयोगी होना चाहिए। विंडोज प्लेटफ़ॉर्म मानना (अन्यथा मोनिट का उपयोग करें!), पावरहेल वास्तव में शक्तिशाली और आसान है।
नमूना-monitor.ps1 स्क्रिप्ट:
$webClient = new-object System.Net.WebClient
###################################################
# BEGIN USER-EDITABLE VARIABLES
# the URL to ping
$HeartbeatUrl = "http://someplace.com/somepage/"
# the response string to look for that indicates things are working ok
$SuccessResponseString = "Some Text"
# the name of the windows service to restart (the service name, not the display name)
$ServiceName = "Tomcat6"
# the log file used for monitoring output
$LogFile = "c:\temp\heartbeat.log"
# used to indicate that the service has failed since the last time we checked.
$FailureLogFile = "c:\temp\failure.log"
# END USER-EDITABLE VARIABLES
###################################################
# create the log file if it doesn't already exist.
if (!(Test-Path $LogFile)) {
New-Item $LogFile -type file
}
$startTime = get-date
$output = $webClient.DownloadString($HeartbeatUrl)
$endTime = get-date
if ($output -like "*" + $SuccessResponseString + "*") {
# uncomment the below line if you want positive confirmation
#"Success`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds" >> $LogFile
# remove the FailureLog if it exists to indicate we're in good shape.
if (Test-Path $FailureLogFile) {
Remove-Item $FailureLogFile
}
}
else {
"Fail`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds" >> $LogFile
# restart the service if this is the first time it's failed since the last successful check.
if (!(Test-Path $FailureLogFile)) {
New-Item $FailureLogFile -type file
"Initial failure:" + $startTime.DateTime >> $FailureLogFile
Restart-Service $ServiceName
}
}
इस स्क्रिप्ट में केवल तर्क यह है कि यह केवल एक बार एक प्रारंभिक विफलता के बाद सेवा को पुनः आरंभ करने की कोशिश करेंगे है। यह ऐसी स्थिति को रोकने के लिए है जहां किसी सेवा को पुनरारंभ करने में कुछ समय लगता है, और जब यह पुनरारंभ होता है, तो मॉनिटर विफलता को देखता रहता है और फिर से पुन: प्रारंभ होता है (खराब अनंत लूप)। अन्यथा आप कुछ भी कर सकते हैं, जैसे ईमेल अधिसूचनाएं जोड़ें, या बस एक सेवा को पुनरारंभ करने से ज्यादा कुछ करें।
यह स्क्रिप्ट एक बार निष्पादित होगी, जिसका अर्थ है कि आपको बाहरी पुनरावृत्ति को बाहरी रूप से नियंत्रित करने की आवश्यकता होगी। आप इसे स्क्रिप्ट में एक अनंत लूप में डाल सकते हैं, लेकिन यह थोड़ा सा झटकेदार लगता है। मैंने विंडोज़ टास्क शेड्यूलर का इस्तेमाल किया, इसे इस तरह निष्पादित किया: प्रोग्राम: पावरहेल.एक्सई तर्क: -कॉम "सी: \ परियोजनाएं \ foo \ scripts \ monitor.ps1" -noprofile प्रारंभ करें: सी: \ projects \ foo \ स्क्रिप्ट
आप विजुअलक्रोन जैसे अधिक मजबूत शेड्यूलर का उपयोग कर सकते हैं, इसे विंडोज़ सेवा में प्लग कर सकते हैं, या क्वार्ट.NET जैसे अनुप्रयोग सर्वर शेड्यूलर के माध्यम से। मेरे मामले में कार्य शेड्यूलर ठीक काम करता है।
स्रोत
2009-06-19 02:37:13
हा पुनः आरंभ नहीं किया! - एक 7 साल का सवाल और जवाब पकड़ पर रखा गया। –