确定一个批处理文件运行实例 [英] Identify running instances of a batch file

查看:109
本文介绍了确定一个批处理文件运行实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些都不是为我工作。

These are not working for me.

任何有助于definitelly corret下面的四个例子?

Any help to definitelly corret the four examples below ?

该EXAMPLE01只是呼应继续,即使我有三个的CMD.exe打开。

The EXAMPLE01 just echoes "continue", even if I have three CMD.exe opened.

----------例01 ------------

---------- EXAMPLE 01 ------------

@echo off 
wmic process where name="cmd.exe" | find "cmd.exe" /c
SET ERRORLEVEL=value if "%value%" GTR 1 ( 
    ECHO This batch is not first  
    ECHO quitting ...
    )
if "%value%" LSS 2 ECHO continue

我在例02得到意外我的错误的信息!

I am getting the "unexpected i error" message in the EXAMPLE 02!

-----------例02 -------

----------- EXAMPLE 02 -------

@echo off
FOR /F "usebackq tokens=2" %i IN (`tasklist ^| findstr /r /b "cmd.exe"`)
   DO taskkill /pid %%i

我在例03获得第一的消息,甚至有三个的CMD.exe开了!

I am getting the "is first" message in the EXAMPLE 03, even with three CMD.exe opened!

-----------例03 -------

----------- EXAMPLE 03 -------

 @echo off
    wmic process where name="cmd.exe" | find "cmd.exe" /c
    if "%errorlevel%" LEQ 1 echo CMD is first
    if "%errorlevel%" GTR 1 echo CMD is already running

这也可能是我不会有机会获得在工作中WMIC命令,于是,另一种可能性是在例04中...但无济于事。

It is also possible that I will not have access to the Wmic command at work, so, another possibility is found in the EXAMPLE 04 ... but to no avail.

-----------例04 -------

----------- EXAMPLE 04 -------

@echo off
Tasklist /FI "IMAGENAME eq cmd.exe" 2>NUL | find /I /N "cmd.exe">NUL
if "%ERRORLEVEL%"==0 do (goto Use) else (goto Cont)
:Cont
ECHO Only one instance running
pause

:Use
echo Application running already. Close this window


亲切的问候,
Maleck


Kind regards, Maleck

推荐答案

WMZ确定了一些错误,在OP的code,同时还拥有出色的建议,使用锁文件进行并发控制。

wmz identified a number of errors in the OP's code, and also has an excellent suggestion to use a lock file for concurrency control.

下面是一个使用锁文件的批处理文件prevent多个实例同时运行一个强大的批处理的解决方案。它采用了并发控制的临时锁定文件。锁定文件的仅仅是presence不停止运行脚本。如果另一个进程对锁定文件的独占锁定的脚本只能失败。这是很重要的情况下,该脚本会崩溃或不删除锁文件被杀害。接下来的过程中运行该脚本仍然会成功,因为文件不再被锁定。

Below is a robust batch solution that uses a lock file to prevent multiple instances of the batch file from running at the same time. It uses a temporary lock file for the concurrency control. The mere presence of the lock file does NOT stop the script from running. The script will only fail if another process has an exclusive lock on the lock file. This is important in case the script should crash or be killed without deleting the lock file. The next process to run the script will still succeed because the file is no longer locked.

该脚本假设脚本安装在本地驱动器上。它允许只有一个为整个机实例。有很多的变化,以控制并发的允许量。例如,掺入%USERNAME%到锁定的文件名将允许在网络环境中每个用户一个实例。在名称掺入%COMPUTERNAME%将允许在网络环境中每个机器一个实例

This script assumes the script is installed on a local drive. It allows only one instance for the entire machine. There are lots of variations to control the amount of concurrency allowed. For example, incorporating the %USERNAME% into the lock file name would allow one instance per user in a network environment. Incorporating %COMPUTERNAME% in the name would allow one instance per machine in a network environment.

@echo off
setlocal

:: save parameters to variables here as needed
set "param1=%~1"
:: etc.

:: Redirect an unused file handle for an entire code block to a lock file.
:: The batch file will maintain a lock on the file until the code block
:: ends or until the process is killed. The main code is called from
:: within the block. The first process to run this script will succeed.
:: Subsequent attempts will fail as long as the original is still running.
set "started="
9>"%~f0.lock" (
  set "started=1"
  call :start
)
if defined started (
    del "%~f0.lock" >nul 2>nul
) else (
    echo Process aborted: "%~f0" is already running
)
exit /b

:start
:: The main program appears below
:: All this script does is PAUSE so we can see what happens if
:: the script is run multiple times simultaneously.
pause
exit /b


修改

该错误消息的的进程不能因为它正被另一个进程访问文件。的可以通过重定向标准错误输出外块NUL pssed燮$ P $。

The error message "The process cannot access the file because it is being used by another process." can be suppressed by redirecting stderr output to nul in an outer block.

2>nul (
  9>"%~f0.lock" (
    set "started=1"
    call :start
  )
)

与上述的问题是,对主节目的所有错误消息也将SUP pressed。这可以通过节省1标准错误当前定义的未使用的文件句柄,然后又添加重定向​​标准错误回所保存的文件句柄另一个内部块是固定的。

The problem with the above is that all error messages for the main program will also be suppressed. That can be fixed by 1st saving the current definition of stderr to another unused file handle, and then adding yet another inner block that redirects stderr back to the saved file handle.

8>&2 2>nul (
  9>"%~f0.lock" (
    2>&8 (
      set "started=1"
      call :start
    )
  )
)

这篇关于确定一个批处理文件运行实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