我有一台服务器运行着一个网站,由于某种原因,它随时会死机。不知道为什么,我知道我要描述的这个解决方案只是一种灭火方法,而不是解决问题。但现在没关系。
所以,我想在服务器上运行一个 Powershellscript,它会持续检查网站是否可用,并在此基础上执行一些魔法来让它再次运行。
到目前为止,我有这个代码。
当我打开 Apache 并运行脚本时,“运行”功能正在工作。当我关闭 Apache 并运行脚本时,“未运行”功能正在工作。
但问题是,当我启动 Apache 时,运行脚本并且“运行”不断循环,我打开 Apache(脚本仍在运行)我希望它继续“不运行”,解决问题,启动我的服务再次并从头开始检查站点是否已启动并转到“运行”。但它卡在“正在运行”并且不会变为“未运行”。
知道我怎么能做到这一点吗?
(为了我的公司,我编辑了脚本以取出真实的进程和服务名称)
function checkSite
{
[string] $url = 'http://websitegoeshere.com'
[net.httpWebRequest] $req = [net.webRequest]::create($url)
$req.Method = "HEAD"
[net.httpWebResponse] $res = $req.getResponse()
while ($res.StatusCode -ge "200") {running} notrunning
}
function running
{
cls;
Write-Host " -=IS-checker=-" -fore yellow
Write-Host "" -fore green
Write-Host "----------------------" -fore green
Write-Host " Access to web = OK " -fore green
Write-Host "----------------------" -fore green
sleep 60
}
function notrunning
{
cls;
Write-Host "-=IS-checker=-" -fore yellow
Write-Host "" -fore red
Write-Host "-------------------------" -fore red
Write-Host " Site down - Site Down " -fore red
Write-Host "-------------------------" -fore red
sleep 2
$process = "process-name-goes-here"
Write-Host "Stopping process" $process -fore red
Stop-Process -processname $process
sleep 5
$service = "service-name-goes-here"
Write-Host "Killing service" $service -fore red
Stop-Service $service -Force
sleep 5
Write-Host "Starting service" $service -fore green
Start-Service $service
sleep 5
$LogTime = Get-Date -Format "yyyy-MM-dd hh-mm-ss"
$LogFile = 'C:\PSLog\'+"LOG_"+$LogTime+".log"
$LogTime+" Stopped "+$process+", Stopped "+$service+", Started "+$service | Out-File $LogFile
Write-Host "Logfile written to"$LogFile -fore green
sleep 5
checkSite
}
检查网站