[英]How to assign process name to batch file when running from taskscheduler
I am running a webapplication on Windows Server 2012 and have to run an endless loop in a PHP file. 我在Windows Server 2012上运行Web应用程序,并且必须在PHP文件中运行无限循环。 In order to control this, I created two batch files. 为了控制这一点,我创建了两个批处理文件。
The PHP file: PHP文件:
<?php
while(true) {
sleep(10);
}
?>
BatchFile that calls the PHP file: 调用PHP文件的BatchFile:
TITLE WatchdogStarterBATCH
php "%~dp0watchdog.php"
timeout 5
Batchfile 2 批处理文件2
@ECHO OFF
:: Detect whether program is running
for /f "tokens=2 delims=," %%P in ('tasklist /v /fo csv ^| findstr /i "WatchdogStarterBATCH"') do set pid=%%~P
IF [%pid%] == [] (
:: Program not running, trying to restart
start "WatchdogStarterBATCH" "%~dp0WatchdogStarter.bat"
timeout 5
GOTO rerun
) ELSE (
:: Program is running
GOTO end
)
:rerun
:: Check whether program is running now
for /f "tokens=2 delims=," %%P in ('tasklist /v /fo csv ^| findstr /i "WatchdogStarterBATCH"') do set pid=%%~P
IF [%pid%] == [] (
:: Restart failed, log to database
php "%~dp0WatchdogErrorLogger.php"
) ELSE (
:: Restart successful, log to database
php "%~dp0WatchdogWarningLogger.php"
GOTO end
)
:end
echo Done.
timeout 5
Batchfile 2 is called by the Task Scheduler and then calles WatchdogStarter.bat as you can see. 如您所见,任务计划程序将调用Batchfile 2,然后调用WatchdogStarter.bat。 It tries to give this service a name, so that it can find it later in the tasklist. 它尝试为该服务命名,以便稍后可以在任务列表中找到它。
Things I tried: 我尝试过的事情:
This is vbs. 这是vbs。
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set objEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM Win32_ProcessStopTrace")
Do
Set objReceivedEvent = objEvents.NextEvent
msgbox objReceivedEvent.ProcessName
If lcase(objReceivedEvent.ProcessName) = lcase("Notepad.exe") then
Msgbox "Process exited with exit code " & objReceivedEvent.ExitStatus
WshShell.Run "c:\Windows\notepad.exe", 1, false
End If
Loop
What it does it waits for a program to exit (all programs) and if notepad (in the example) restarts it. 它的作用是等待程序退出(所有程序),然后记事本(在此示例中)重新启动。 You'd check for php.exe or whatever it's called. 您将检查php.exe或其名称。
Or use the following to get the command line and check by PID. 或使用以下命令获取命令行并通过PID进行检查。
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
For Each objItem in colItems
msgbox objItem.ProcessID & " " & objItem.CommandLine
Next
Use Instr to check for your PHP filename in command line and store the PID. 使用Instr在命令行中检查您的PHP文件名并存储PID。
This would also work 这也可以
Set WshShell = WScript.CreateObject("WScript.Shell")
Do
Return = WshShell.Run("notepad " & WScript.ScriptFullName, 0, true)
Loop
It starts notepad and waits for it to exit, then starts it again forever. 它启动记事本,等待它退出,然后永远重新启动。
The same technique in batch 批量相同的技术
:ALabel
start "" /w notepad.exe
Goto ALabel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.