简体   繁体   English

检查相同的 powershell 脚本是否已在运行,如果是,请稍后运行

[英]Check if the same powershell script is already running and run it later if yes

I am calling from my Java EE application through ssh (apache) Powershell script on remote server, this script cannot be run in parallel so I need to check if the script is already running and wait until there is no process and run that waiting script.我从我的 Java EE 应用程序通过 ssh (apache) Powershell 脚本在远程服务器上调用,这个脚本不能在远程服务器上并行运行,所以我需要检查是否没有进程并等待脚本已经运行。 It might happen that the script will be called several times, eg.脚本可能会被多次调用,例如。 20 times in few seconds and all of these needs to be run one by one.几秒钟内 20 次,所有这些都需要一个一个地运行。

Any idea how this can be achieved or if this is even possible?知道如何实现这一点,或者这是否可能? Thank you!谢谢!

The way that I've accomplished this in some of my scripts is to use a lock file我在某些脚本中完成此操作的方法是使用锁定文件

  $lockFile = "$PSScriptRoot\LOCK"
    if (-not (Test-Path $lockFile)) {
        New-Item -ItemType File -Path $lockFile | Out-Null
       
        #  Do other stuff...
    
        Remove-Item -Path  $lockFile -Force
    }

You could maybe modify to something like this?你也许可以修改成这样的东西?

$lockFile = "$PSScriptRoot\LOCK"
while (Test-Path $lockFile)
{
    Start-Sleep -Seconds 2
}

New-Item -ItemType File -Path $lockFile | Out-Null

#  Do other stuff...

Remove-Item -Path  $lockFile -Force

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM