通过Windows服务检查批处理文件是否正在执行

时间:2012-06-21 10:35:56

标签: c# windows-services scheduled-tasks

我有一个场景,我必须创建一个Windows服务,它可以定期检查批处理文件,如果该文件没有执行,那么服务应该开始执行相同的操作。如果它正在执行,那么什么都不做。

目前我正在使用计划任务执行此操作,但计划任务有一个限制,我必须猜测我的批处理文件的预期执行时间,并相应地设置计划任务。

我希望我的服务会以5分钟的间隔检查批处理文件的执行情况,如果文件没有执行,那么它应该开始执行批处理文件。

提前致谢。

2 个答案:

答案 0 :(得分:1)

您可以更改批处理文件,以便在启动时创建文件,并在完成后删除相同的文件。

该服务可以简单地检查文件是否存在。

在服务中:

while(_running)
{
    if(!File.Exists("..."))
    { 
        // start batch job
    }
    Thread.Sleep(5 * 60 * 1000);
}

答案 1 :(得分:-1)

这是可以做什么的例子:

@echo off

color 1F


@sc query >%COMPUTERNAME%_START.TXT

ECHO REPORT MISSING INSTALL SERVICES >%COMPUTERNAME%.TXT


find /I "AcPrfMgrSvc" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "AcPrfMgrSvc"

IF ERRORLEVEL 1 REG IMPORT REG\AcPrfMgrSvc.REG >>%COMPUTERNAME%.TXT && ECHO AcPrfMgrSvc >>%COMPUTERNAME%.TXT

find /I "AcSvc" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "AcSvc"

IF ERRORLEVEL 1 REG IMPORT REG\AcSvc.REG >>%COMPUTERNAME%.TXT && ECHO AcSvc >>%COMPUTERNAME%.TXT

find /I "AudioSrv" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "AudioSrv"

IF ERRORLEVEL 1 REG IMPORT REG\AudioSrv.REG >>%COMPUTERNAME%.TXT && ECHO AudioSrv >>%COMPUTERNAME%.TXT

find /I "BITS" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "BITS"

IF ERRORLEVEL 1 REG IMPORT REG\BITS.REG >>%COMPUTERNAME%.TXT && ECHO BITS >>%COMPUTERNAME%.TXT

find /I "btwdins" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "btwdins"

IF ERRORLEVEL 1 REG IMPORT REG\btwdins.REG >>%COMPUTERNAME%.TXT && ECHO btwdins >>%COMPUTERNAME%.TXT

find /I "CcmExec" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "CcmExec"

IF ERRORLEVEL 1 REG IMPORT REG\CcmExec.REG >>%COMPUTERNAME%.TXT && ECHO CcmExec >>%COMPUTERNAME%.TXT

find /I "CryptSvc" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "CryptSvc"

IF ERRORLEVEL 1 REG IMPORT REG\CryptSvc.REG >>%COMPUTERNAME%.TXT && ECHO CryptSvc >>%COMPUTERNAME%.TXT

find /I "DcomLaunch" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "DcomLaunch"

IF ERRORLEVEL 1 REG IMPORT REG\DcomLaunch.REG >>%COMPUTERNAME%.TXT && ECHO DcomLaunch >>%COMPUTERNAME%.TXT

find /I "Dhcp" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Dhcp"

IF ERRORLEVEL 1 REG IMPORT REG\Dhcp.REG >>%COMPUTERNAME%.TXT && ECHO Dhcp >>%COMPUTERNAME%.TXT

find /I "dmserver" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "dmserver"

IF ERRORLEVEL 1 REG IMPORT REG\dmserver.REG >>%COMPUTERNAME%.TXT && ECHO dmserver >>%COMPUTERNAME%.TXT

find /I "Dnscache" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Dnscache"

IF ERRORLEVEL 1 REG IMPORT REG\Dnscache.REG >>%COMPUTERNAME%.TXT && ECHO Dnscache >>%COMPUTERNAME%.TXT

find /I "DozeSvc" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "DozeSvc"

IF ERRORLEVEL 1 REG IMPORT REG\DozeSvc.REG >>%COMPUTERNAME%.TXT && ECHO DozeSvc >>%COMPUTERNAME%.TXT

find /I "EEPROMService" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "EEPROMService"

IF ERRORLEVEL 1 REG IMPORT REG\EEPROMService.REG >>%COMPUTERNAME%.TXT && ECHO EEPROMService >>%COMPUTERNAME%.TXT

find /I "enterceptAgent" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "enterceptAgent"

