dongqidi2799 2016-01-11 11: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:

  1. <?php
  2. while(true) {
  3. sleep(10);
  4. }
  5. ?>

BatchFile that calls the PHP file:

  1. TITLE WatchdogStarterBATCH
  2. php "%~dp0watchdog.php"
  3. timeout 5

Batchfile 2

  1. @ECHO OFF
  2. :: Detect whether program is running
  3. for /f "tokens=2 delims=," %%P in ('tasklist /v /fo csv ^| findstr /i "WatchdogStarterBATCH"') do set pid=%%~P
  4. IF [%pid%] == [] (
  5. :: Program not running, trying to restart
  6. start "WatchdogStarterBATCH" "%~dp0WatchdogStarter.bat"
  7. timeout 5
  8. GOTO rerun
  9. ) ELSE (
  10. :: Program is running
  11. GOTO end
  12. )
  13. :rerun
  14. :: Check whether program is running now
  15. for /f "tokens=2 delims=," %%P in ('tasklist /v /fo csv ^| findstr /i "WatchdogStarterBATCH"') do set pid=%%~P
  16. IF [%pid%] == [] (
  17. :: Restart failed, log to database
  18. php "%~dp0WatchdogErrorLogger.php"
  19. ) ELSE (
  20. :: Restart successful, log to database
  21. php "%~dp0WatchdogWarningLogger.php"
  22. GOTO end
  23. )
  24. :end
  25. echo Done.
  26. 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 12: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 KeiI中头文件找不到怎么解决
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 本地安装org.Hs.eg.dby一直这样的图片报错如何解决?
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)
  • ¥20 为什么在复位后出现错误帧
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部