exec() with timeout

后端 未结 7 1080
北荒
北荒 2020-12-03 04:27

I\'m looking for a way to run a PHP process with a timeout. Currently I\'m simply using exec(), but it does not provide a timeout option.

What I also tried is openin

7条回答
  •  青春惊慌失措
    2020-12-03 04:38

    I am facing the same problem that I have tried all the answers above, but windows server cant work with any of these, maybe it is my stupidity.

    My final working solution for windows is executing a batch file,

    timeout.bat

    ::param 1 is timeout seconds, param 2 is executable
    echo "running %2 with timeout %1"
    start %2
    set time=0
    
    :check
    tasklist /FI "IMAGENAME eq %2" 2>NUL | find /I /N "%2">NUL
    ::time limit exceed
    if "%time%"=="%1" goto kill
    ::program is running
    if "%ERRORLEVEL%"=="0" ( ping 127.0.0.1 -n 2 >nul & set /a time=%time%+1 & goto check) else ( goto end)
    
    :kill
    echo "terminate"
    taskkill /im %2 /f
    
    :end
    echo "end"
    

    the php command

    exec("timeout.bat {$time} your_program.exe");
    

提交回复
热议问题