IF ERRORLEVEL 1 REG IMPORT REG\enterceptAgent.REG >>%COMPUTERNAME%.TXT && ECHO enterceptAgent >>%COMPUTERNAME%.TXT 

find /I "Eventlog" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Eventlog"

IF ERRORLEVEL 1 REG IMPORT REG\Eventlog.REG >>%COMPUTERNAME%.TXT && ECHO Eventlog >>%COMPUTERNAME%.TXT

find /I "EventSystem" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "EventSystem"

IF ERRORLEVEL 1 REG IMPORT REG\EventSystem.REG >>%COMPUTERNAME%.TXT && ECHO EventSystem >>%COMPUTERNAME%.TXT

find /I "EvtEng" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "EvtEng"

IF ERRORLEVEL 1 REG IMPORT REG\EvtEng.REG >>%COMPUTERNAME%.TXT && ECHO EvtEng >>%COMPUTERNAME%.TXT

find /I "FontCache3.0.0.0" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "FontCache3.0.0.0"

IF ERRORLEVEL 1 REG IMPORT REG\FontCache3.REG >>%COMPUTERNAME%.TXT && ECHO FontCache3.0.0.0 >>%COMPUTERNAME%.TXT

find /I "helpsvc" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "helpsvc"

IF ERRORLEVEL 1 REG IMPORT REG\helpsvc.REG >>%COMPUTERNAME%.TXT && ECHO helpsvc >>%COMPUTERNAME%.TXT

find /I "hips" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "hips"

IF ERRORLEVEL 1 REG IMPORT REG\hips.REG >>%COMPUTERNAME%.TXT && ECHO hips >>%COMPUTERNAME%.TXT

find /I "IBMPMSVC" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "IBMPMSVC"

IF ERRORLEVEL 1 REG IMPORT REG\IBMPMSVC.REG >>%COMPUTERNAME%.TXT && ECHO IBMPMSVC >>%COMPUTERNAME%.TXT

find /I "IviRegMgr" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "IviRegMgr"

IF ERRORLEVEL 1 REG IMPORT REG\IviRegMgr.REG >>%COMPUTERNAME%.TXT && ECHO IviRegMgr >>%COMPUTERNAME%.TXT

find /I "JavaQuickStarterService" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "JavaQuickStarterService"

IF ERRORLEVEL 1 REG IMPORT REG\JavaQuickStarterService.REG >>%COMPUTERNAME%.TXT && ECHO JavaQuickStarterService >>%COMPUTERNAME%.TXT

find /I "LanmanServer" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "LanmanServer"

IF ERRORLEVEL 1 REG IMPORT REG\LanmanServer.REG >>%COMPUTERNAME%.TXT && ECHO LanmanServer >>%COMPUTERNAME%.TXT

find /I "lanmanworkstation" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "lanmanworkstation"

IF ERRORLEVEL 1 REG IMPORT REG\lanmanworkstation.REG >>%COMPUTERNAME%.TXT && ECHO lanmanworkstation >>%COMPUTERNAME%.TXT

find /I "LmHosts" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "LmHosts"

IF ERRORLEVEL 1 REG IMPORT REG\LmHosts.REG >>%COMPUTERNAME%.TXT && ECHO LmHosts >>%COMPUTERNAME%.TXT


find /I "LucentIKE" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "LucentIKE"

IF ERRORLEVEL 1 REG IMPORT REG\LucentIKE.REG >>%COMPUTERNAME%.TXT && ECHO LucentIKE >>%COMPUTERNAME%.TXT


find /I "McAfeeFramework" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "McAfeeFramework"

IF ERRORLEVEL 1 REG IMPORT REG\McAfeeFramework.REG >>%COMPUTERNAME%.TXT && ECHO McAfeeFramework >>%COMPUTERNAME%.TXT


find /I "McShield" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "McShield"

IF ERRORLEVEL 1 REG IMPORT REG\McShield.REG >>%COMPUTERNAME%.TXT && ECHO McShield >>%COMPUTERNAME%.TXT

find /I "McTaskManager" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "McTaskManager"

IF ERRORLEVEL 1 REG IMPORT REG\McTaskManager.REG >>%COMPUTERNAME%.TXT && ECHO McTaskManager >>%COMPUTERNAME%.TXT

find /I "MDM" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "MDM"

IF ERRORLEVEL 1 REG IMPORT REG\MDM.REG >>%COMPUTERNAME%.TXT && ECHO MDM >>%COMPUTERNAME%.TXT

find /I "Messenger" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Messenger"

