在bash中,&号(&)可用于在后台运行命令,并在命令运行完成之前将交互控制返回给用户。在Powershell中是否有等效的方法来做到这一点?
在bash中的用法示例:
sleep 30 &
在bash中,&号(&)可用于在后台运行命令,并在命令运行完成之前将交互控制返回给用户。在Powershell中是否有等效的方法来做到这一点?
在bash中的用法示例:
sleep 30 &
当前回答
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-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 }
博士tl;
Start-Process powershell { sleep 30 }