在 PowerShell 中启动分离的后台进程 [英] Start a detached background process in PowerShell

查看:141
本文介绍了在 PowerShell 中启动分离的后台进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java 程序,我想将其作为后台进程从 PowerShell 脚本启动,类似于守护进程在 Linux 上的运行方式.PowerShell 脚本需要做几件事:

I have a Java program which I would like to launch as a background process from a PowerShell script, similar to the way a daemon runs on Linux. The PowerShell script needs to do a couple of things:

  1. 在后台将程序作为单独的分离进程运行,这意味着可以关闭父窗口而进程继续运行.
  2. 将程序的标准输出和标准错误重定向到文件.
  3. 将后台进程的 PID 保存到一个文件中,以便稍后可以被另一个脚本终止.

我在 Linux 上有一个 shell 脚本,它可以像这样启动程序:

I have a shell script on Linux which starts the program like so:

$ java -jar MyProgram.jar >console.out 2>console.err &

我希望使用 PowerShell 脚本在 Windows 上复制相同的行为.我尝试使用 Start-Process 和各种选项组合,以及创建 System.Diagnostics.ProcessStartInfoSystem.Diagnostics.Process 对象,但到目前为止我没有任何运气.PowerShell 将程序作为后台进程启动,但是当启动 PowerShell 会话的 DOS 窗口关闭时,程序会突然终止.我希望它在后台启动并且独立于启动它的命令窗口.

I'm hoping to replicate the same behavior on Windows using a PowerShell script. I have tried using Start-Process with various combinations of options, as well as creating System.Diagnostics.ProcessStartInfo and System.Diagnostics.Process objects, but so far I am not having any luck. PowerShell starts the program as a background process, but the program abruptly terminates when the DOS window which started the PowerShell session is closed. I would like it to start in the background and be independent of the command window which started it.

输出重定向也很麻烦,因为似乎输出和错误流只能在同一窗口中运行的进程中重定向(例如,使用-NoNewWindow).

The output redirection has also been troublesome, as it seems that the output and error streams can only be redirected in the process is being run in the same window (e.g., using -NoNewWindow).

这种事情在 PowerShell 中可行吗?

Is this sort of thing possible in PowerShell?

推荐答案

这是一篇旧帖子,但由于我的帖子运行良好,因此认为它可能有助于分享.对java"而不是javaw"的调用可能是您的问题.我自己用我的 JEdit java 程序通过 powershell 运行它来启动它.

This is an old post but since I have it working fine thought it might help to share. Its the call to 'java' instead of 'javaw' that is likely your issue. Ran it out myself using my JEdit java program through powershell to launch it.

#Requires -Version 3.0
$MyDriveRoot = (Get-Location).Drive.Root
$JEditDir = $($mydriveroot + "jEdit") ;# Should be C:\jEdit or wherever you want. JEdit is a sub-directory.
$jEdit = $($JEditDir + "\jedit.jar" )
$jEditSettings = $($JEditDir + "\settings")
$JEditLogs = $($JEditDir + "\logs")

Start-Process -FilePath javaw -ArgumentList ( '-jar',"$jEdit", '-settings="$JEditSettings"' ) -RedirectStandardOutput "$JEditLogs\console.out" -RedirectStandardError "$JEditLogs\console.err"

你可以把它变成一个小函数,然后变成一个别名,以便在 Powershell 中轻松启动.

Which you can turn into a little function and then an alias to make it easy to launch in Powershell.

If ( ( Test-Path $jedit) ) {
    Function Start-JEdit() {
        Start-Process -FilePath javaw -ArgumentList ( '-jar',"$jEdit", '-settings="$($mydriveroot + "jEdit\settings")"' ) -RedirectStandardOutput "$JEditLogs\console.out" -RedirectStandardError "$JEditLogs\console.err"
    }
New-Alias -Name jedit  -Force Start-JEdit  -Description "Start JEdit programmers text editor" 
}

这篇关于在 PowerShell 中启动分离的后台进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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