IF ERRORLEVEL 1 REG IMPORT REG\Messenger.REG >>%COMPUTERNAME%.TXT && ECHO Messenger >>%COMPUTERNAME%.TXT

find /I "mfevtp" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "mfevtp"

IF ERRORLEVEL 1 REG IMPORT REG\mfevtp.REG >>%COMPUTERNAME%.TXT && ECHO mfevtp >>%COMPUTERNAME%.TXT

find /I "MSIServer" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "MSIServer"

IF ERRORLEVEL 1 REG IMPORT REG\MSIServer.REG >>%COMPUTERNAME%.TXT && ECHO MSIServer >>%COMPUTERNAME%.TXT

find /I "Net Driver HPZ12" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Net Driver HPZ12"

IF ERRORLEVEL 1 REG IMPORT REG\Net_Driver_HPZ12.REG >>%COMPUTERNAME%.TXT && ECHO Net Driver HPZ12 >>%COMPUTERNAME%.TXT

find /I "Netlogon" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Netlogon"

IF ERRORLEVEL 1 REG IMPORT REG\Netlogon.REG >>%COMPUTERNAME%.TXT && ECHO Netlogon >>%COMPUTERNAME%.TXT

find /I "Netman" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Netman"

IF ERRORLEVEL 1 REG IMPORT REG\Netman.REG >>%COMPUTERNAME%.TXT && ECHO Netman >>%COMPUTERNAME%.TXT

find /I "Nla" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Nla"

IF ERRORLEVEL 1 REG IMPORT REG\Nla.REG >>%COMPUTERNAME%.TXT && ECHO Nla >>%COMPUTERNAME%.TXT

find /I "NMSAccess" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "NMSAccess"

IF ERRORLEVEL 1 REG IMPORT REG\NMSAccess.REG >>%COMPUTERNAME%.TXT && ECHO NMSAccess >>%COMPUTERNAME%.TXT

find /I "PlugPlay" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "PlugPlay"

IF ERRORLEVEL 1 REG IMPORT REG\PlugPlay.REG >>%COMPUTERNAME%.TXT && ECHO PlugPlay >>%COMPUTERNAME%.TXT

find /I "Pml Driver HPZ12" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Pml Driver HPZ12"

IF ERRORLEVEL 1 REG IMPORT REG\Pml_Driver_HPZ12.REG >>%COMPUTERNAME%.TXT && ECHO Pml Driver HPZ12 >>%COMPUTERNAME%.TXT

find /I "PolicyAgent" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "PolicyAgent"

IF ERRORLEVEL 1 REG IMPORT REG\PolicyAgent.REG >>%COMPUTERNAME%.TXT && ECHO PolicyAgent >>%COMPUTERNAME%.TXT

find /I "Power Manager DBC Service" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Power Manager DBC Service"

IF ERRORLEVEL 1 REG IMPORT REG\Power.REG >>%COMPUTERNAME%.TXT && ECHO Power Manager DBC Service >>%COMPUTERNAME%.TXT

find /I "ProtectedStorage" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "ProtectedStorage"

IF ERRORLEVEL 1 REG IMPORT REG\ProtectedStorage.REG >>%COMPUTERNAME%.TXT && ECHO ProtectedStorage >>%COMPUTERNAME%.TXT

find /I "RasMan" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "RasMan"

IF ERRORLEVEL 1 REG IMPORT REG\RasMan.REG >>%COMPUTERNAME%.TXT && ECHO RasMan >>%COMPUTERNAME%.TXT

find /I "RegSrvc" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "RegSrvc"

IF ERRORLEVEL 1 REG IMPORT REG\RegSrvc.REG >>%COMPUTERNAME%.TXT && ECHO RegSrvc >>%COMPUTERNAME%.TXT

find /I "RemoteRegistry" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "RemoteRegistry"

IF ERRORLEVEL 1 REG IMPORT REG\RemoteRegistry.REG >>%COMPUTERNAME%.TXT && ECHO RemoteRegistry >>%COMPUTERNAME%.TXT

find /I "RpcSs" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "RpcSs"

IF ERRORLEVEL 1 REG IMPORT REG\RpcSs.REG >>%COMPUTERNAME%.TXT && ECHO RpcSs >>%COMPUTERNAME%.TXT

find /I "S24EventMonitor" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "S24EventMonitor"

IF ERRORLEVEL 1 REG IMPORT REG\S24EventMonitor.REG >>%COMPUTERNAME%.TXT && ECHO S24EventMonitor >>%COMPUTERNAME%.TXT

find /I "SamSs" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "SamSs"

