防止或关闭Taskkill后弹出Windows

时间:2019-01-29 09:11:34

标签: windows google-chrome cmd

我在Windows 10中使用script.bat来杀死Chrome,然后重启它。有时,我会弹出一个错误消息,其中包含以下消息:“ Application.exe”未能完成。操作7错误:无法终止进程“ chrome”。 Application.exe以管理员身份运行。如果我按OK,一切都会顺利进行,但是如果我不重新启动,Chrome将无法继续。有没有办法使用脚本关闭或防止错误弹出?

我尝试了以下代码片段:

第一个不会阻止错误弹出。

taskkill /IM application.exe /T /F 2> nul
taskkill /IM chrome.exe /T /F 2> nul
start "" /wait C:\a\b\c\application.exe
start chrome --incognito --kiosk http://localhost:8000

第二个不能启动Chrome。

taskkill /IM chrome.exe /T /F 2> nul
tasklist /FI "IMAGENAME eq application.exe" | findstr /I /C:"application.exe"
IF ERRORLEVEL 1
start chrome --incognito --kiosk http://localhost:8000

1 个答案:

答案 0 :(得分:0)

以下.bat脚本应完成该任务。

@echo OFF
set "_app=application.exe"
:CheckApp
tasklist /FI "IMAGENAME eq %_app%" | findstr /I /C:"%_app%" >NUL
IF ERRORLEVEL 1 (
    REM Application.exe has finished and you can kill chrome.exe safely
    REM without `/F` switch, `taskkill` merely sends termination signal;
    REM `/F` specifies to forcefully terminate specified process(es).
    taskkill /IM chrome.exe /T /F 2> nul
    REM labels are not allowed inside a parenthesized code block;
    REM                                  hence, call a procedure 
    CALL :RestartChrome
) else (
    REM `Application` is running; go back to `:CheckApp` test after some delay
    timeout /T 1 /NOBREAK >NUL 2>&1
    GOTO :CheckApp
)
REM script continues here after `chrome` restart
GOTO :eof

:RestartChrome
REM check if `chrome.exe` is still running
tasklist /FI "IMAGENAME eq chrome.exe" | findstr /I /C:"chrome.exe" >NUL
IF ERRORLEVEL 1 (
    REM `chrome.exe` has finished and you can restart it safely
    start "" chrome --incognito --kiosk http://localhost:8000
) else (
    REM `chrome.exe` is running
    REM wait here for some time period to finish `chrome` completely
    timeout /T 1 /NOBREAK >NUL 2>&1
    REM go back to `:RestartChrome` test after (above specified) delay
    GOTO :RestartChrome
)
GOTO :eof

以上代码中的REM注释应充分说明自身(考虑先前的讨论)。有关更多信息,请参见必读https://docs.gitlab.com/ee/user/project/pages/introduction.html#can-i-use-gitlab-pages-if-my-project-is-private