dongqidi2799 2016-01-11 19:27
浏览 44
已采纳

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

I am running a webapplication on Windows Server 2012 and have to run an endless loop in a PHP file. In order to control this, I created two batch files.

The PHP file:

<?php 
    while(true) { 
        sleep(10); 
    } 
?>

BatchFile that calls the PHP file:

TITLE WatchdogStarterBATCH

php "%~dp0watchdog.php"
timeout 5

Batchfile 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. 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. 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.
  • 写回答

1条回答 默认 最新

  • doumu6997 2016-01-11 20:49
    关注

    This is vbs.

    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set objWMIService = GetObject("winmgmts:\\.oot\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
    otepad.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.

    Or use the following to get the command line and check by PID.

    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.oot\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.

    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 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)