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
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
tl;dr
Start-Process powershell { sleep 30 }
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 }
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.
You can use PowerShell job cmdlets to achieve your goals.
There are 6 job related cmdlets available in PowerShell.
If interesting about it, you can download the sample How to create background job in PowerShell
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:
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.