如何避免脚本的多个实例,而又无需更改标题即可再次启动它

时间:2018-10-23 07:13:48

标签: batch-file

我有只应在单个实例中运行的脚本。我确保这种情况不会发生的方法是

tasklist /fi "imagename eq cmd.exe" /v | find "lootbot" && echo already running && pause && exit /b
title lootbot

稍后再正常退出,以便可以重新启动脚本...

:action8
cd /d "%~dp0"
title command prompt
echo type launch to return to lootbot
exit /b

问题在于,每当脚本由于错误而崩溃时,如果不先手动将标题重新设置为其他名称,就不允许我对其进行备份。

当然,这不是一个大问题,但是我正在构建供其他人使用的问题,因此我在这里寻找易于使用的解决方案。我在考虑是否有一种方法可以确定 current 命令提示符是否标题为lootbot,那么可以自动避免此问题。这是整个脚本,以防万一。

@echo off
tasklist /fi "imagename eq cmd.exe" /v | find "lootbot" && echo lootbot already running, press a key to exit && pause && exit /b
title lootbot
:: enable save files location (persistant memory)
set "saves=%appdata%\lootbot"
if not exist "%saves%" mkdir "%saves%"
:: create/load saves, echo first
if exist "%saves%\echoes\launch" echo on
if not exist "%saves%\loopdelay" echo 300>"%saves%\loopdelay"
if not exist "%saves%\promptdelay" echo 60>"%saves%\promptdelay"
set /p loopdelay=<"%saves%\loopdelay" & set /p promptdelay=<"%saves%\promptdelay"
:: advanced prep
chcp 65001 >nul
set "path=%path%;%~dp0;c:\program files\filebot"
wmic process where name="cmd.exe" call setpriority "idle" >nul
:: additional scripts (all-in-one launcher)
start vpn.cmd

:: add "going legit" to vpn or somewhere (meaning close torrents and vpn, reactivate vpn if torrents appear, or close torrents)
:: add skippable delay here to auto-avoid possible mishandling ones being checked in qbittorrent
cls
:menu
@colorx -c 08
@if exist "%saves%\echoes\launch" echo on
@cd /d "%~dp0"
@set "msg=lootbot ready (%time:~0,2%:%time:~3,2%) wait %loopdelay%s or enter command"
@set "msg=%msg:( =(%" & rem this removes the blank space from early hours
@choice /t %loopdelay% /c rcplsjdq /n /d r /m "%msg% [R]un [C]lear [P]ause [L]ist [S]etup [J]obs [D]onate [Q]uit: "
:: add selective choices here, based on what scripts are available. might need those scripts to be names or placed different to autodetect
@goto action%errorlevel%
@goto menu

:action1
:routine
:: run now/default infinite loop
call report %~n0 import
call report %~n0 direct
:: idea: add small amount of random files to scan?
goto menu

:action2
:clear
cls
goto menu

:action3
pause
goto menu

:action4
:playlist
call report %~n0 player
goto menu

:action5
:settings
call report %~n0 option
goto menu

:action6
:caring
call report %~n0 caring
goto menu

:action7
:donate
call report %~n0 donate
goto menu

:action8
:exit
:: quit back to command prompt
cd /d "%~dp0"
title command prompt
echo use command launch to return to lootbot, bye!
exit /b

1 个答案:

答案 0 :(得分:4)

通过在策略上使用CALL重定向来建立锁定文件,可以轻松完成此操作。只有一个实例可以对锁定文件进行写访问。再次尝试将失败。第一个实例将在退出后立即释放该锁定,无论它如何终止。

我使用重定向的多个阶段来隐藏不需要的错误消息,但是主例程具有正常的stdout和stderr。我添加%*以将原始参数传递给main。 main中唯一的“奇数”情况是%0:main而不是执行脚本。但是%~f0可用于获取执行脚本的完整路径。

test.bat

@echo off
9>&2 2>nul (call :lockAndRestoreStdErr %* 8>"%~f0.lock") && (
  del "%~f0.lock"
) || (
  echo Only one instance allowed - "%~f0" is already running >&2
)
exit /b

:lockAndRestoreStdErr
call :main %* 2>&9
exit /b 0

:main
echo        %%0 = %0
echo    "%%~f0" = "%~f0"
echo Arguments = %*
pause
exit /b

实例1输出示例

C:\test>test arg1 arg2
       %0 = :main
   "%~f0" = "C:\test\test.bat"
Arguments = arg1 arg2
Press any key to continue . . .

实例2的示例输出,而实例1仍在运行

C:\test>test arg1 arg2
Only one instance allowed - "C:\test\test.bat" is already running

C:\test>

一旦按下一个键,实例1将终止,然后实例2可以运行脚本。

编辑

实际上,您不需要单独的锁定文件。您可以将批处理脚本本身用作锁定文件。只需记住使用>>而不是>,否则您将清除脚本!

@echo off
9>&2 2>nul (call :lockAndRestoreStdErr %* 8>>"%~f0") || (
  echo Only one instance allowed - "%~f0" is already running >&2
)
exit /b

:lockAndRestoreStdErr
call :main %* 2>&9
exit /b 0

:main
echo        %%0 = %0
echo    "%%~f0" = "%~f0"
echo Arguments = %*
pause
exit /b

如果用户没有对该脚本的写权限,则以上两种方法均无效。在这种情况下,您应该将锁定文件放在%temp%文件夹中。

@echo off
9>&2 2>nul (call :lockAndRestoreStdErr %* 8>"%temp%\%~nx0.lock") && (
  del "%temp%\%~nx0.lock"
) || (
  echo Only one instance allowed - "%~f0" is already running >&2
)
exit /b

:lockAndRestoreStdErr
call :main %* 2>&9
exit /b 0

:main
echo        %%0 = %0
echo    "%%~f0" = "%~f0"
echo Arguments = %*
pause
exit /b

编辑-评论中的问题答案

要将上述技术应用到脚本中,只需删除任务列表行,然后用我的技术替换它,以使脚本顶部看起来像:

@echo off
9>&2 2>nul (call :lockAndRestoreStdErr %* 8>>"%~f0") || (
  echo Only one instance allowed - "%~f0" is already running >&2
  pause
)
exit /b

:lockAndRestoreStdErr
call :main %* 2>&9
exit /b 0

:main
title lootbot
:: enable save files location (persistant memory)
etc...