IF ERRORLEVEL 1 REG IMPORT REG\SamSs.REG >>%COMPUTERNAME%.TXT && ECHO SamSs >>%COMPUTERNAME%.TXT

find /I "Schedule" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Schedule"

IF ERRORLEVEL 1 REG IMPORT REG\Schedule.REG >>%COMPUTERNAME%.TXT && ECHO Schedule >>%COMPUTERNAME%.TXT

find /I "SENS" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "SENS"

IF ERRORLEVEL 1 REG IMPORT REG\SENS.REG >>%COMPUTERNAME%.TXT && ECHO SENS >>%COMPUTERNAME%.TXT

find /I "ShellHWDetection" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "ShellHWDetection"

IF ERRORLEVEL 1 REG IMPORT REG\ShellHWDetection.REG >>%COMPUTERNAME%.TXT && ECHO ShellHWDetection >>%COMPUTERNAME%.TXT

find /I "Spooler" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Spooler"

IF ERRORLEVEL 1 REG IMPORT REG\Spooler.REG >>%COMPUTERNAME%.TXT && ECHO Spooler >>%COMPUTERNAME%.TXT

find /I "srservice" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "srservice"

IF ERRORLEVEL 1 REG IMPORT REG\srservice.REG >>%COMPUTERNAME%.TXT && ECHO srservice >>%COMPUTERNAME%.TXT

find /I "TapiSrv" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "TapiSrv"

IF ERRORLEVEL 1 REG IMPORT REG\TapiSrv.REG >>%COMPUTERNAME%.TXT && ECHO TapiSrv >>%COMPUTERNAME%.TXT

find /I "TermService" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "TermService"

IF ERRORLEVEL 1 REG IMPORT REG\TermService.REG >>%COMPUTERNAME%.TXT && ECHO TermService >>%COMPUTERNAME%.TXT

find /I "Themes" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "Themes"

IF ERRORLEVEL 1 REG IMPORT REG\Themes.REG >>%COMPUTERNAME%.TXT && ECHO Themes >>%COMPUTERNAME%.TXT

find /I "TPHKLOAD" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "TPHKLOAD"

IF ERRORLEVEL 1 REG IMPORT REG\TPHKLOAD.REG >>%COMPUTERNAME%.TXT && ECHO TPHKLOAD >>%COMPUTERNAME%.TXT

find /I "TPHKSVC" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "TPHKSVC"

IF ERRORLEVEL 1 REG IMPORT REG\TPHKSVC.REG >>%COMPUTERNAME%.TXT && ECHO TPHKSVC >>%COMPUTERNAME%.TXT

find /I "TpKmpSVC" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "TpKmpSVC"

IF ERRORLEVEL 1 REG IMPORT REG\TpKmpSVC.REG >>%COMPUTERNAME%.TXT && ECHO TpKmpSVC >>%COMPUTERNAME%.TXT

find /I "UPHClean" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "UPHClean"

IF ERRORLEVEL 1 REG IMPORT REG\UPHClean.REG >>%COMPUTERNAME%.TXT && ECHO UPHClean >>%COMPUTERNAME%.TXT

find /I "W32Time" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "W32Time"

IF ERRORLEVEL 1 REG IMPORT REG\W32Time.REG >>%COMPUTERNAME%.TXT && ECHO W32Time >>%COMPUTERNAME%.TXT

find /I "winmgmt" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "winmgmt"

IF ERRORLEVEL 1 REG IMPORT REG\winmgmt.REG >>%COMPUTERNAME%.TXT && ECHO winmgmt >>%COMPUTERNAME%.TXT

find /I "WSearch" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "WSearch"

IF ERRORLEVEL 1 REG IMPORT REG\WSearch.REG >>%COMPUTERNAME%.TXT && ECHO WSearch >>%COMPUTERNAME%.TXT

find /I "wuauserv" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "wuauserv"

IF ERRORLEVEL 1 REG IMPORT REG\wuauserv.REG >>%COMPUTERNAME%.TXT && ECHO wuauserv >>%COMPUTERNAME%.TXT

find /I "WZCSVC" %COMPUTERNAME%_START.TXT >nul

IF ERRORLEVEL 1 NET START "WZCSVC"

IF ERRORLEVEL 1 REG IMPORT REG\WZCSVC.REG >>%COMPUTERNAME%.TXT && ECHO WZCSVC >>%COMPUTERNAME%.TXT



START /max NOTEPAD.EXE %COMPUTERNAME%.TXT


START cmd /c shutdown -R -t 60 -c "REDEMARRAGE DU PC DANS 1 MN" /f