windows 在 PowerShell 中启动分离的后台进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25023458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Start a detached background process in PowerShell
提问by Dave
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:
我有一个 Java 程序,我想将其作为后台进程从 PowerShell 脚本启动,类似于守护进程在 Linux 上运行的方式。PowerShell 脚本需要做几件事:
- Run the program as a separate and detached process in the background, meaning the parent window can be closed and the process keeps running.
- Redirect the program's standard output and standard error to files.
- Save the PID of the background process to a file so it can be terminated later by another script.
- 在后台将程序作为单独的分离进程运行,这意味着可以关闭父窗口并且进程继续运行。
- 将程序的标准输出和标准错误重定向到文件。
- 将后台进程的 PID 保存到一个文件中,以便稍后可以被另一个脚本终止。
I have a shell script on Linux which starts the program like so:
我在 Linux 上有一个 shell 脚本,它像这样启动程序:
$ java -jar MyProgram.jar >console.out 2>console.err &
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.
我希望使用 PowerShell 脚本在 Windows 上复制相同的行为。我尝试使用Start-Process
各种选项组合,以及创建System.Diagnostics.ProcessStartInfo
和System.Diagnostics.Process
对象,但到目前为止我没有任何运气。PowerShell 将程序作为后台进程启动,但是当启动 PowerShell 会话的 DOS 窗口关闭时,程序会突然终止。我希望它在后台启动并且独立于启动它的命令窗口。
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
).
输出重定向也很麻烦,因为似乎输出和错误流只能在同一窗口中运行的进程中重定向(例如,使用-NoNewWindow
)。
Is this sort of thing possible in PowerShell?
这种事情在 PowerShell 中是可能的吗?
回答by Ansgar Wiechers
Use jobsfor this:
为此使用作业:
Start-Job -ScriptBlock {
& java -jar MyProgram.jar >console.out 2>console.err
}
Another option would be Start-Process
:
另一种选择是Start-Process
:
Start-Process java -ArgumentList '-jar', 'MyProgram.jar' `
-RedirectStandardOutput '.\console.out' -RedirectStandardError '.\console.err'
回答by use
Consider using the task scheduler for this. Define a task and set it without any triggers. That will allow you to simply "Run" (manually trigger) the task.
考虑为此使用任务调度程序。定义一个任务并在没有任何触发器的情况下设置它。这将允许您简单地“运行”(手动触发)任务。
You can set up and/or trigger scheduled tasks using the ScheduledTasks powershell module, or you can use the GUI.
您可以使用 ScheduledTasks powershell 模块设置和/或触发计划任务,也可以使用 GUI。
回答by Ernie M.
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.
这是一篇旧帖子,但由于我的工作正常,因此我认为它可能有助于分享。对“java”而不是“javaw”的调用可能是您的问题。通过powershell使用我的JEdit java程序自己运行它来启动它。
#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"
Which you can turn into a little function and then an alias to make it easy to launch in Powershell.
你可以把它变成一个小函数,然后变成一个别名,以便在 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"
}