Search code examples
powershellcmdexitexecution

powershell run from cmd to to start program and exit. The program will stay running, but powershell should extit


I am trying to run windowed version on JlinkGDBServer (it is the windows application with GUI) at the end of the Ninja build process.

The powershell is run from the cmd

cmd.exe /C "powershell  & "C:/Program Files (x86)/SEGGER/JLink/JLinkGDBServer.exe" -port 2331 -s -CPU Cortex-M -device STM32L476RG -endian little -speed 4000 -vd &"

but the powershell does not exit (the jlinkGDBserver window should remain)


Edit: It is run from the Ninja script generated by the Cmake. I have only partial control here.

The solution:

powershell -command Start-Process -FilePath "'C:/Program Files (x86)/SEGGER/JLink/JLinkGDBServer.exe'" -ArgumentList '-port 2331', '-s', '-CPU Cortex-M', '-device stm32l476RG', '-endian little', '-speed 4000', '-vd'

Solution

  • If you can call cmd.exe or powershell.exe, you should also be able to call the program directly. I don't know why you're invoking cmd.exe to invoke powershell.exe to invoke JLinkGDBServer.exe.

    Unless there's some nuance with ninja I don't understand this should be all you need to run your command:

    "C:/Program Files (x86)/SEGGER/JLink/JLinkGDBServer.exe" -port 2331 -s -CPU Cortex-M -device STM32L476RG -endian little -speed 4000 -vd
    

    For the record, your PowerShell process likely isn't exiting for one of two reasons:

    1. You didn't use call it with powershell.exe -command. This works similarly to cmd.exe /c, where it runs the command and exits. Omitting this parameter will run whatever you tell it and then drop back to interactive mode (unless you execute it as a non-interactive user).

    2. It's also possible that JLinkGDBServer.exe blocks execution and wasn't even dropping back to interactive mode once the process launched. In this case you can wrap the command with Start-Job which will start it without attaching it to your current PowerShell process. For example:

      powershell -command "Start-Job { 'C:/Program Files (x86)/SEGGER/JLink/JLinkGDBServer.exe' -port 2331 -s -CPU Cortex-M -device STM32L476RG -endian little -speed 4000 -vd" }
      

      Using Start-Job is fundamentally similar to appending an & to the end of your command in Linux, and should give you the same effect here.