3

This is really basic, but I can't find the answer. The installer sets up my path so that I can just type the command:

ng serve

at the command prompt and the script runs. I don't want to wait for this program to finish (it's a server, after all). How do I launch the same script (it's a CMD script as far as I can tell) from Powershell without waiting for it to finish (and without having to find the source directory for the script)?

Biffen
  • 6,249
  • 6
  • 28
  • 36
Quark Soup
  • 4,272
  • 3
  • 42
  • 74

1 Answers1

6

If it's acceptable to terminate the server when the PowerShell session exits, use a background job:

In PowerShell (Core) 7+

ng server &

In Windows PowerShell, explicit use of Start-Job is required:

Start-Job { ng server }

Both commands return a job-information object, which you can either save in a variable ($jb = ...) or discard ($null = ...)

If the server process produces output you'd like to monitor, you can use the Receive-Job cmdlet.

See the conceptual about_Jobs topic for more information.


If the server must continue to run even after the launching PowerShell session exits, use the Start-Process cmdlet, which on Windows launches an independent process in a new console window (by default); use the -WindowStyle parameter to control the visibility / state of that window:

Start-Process ng server # short for: Start-Process -FilePath ng -ArgumentList server

Note: On Unix-like platforms, where Start-Process doesn't support creating independent new terminal windows, you must additionally use nohup - see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • On Windows PowerShell 5.1, `Start-Process` with `-NoNewWindow` option create process (external GUI program) that will be terminated after PowerShell session exists. In order to run GUI program even after PowerShell session exists and without additional console window, `Start-Process -WindowStyle Hidden` worked. E.g. `Start-Process -WindowStyle Hidden winmergeu.exe "/s /t text ""${file1}"" ""${file2}"""` – aruku7230 Feb 09 '23 at 07:34
  • @aruku7230, `-NoNewWindow` has no effect on GUI-subystem applications, and the application always lives on after the launching PowerShell session exits.Similarly, the use of `-WindowStyle` has no impact on the lifetime of the launched process (but `-WindowStyle Hidden` hides the process' window, both for console-subsystem and GUI subsystem applications). .Only if the launched process is a console-subsystem application and you use `-NoNewWindow` does closing the calling window terminate the launched process too. – mklement0 Feb 09 '23 at 09:43