在bash中,&号(&)可用于在后台运行命令,并在命令运行完成之前将交互控制返回给用户。在Powershell中是否有等效的方法来做到这一点?
在bash中的用法示例:
sleep 30 &
在bash中,&号(&)可用于在后台运行命令,并在命令运行完成之前将交互控制返回给用户。在Powershell中是否有等效的方法来做到这一点?
在bash中的用法示例:
sleep 30 &
当前回答
博士tl;
Start-Process powershell { sleep 30 }
其他回答
在PowerShell Core 6.0中,你可以在命令的结尾写&,这相当于在当前工作目录的后台运行你的管道。
它并不等同于bash中的&,它只是当前PowerShell作业特性的一种更好的语法。它返回一个作业对象,以便您可以使用用于作业的所有其他命令。例如Receive-Job:
C:\utils> ping google.com &
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
35 Job35 BackgroundJob Running True localhost Microsoft.PowerShell.M...
C:\utils> Receive-Job 35
Pinging google.com [172.217.16.14] with 32 bytes of data:
Reply from 172.217.16.14: bytes=32 time=11ms TTL=55
Reply from 172.217.16.14: bytes=32 time=11ms TTL=55
Reply from 172.217.16.14: bytes=32 time=10ms TTL=55
Reply from 172.217.16.14: bytes=32 time=10ms TTL=55
Ping statistics for 172.217.16.14:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 10ms, Maximum = 11ms, Average = 10ms
C:\utils>
如果你想在后台执行一些语句,你可以结合& call操作符,{}脚本块和这个新的& background操作符,如下所示:
& { cd .\SomeDir\; .\SomeLongRunningOperation.bat; cd ..; } &
以下是来自文档页面的更多信息:
PowerShell Core 6.0新增功能:
Support backgrounding of pipelines with ampersand (&) (#3360) Putting & at the end of a pipeline causes the pipeline to be run as a PowerShell job. When a pipeline is backgrounded, a job object is returned. Once the pipeline is running as a job, all of the standard *-Job cmdlets can be used to manage the job. Variables (ignoring process-specific variables) used in the pipeline are automatically copied to the job so Copy-Item $foo $bar & just works. The job is also run in the current directory instead of the user's home directory. For more information about PowerShell jobs, see about_Jobs.
from about_operators / & &后台操作符&:
Ampersand background operator & Runs the pipeline before it in a PowerShell job. The ampersand background operator acts similarly to the UNIX "ampersand operator" which famously runs the command before it as a background process. The ampersand background operator is built on top of PowerShell jobs so it shares a lot of functionality with Start-Job. The following command contains basic usage of the ampersand background operator. Get-Process -Name pwsh & This is functionally equivalent to the following usage of Start-Job. Start-Job -ScriptBlock {Get-Process -Name pwsh} Since it's functionally equivalent to using Start-Job, the ampersand background operator returns a Job object just like Start-Job does. This means that you are able to use Receive-Job and Remove-Job just as you would if you had used Start-Job to start the job. $job = Get-Process -Name pwsh & Receive-Job $job Output NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- 0 0.00 221.16 25.90 6988 988 pwsh 0 0.00 140.12 29.87 14845 845 pwsh 0 0.00 85.51 0.91 19639 988 pwsh $job = Get-Process -Name pwsh & Remove-Job $job For more information on PowerShell jobs, see about_Jobs.
只要命令是一个可执行文件,或者是一个具有相关可执行文件的文件,就可以使用Start-Process(从v2可用):
Start-Process -NoNewWindow ping google.com
你也可以在你的配置文件中添加这个函数:
function bg() {Start-Process -NoNewWindow @args}
然后调用变成:
bg ping google.com
在我看来,对于在后台运行进程的简单用例来说,Start-Job是一种过度使用:
Start-Job不能访问现有的作用域(因为它在单独的会话中运行)。无法执行“Start-Job {notepad $myfile}” Start-Job不保存当前目录(因为它在单独的会话中运行)。不能执行“Start-Job {notepad myfile.txt}”,其中myfile.txt位于当前目录。 输出结果不会自动显示。您需要以作业ID为参数运行Receive-Job。
注意:对于您最初的示例,“bg sleep 30”将不起作用,因为sleep是一个Powershell命令行。Start-Process只在实际fork一个进程时才有效。
我已经在PowerShell v1.0中成功地使用了这里描述的解决方案http://jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!130.entry。在PowerShell v2.0中,这肯定会更容易。
ps2> start-job {start-sleep 20}
我还没有弄清楚如何实时获得stdout, start-job要求你用get-job轮询stdout
更新:我不能开始工作轻松地做我想要的基本上是bash &操作符。这是我目前为止最好的一招
PS> notepad $profile #edit init script -- added these lines
function beep { write-host `a }
function ajp { start powershell {ant java-platform|out-null;beep} } #new window, stderr only, beep when done
function acjp { start powershell {ant clean java-platform|out-null;beep} }
PS> . $profile #re-load profile script
PS> ajp
传递给Start-Job的脚本块似乎没有在与Start-Job命令相同的当前目录下执行,因此如果需要,请确保指定完全限定的路径。
例如:
Start-Job { C:\absolute\path\to\command.exe --afileparameter C:\absolute\path\to\file.txt }