在最后一分钟监视一个 txt 文件是否缺少修改并发邮件给我
Monitor a txt file for lack of modification in the last minute and mail me
我正在寻找检查 txt 文件在过去 10 秒内是否未被修改的最佳方法。它必须每分钟自动检查一次,如果它导致缺少修改,它必须发送电子邮件给我。
将使用 script/software 的服务器是 运行,位于 Windows。
详情
我有一个软件,每分钟编辑一个 txt 文件,让我知道它仍然是 运行。如果软件无法编辑 txt 文件,则意味着它不再 运行,必须尽快通知我,所以我需要一个 script/software 来完成。
您可以尝试使用像这样的简单 powershell 脚本
#endless loop
while ($true)
{
#Getting difference between last write time for your TXT, and current system time, converting it to seconds
$diff=((Get-ChildItem C:\Temp\YourTXTfile.log).LastWriteTime - (get-date)).totalseconds
#Check, if it greater than 10 seconds
if ($diff -gt -10)
{
#you dont need this write-host in your prod, so remove it after testing
Write-Host "Everything is ok. File last modified $diff seconds ago. Next check in 10 seconds"
Start-Sleep -Seconds 10
}
else
{
Send-MailMessage -To "some@mail.com" -Body "Seems like our app is down" -SmtpServer "yourSMTP" -Subject "Etc, etc"
#you dont need this write-host in your prod, so remove it after testing
Write-Host "Mail report out, next check will be performed in one hour"
Start-Sleep -Seconds 3600
}
}
并将其放入任务调度程序 运行 作为后台任务。
我正在寻找检查 txt 文件在过去 10 秒内是否未被修改的最佳方法。它必须每分钟自动检查一次,如果它导致缺少修改,它必须发送电子邮件给我。
将使用 script/software 的服务器是 运行,位于 Windows。
详情
我有一个软件,每分钟编辑一个 txt 文件,让我知道它仍然是 运行。如果软件无法编辑 txt 文件,则意味着它不再 运行,必须尽快通知我,所以我需要一个 script/software 来完成。
您可以尝试使用像这样的简单 powershell 脚本
#endless loop
while ($true)
{
#Getting difference between last write time for your TXT, and current system time, converting it to seconds
$diff=((Get-ChildItem C:\Temp\YourTXTfile.log).LastWriteTime - (get-date)).totalseconds
#Check, if it greater than 10 seconds
if ($diff -gt -10)
{
#you dont need this write-host in your prod, so remove it after testing
Write-Host "Everything is ok. File last modified $diff seconds ago. Next check in 10 seconds"
Start-Sleep -Seconds 10
}
else
{
Send-MailMessage -To "some@mail.com" -Body "Seems like our app is down" -SmtpServer "yourSMTP" -Subject "Etc, etc"
#you dont need this write-host in your prod, so remove it after testing
Write-Host "Mail report out, next check will be performed in one hour"
Start-Sleep -Seconds 3600
}
}
并将其放入任务调度程序 运行 作为后台任务。