'Run commands in new terminals, then kill later

I have a start.bat similar to the following:

start npx tailwindcss -i ./www/src/styles/main.css -o ./www/dist/styles/main.css --watch
start caddy run

It launches 2 new cmd prompts and starts processes within them (which both run indefinitely). Is there a way for this bat file to send a kill command to the cmds it spawned? Ideally I'd have a single command to kill all "child" processes which were created.



Solution 1:[1]

set "wintitle=my process %time%"
start "%wintitle%" npx tailwindcss -i ./www/src/styles/main.css -o ./www/dist/styles/main.css --watch
start "%wintitle%" caddy run

....

taskkill /FI "WINDOWTITLE eq %wintitle%"

[untested] - should work in theory...

Always best to start with a quoted-title as the first quoted string seen by the start command may be eaten and used as a title otherwise.

Solution 2:[2]

A PowerShell solution - which is acceptable as both implied by this question's tagging and as explicitly stated by icanfathom (the OP) in a comment ("No, PS would be fine") - using the Start-Process and Stop-Process cmdlets:

$processes = & {
  Start-Process -PassThru npx 'tailwindcss -i ./www/src/styles/main.css -o ./www/dist/styles/main.css --watch'
  Start-Process -PassThru caddy run
}

# ...

# Stop (kill) all launched processes.
$processes | Stop-Process

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Magoo
Solution 2