Powershell equivalent of bash ampersand (&) for forking/running background processes

后端 未结 8 1335
北海茫月
北海茫月 2020-11-28 20:33

In bash the ampersand (&) can be used to run a command in the background and return interactive control to the user before the command has finished running. Is there an

相关标签:
8条回答
  • 2020-11-28 21:16
    ps2> start-job {start-sleep 20}
    

    i have not yet figured out how to get stdout in realtime, start-job requires you to poll stdout with get-job

    update: i couldn't start-job to easily do what i want which is basically the bash & operator. here's my best hack so far

    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
    
    0 讨论(0)
  • 2020-11-28 21:16

    tl;dr

    Start-Process powershell { sleep 30 }
    
    0 讨论(0)
  • 2020-11-28 21:17

    Seems that the script block passed to Start-Job is not executed with the same current directory as the Start-Job command, so make sure to specify fully qualified path if needed.

    For example:

    Start-Job { C:\absolute\path\to\command.exe --afileparameter C:\absolute\path\to\file.txt }
    
    0 讨论(0)
  • 2020-11-28 21:18

    I've used the solution described here http://jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!130.entry successfully in PowerShell v1.0. It definitely will be easier in PowerShell v2.0.

    0 讨论(0)
  • 2020-11-28 21:20

    You can use PowerShell job cmdlets to achieve your goals.

    There are 6 job related cmdlets available in PowerShell.

    • Get-Job
      • Gets Windows PowerShell background jobs that are running in the current session
    • Receive-Job
      • Gets the results of the Windows PowerShell background jobs in the current session
    • Remove-Job
      • Deletes a Windows PowerShell background job
    • Start-Job
      • Starts a Windows PowerShell background job
    • Stop-Job
      • Stops a Windows PowerShell background job
    • Wait-Job
      • Suppresses the command prompt until one or all of the Windows PowerShell background jobs running in the session are complete

    If interesting about it, you can download the sample How to create background job in PowerShell

    0 讨论(0)
  • 2020-11-28 21:22

    As long as the command is an executable or a file that has an associated executable, use Start-Process (available from v2):

    Start-Process -NoNewWindow ping google.com
    

    You can also add this as a function in your profile:

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

    and then the invocation becomes:

    bg ping google.com
    

    In my opinion, Start-Job is an overkill for the simple use case of running a process in the background:

    1. Start-Job does not have access to your existing scope (because it runs in a separate session). You cannot do "Start-Job {notepad $myfile}"
    2. Start-Job does not preserve the current directory (because it runs in a separate session). You cannot do "Start-Job {notepad myfile.txt}" where myfile.txt is in the current directory.
    3. The output is not displayed automatically. You need to run Receive-Job with the ID of the job as parameter.

    NOTE: Regarding your initial example, "bg sleep 30" would not work because sleep is a Powershell commandlet. Start-Process only works when you actually fork a process.

    0 讨论(0)
提交回复
热议问题