Powershell相当于bash&符号(&)用于分叉/运行后台进程

时间:2008-10-09 01:16:33

标签: powershell

在bash中,&符号(&)可用于在后台运行命令,并在命令运行完毕之前将交互式控件返回给用户。在Powershell中有没有相同的方法呢?

bash中的用法示例:

 sleep 30 &

9 个答案:

答案 0 :(得分:105)

只要命令是可执行文件或具有关联可执行文件的文件,请使用启动过程(可从v2获得):

Start-Process -NoNewWindow ping google.com

您也可以将此功能添加到个人资料中:

function bg() {Start-Process -NoNewWindow @args}

然后调用变为:

bg ping google.com

在我看来,Start-Job对于在后台运行流程的简单用例来说是一种过度杀伤:

  1. Start-Job无权访问您的现有范围(因为它在单独的会话中运行)。你不能做“Start-Job {notepad $ myfile}”
  2. Start-Job不保留当前目录(因为它在单独的会话中运行)。您不能执行“Start-Job {notepad myfile.txt}”,其中myfile.txt位于当前目录中。
  3. 输出不会自动显示。您需要使用作业的ID作为参数运行Receive-Job。
  4. 注意:关于您的初始示例,“bg sleep 30”不起作用,因为sleep是Powershell命令行开关。 Start-Process仅在您实际分叉流程时才有效。

答案 1 :(得分:37)

这个问题有几个答案。

  1. 您无法在版本1中轻松完成
  2. 版本2(现在在社区技术预览2中)确实具有此功能,它被称为Job(以前称为PSJob)。详细了解herehere
  3. 你可以在v1中实际做到the hard way,随意拥有它,但我从来没有打扰过。

答案 2 :(得分:19)

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

答案 3 :(得分:19)

似乎传递给Start-Job的脚本块没有使用与Start-Job命令相同的当前目录执行,因此请确保在需要时指定完全限定的路径。

例如:

Start-Job { C:\absolute\path\to\command.exe --afileparameter C:\absolute\path\to\file.txt }

答案 4 :(得分:12)

您可以使用PowerShell作业cmdlet来实现目标。

PowerShell中有6个与作业相关的cmdlet。

  • 获取-工作
    • 获取当前会话中运行的Windows PowerShell后台作业
  • 接收 - 工作
    • 获取当前会话中的Windows PowerShell后台作业的结果
  • 删除 - 工作
    • 删除Windows PowerShell后台作业
  • 启动工作
    • 启动Windows PowerShell后台作业
  • 停止-工作
    • 停止Windows PowerShell后台作业
  • 等待-工作
    • 禁止命令提示,直到会话中运行的一个或所有Windows PowerShell后台作业完成

如果感兴趣,可以下载样本How to create background job in PowerShell

答案 5 :(得分:8)

在PowerShell Core 6.0中,您可以在命令末尾编写&,这等效于在当前工作目录中在后台运行管道

在bash中它不等于&,它只是当前PowerShell jobs功能的更好语法。它返回一个作业对象,因此您可以使用将用于作业的所有其他命令。例如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 operator{ } script block和这个新的& background operator结合起来,如下所示:

& { cd .\SomeDir\; .\SomeLongRunningOperation.bat; cd ..; } &

以下是文档页面中的更多信息:

来自What's New in PowerShell Core 6.0

  

使用&号(#3360)支持管道的背景

     

在管道的末尾插入&会使管道作为PowerShell作业运行。当管道后台运行时,将返回作业对象。管道作为作业运行后,所有标准*-Job cmdlet均可用于管理作业。管道中使用的变量(忽略特定于进程的变量)会自动复制到作业中,因此Copy-Item $foo $bar &可以正常工作。该作业还将在当前目录而不是用户的主目录中运行。有关PowerShell作业的更多信息,请参见about_Jobs

来自about_operators / Ampersand background operator &

  

放大器和后台运算符&

     

在PowerShell作业中先运行管道。 “&”号背景运算符的行为类似于UNIX“&”号运算符,后者在运行命令之前先将其作为后台进程运行。 &符号背景运算符建立在PowerShell作业之上,因此它与Start-Job共享许多功能。以下命令包含&符号背景运算符的基本用法。

Get-Process -Name pwsh &
     

这在功能上等效于Start-Job的以下用法。

     

Start-Job -ScriptBlock {Get-Process -Name pwsh}

     

由于在功能上等同于使用Start-Job,所以&符号背景运算符将返回Job对象,就像Start-Job does一样。这意味着您可以使用Receive-JobRemove-Job,就像您使用Start-Job开始工作一样。

$job = Get-Process -Name pwsh &
Receive-Job $job
     

输出

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
     

有关PowerShell作业的更多信息,请参见about_Jobs

答案 6 :(得分:0)

我在PowerShell v1.0中成功使用了此处描述的解决方案http://jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!130.entry。在PowerShell v2.0中肯定会更容易。

答案 7 :(得分:0)

您可以执行以下操作。

$a = start-process -NoNewWindow powershell {timeout 10; 'done'} -PassThru

done

如果您要等待:

$a | wait-process

奖金osx或linux版本:

$a = start-process pwsh '-c',{start-sleep 5; 'done'} -PassThru 

答案 8 :(得分:0)

tl; dr

.slick-arrow {
    position: absolute;
    top: 0;
    width: calc(50% - 200px);
    height: 100%;
    line-height: 50px;
    border: none;
    background: blue;
    color: #fff;
    font-family: monospace;
    font-size: 5rem;
    z-index: 300;
    outline: none;
    opacity: 0;
    cursor: pointer;
}