George Mauer
George Mauer

Reputation: 122052

Start powershell job to run process that stays open in the background

Please note that while this post asks about coffeescript.cmd I don't think the answer has anything to do with it but more with me misunderstanding powershell's job system.


So to compile and watch coffeescript I can call

> coffee.cmd -cw ./scripts

which will start the coffeescript process. Due to the w watch flag, the process will stay open and recompile any changes until I Ctrl+c.

What I would like is to start this process but have it run in the background so that I can continue using my console (yes, I know I can have multiple consoles). Ideally, any output from that job will just be pushed to my console as it happens.

So I thought I could do this

> start-job { coffee.cmd -cw .\scripts }
Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
3      Job3            BackgroundJob   Running       True            localhost             coffee.cmd -cw .\scripts

Unfortunately that seems a) swallow all output, and more importantly b) to finish immediately and not stay watching my directory

W:\surge\ogre> Get-Job

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
3      Job3            BackgroundJob   Completed     False           localhost             coffee.cmd -cw .\scripts

Note that I'm aware of this post but as I've demonstrated I can't seem to get the job to stay open even when I Recieve-Job.

What's going on? Are jobs not the right tool for this? What's the correct way to start a process that waits in the background? How do I redirect output from it to my console.

Upvotes: 1

Views: 1322

Answers (1)

George Mauer
George Mauer

Reputation: 122052

I've managed to figure out part of this thanks to a comment by @MickyBalladelli. Namely that this

start-job { Get-Location } | Receive-Job -Wait

returns ~/Documents. Aha! so relative paths won't work. When I do

Start-Job { coffee.cmd -cw w:\absoltue\path\to\scripts }

The job does remain running and will compile stuff in the background. Of course results (compliation successes and errors) don't get streamed to my console. In order to do that I need to explicitly call Get-Job | Receive-Job.

That's annoying but one step closer to what I really want.

Upvotes: 1

Related Questions