edd*_*ves 138 powershell
在bash中,&符号(&)可用于在后台运行命令,并在命令运行完毕之前将交互式控件返回给用户.在Powershell中有没有相同的方法呢?
bash中的用法示例:
sleep 30 &
Run Code Online (Sandbox Code Playgroud)
Bog*_*mac 115
只要命令是可执行文件或具有关联可执行文件的文件,请使用Start-Process(可从v2获得):
Start-Process -NoNewWindow ping google.com
Run Code Online (Sandbox Code Playgroud)
您也可以在配置文件中将其添加为功能:
function bg() {Start-Process -NoNewWindow @args}
Run Code Online (Sandbox Code Playgroud)
然后调用变为:
bg ping google.com
Run Code Online (Sandbox Code Playgroud)
在我看来,Start-Job对于在后台运行进程的简单用例来说太过分了:
注意:关于您的初始示例,"bg sleep 30"不起作用,因为sleep是Powershell命令行开关.Start-Process仅在实际分叉进程时有效.
hal*_*000 36
这是这个问题的几个答案.
Gia*_*rdi 21
似乎传递给的脚本块Start-Job不是使用与Start-Job命令相同的当前目录执行的,因此请确保在需要时指定完全限定的路径.
例如:
Start-Job { C:\absolute\path\to\command.exe --afileparameter C:\absolute\path\to\file.txt }
Run Code Online (Sandbox Code Playgroud)
Dus*_*etz 20
ps2> start-job {start-sleep 20}
Run Code Online (Sandbox Code Playgroud)
我还没弄明白如何实时获得stdout,start-job要求你用get-job来调查stdout
更新:我无法开始工作,轻松做我想要的,基本上是bash&operator.到目前为止,这是我最好的黑客
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
Run Code Online (Sandbox Code Playgroud)
小智 13
您可以使用PowerShell作业cmdlet来实现目标.
PowerShell中有6个与作业相关的cmdlet.
如果有意思,可以下载如何在PowerShell中创建后台作业的示例
Mar*_*ski 10
从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>
Run Code Online (Sandbox Code Playgroud)
如果你想在后台执行几个语句,可以将&调用运算符,{ }脚本块和这个新的&后台运算符组合在一起,如下所示:
& { cd .\SomeDir\; .\SomeLongRunningOperation.bat; cd ..; } &
Run Code Online (Sandbox Code Playgroud)
以下是文档页面的更多信息:
支持带&符号的管道背景()(#3360)
放在
&管道的末尾会导致管道作为PowerShell作业运行.管道后台运行时,将返回作业对象.管道作为作业运行后,所有标准*-Jobcmdlet都可用于管理作业.管道中使用的变量(忽略特定于流程的变量)会自动复制到作业中,因此Copy-Item $foo $bar &只能起作用.作业也在当前目录而不是用户的主目录中运行.有关PowerShell作业的详细信息,请参阅about_Jobs.
来自about_operators/Ampersand背景运算符&:
&符背景操作员&
在PowerShell作业中运行管道之前的管道.&符背景操作符的作用类似于UNIX"&符号运算符",它在它作为后台进程之前运行命令.&符背景运算符构建在PowerShell作业之上,因此它具有很多功能
Start-Job.以下命令包含&符背景运算符的基本用法.Run Code Online (Sandbox Code Playgroud)Get-Process -Name pwsh &这在功能上等同于以下用法
Start-Job.
Start-Job -ScriptBlock {Get-Process -Name pwsh}由于它在功能上等同于使用
Start-Job,因此&符号背景操作符返回一个Job对象Start-Job does.这意味着您可以使用Receive-Job,Remove-Job就像您曾经Start-Job开始工作一样.Run Code Online (Sandbox Code Playgroud)$job = Get-Process -Name pwsh & Receive-Job $job产量
Run Code Online (Sandbox Code Playgroud)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.
你可以做这样的事情。
$a = start-process -NoNewWindow powershell {timeout 10; 'done'} -PassThru
Run Code Online (Sandbox Code Playgroud)
如果你想等待它:
$a | wait-process
Run Code Online (Sandbox Code Playgroud)
额外的 osx 或 linux 版本:
$a = start-process pwsh '-c',{start-sleep 5; 'done'} -PassThru
Run Code Online (Sandbox Code Playgroud)
我有示例 pinger 脚本。args 作为数组传递:
$1 = start -n powershell pinger,comp001 -pa
Run Code Online (Sandbox Code Playgroud)
小智 0
我已经在 PowerShell v1.0 中成功使用了此处描述的解决方案http://jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!130.entry。在 PowerShell v2.0 中肯定会更容易。
| 归档时间: |
|
| 查看次数: |
110550 次 |
| 最近记录: |