简体   繁体   English

从taskscheduler运行时如何为批处理文件分配进程名称

[英]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: 我尝试过的事情:

  1. Run batfile2 from command line and check if i can find it the string "WatchdogStarterBATCH" in the tasklist. 从命令行运行batfile2,并检查是否可以在任务列表中找到字符串“ WatchdogStarterBATCH”。 I was able to find it, so that works. 我能够找到它,所以可行。
  2. Try assigning a name in the 'start' command I used, but that did not help. 尝试在我使用的“开始”命令中分配一个名称,但这没有帮助。
  3. Try assigning a name in the first batch file with 'TITLE', but that did not help. 尝试在第一个批处理文件中使用“ TITLE”分配名称,但这无济于事。

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.

相关问题 当我从.php执行批处理文件时,实际上如何在前台打开Windows命令行并查看整个过程 - When i execute batch file from .php how can i actually open Windows Command Line in the foreground and see the entire process 如何将文件名分配给 php 中的 class - How to assign the name of a file to a class in php 在任务计划程序中运行批处理文件时无法创建日志文件 - cannot create log file when running batch file in task scheduler 如何运行批处理? - How to run batch process? 当您必须从字符串和变量构建要分配的数组名称时,如何在php中将一个数组分配给另一个数组? - How to assign one array to another in php, when you have to build the name of the array to be assigned from a string and a variable? 在Windows中运行批处理文件时的PHP日期和路径 - PHP date and paths when running a batch file in windows 在批处理文件中运行php文件 - Running a php file in a batch file 从Windows Task Scheduler运行批处理文件,但隐藏或最小化 - Running a batch file from windows task scheduler, but hidden or minimized 从运行php的IIS服务器执行vbscript或批处理文件 - Execute vbscript or batch file from IIS server running php 在php中获取正在运行的进程的名称 - Get name of running process in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM