2

What I need?

Run 3 commands from 1 batch script, and terminate them all with ctrl+c:

sass -w styles/scss/main.scss:styles/css/main.css --style compressed --no-cache
grunt watch
php -S 127.0.0.1:8081

What I tried:

start /b "sass -w styles/scss/main.scss:styles/css/main.css --style compressed --no-cache"
start /b "grunt watch"
start /b "php -S 127.0.0.1:8081"

And:

start /b "sass -w styles/scss/main.scss:styles/css/main.css --style compressed --no-cache"
start /b "grunt watch"
start /b "php -S 127.0.0.1:8081"

And:

start /b cmd /c "sass -w styles/scss/main.scss:styles/css/main.css --style compressed --no-cache"
start /b cmd /c "grunt watch"
start /b cmd /c "php -S 127.0.0.1:8081"

This one doesn't work, but I can only terminate Sass. When "grunt watch" hits - no longer able to terminate it.

Grunt:

> where grunt

> C:\Users\%USERNAME%\AppData\Local\scoop\apps\nodejs\0.12.0\nodejs\grunt
> C:\Users\%USERNAME%\AppData\Local\scoop\apps\nodejs\0.12.0\nodejs\grunt.cmd


> type C:\Users\%USERNAME%\AppData\Local\scoop\apps\nodejs\0.12.0\nodejs\grunt.cmd

> @IF EXIST "%~dp0\node.exe" (
>    "%~dp0\node.exe"  "%~dp0\node_modules\grunt-cli\bin\grunt" %*
> ) ELSE (
>    @SETLOCAL
>    @SET PATHEXT=%PATHEXT:;.JS;=;%
>    node  "%~dp0\node_modules\grunt-cli\bin\grunt" %*
>)

Sass:

> where sass

> C:\Users\%USERNAME%\AppData\Local\scoop\apps\ruby\2.1.5\bin\sass.bat


> type C:\Users\%USERNAME%\AppData\Local\scoop\apps\ruby\2.1.5\bin\sass.bat

> @ECHO OFF
> IF NOT "%~f0" == "~f0" GOTO :WinNT
> @"ruby.exe"    "C:/Users/%USERNAME%/AppData/Local/scoop/apps/ruby/2.1.5/bin/sass" %1 %2 %3 %4 %5 %6 %7 %8 %9
> GOTO :EOF
> :WinNT
> @"ruby.exe" "%~dpn0" %*

I also found this: Powershell: Run multiple jobs in parralel and view streaming results from background jobs

But it's: 1. PowerShell, not plain cmd.exe 2. Too difficult for me to understand.

But still, if cmd isn't capable of running 3 cmds at once, I might switch to PowerShell. So PS solutions are acceptable too.

Thank you.

UPD

Just for clarification. In bash I do traps: https://stackoverflow.com/a/3414377/898099 . And I get the logs with them. Need the same for windows.

Community
  • 1
  • 1
Aleksandr Makov
  • 2,820
  • 3
  • 37
  • 62
  • as per `start /?`: `/b` = "start application without a new window, the application has ^C handling ignored". – Marc B Feb 18 '15 at 15:26
  • Yes, I've read it. BUT on practice it does work inconsistently. That's why I saw hope in it ;) even though it wasn't documented. – Aleksandr Makov Feb 18 '15 at 15:35

1 Answers1

0

Here's a powershell solution for you. You can use powershell jobs to kick off your processes and intercept the Ctrl+C and use it instead to handle the stop, retrieval of output, and removal of the jobs that were created:

$job1 = Start-Job { sass -w styles/scss/main.scss:styles/css/main.css --style compressed --no-cache }
$job2 = Start-Job { grunt watch }
$job3 = Start-Job { php -S 127.0.0.1:8081 }

Write-Host "Jobs started. Ctrl+C to terminate"

[console]::TreatControlCAsInput = $true

while($true) {

    Sleep -milliseconds 500

    if ([console]::KeyAvailable) {
        $key = [system.console]::readkey($true)
        if (($key.modifiers -band [consolemodifiers]"control") -and ($key.key -eq "C")) {
            break;
        }
    }
}

Stop-Job $job1, $job2, $job3
$job1, $job2, $job3 | Foreach-Object { 
  Write-Host "Job $($_.id):"
  Receive-Job $_ 
  Remove-Job $_
  Write-Host
}

If you're not bothered about what the output of these jobs is, you can remove the line that says Receive-Job $_

arco444
  • 22,002
  • 12
  • 63
  • 67
  • Thanks. But I see nothing in the PowerShell window except "Ctrl+C to terminate" CTA, and looks like jobs aren't running. – Aleksandr Makov Feb 18 '15 at 15:53
  • Also, for some reason I have to ctrl+c like 5+ times to be able to close the window. – Aleksandr Makov Feb 18 '15 at 15:54
  • You'd really need to save that into a `ps1` file and execute it from a powershell prompt. You won't see the jobs because they run in the background (separate thread). Would you rather have a new console pop up for each process? – arco444 Feb 18 '15 at 16:03
  • The point is, jobs weren't running. As not grunt, nor sass generated files as if they were executed explicitly. And .ps1 is exactly what I did. – Aleksandr Makov Feb 18 '15 at 19:28
  • And as of output. Well, yea, I need to see the logs, cos there might be errors. In bash I could trap processes, and it works just fine: http://stackoverflow.com/a/3414377/898099 – Aleksandr Makov Feb 18 '15 at 19:30
  • Even if the jobs fail to launch, you should at least see a message saying that's what happened when you hit ctrl+c because `Receive-Job` will pull back the output. Have you just tried manually launching one of them from a console window? – arco444 Feb 19 '15 at 09:59
  • Yes. It works when I manually execute direct commands. Also, the batch script with contents from 3rd example - is what works too. It shouldnt, it's not supported, but it works. With your solution @arco444, the only last command is being executed and it loses the CWD. So php does run it's built-in server, but from different directory. – Aleksandr Makov Feb 27 '15 at 15:09