Search code examples
cmdgithub-actions

Running a server in background using cmd shell for Windows


I am trying to run a file called CoreServer.exe in the background using cmd.exe as mentioned here. When I specify the path of the server, it tells me,

'.' is not recognized as an internal or external command,
operable program or batch file.

On the other hand if I remove shell: cmd it works fine. But in this case it runs using powershell.exe and PowerShell is not what I want.

Here is my workflow file.

name: Test unit tests CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

  
jobs:
  build:

    runs-on: windows-2022

    steps:
    - uses: actions/checkout@v3

    - name: setup Msbuild
      uses: microsoft/[email protected]
      

#try nc -v -N 127.0.0.1 3334
 #curl http://localhost:3333 -I
 #vstest.console.exe ./Pico/Medman/FluidTransferTests/bin/Release/net481/FluidTransferTests.dll
#Start CoreServer.exe in background
    
    - name: Start CoreServer.exe in the background
      shell: cmd
      run: ./Debug/CoreServer.exe -c -s &

    - name: Wait 7 seconds
      run: sleep 7

    - name: See status
      run: netstat -ntp

To add more information, when I run CoreServer.exe in a Command Prompt, on my local machine, it runs fine, and shows the following:

CoreServer.exe -c -s


Solution

  • The windows shell doesn't handle linux style path separators. ./debug must be .\debug, that will remove the error.

        - name: Start CoreServer.exe in the background
          shell: cmd
          run: .\Debug\CoreServer.exe -c -s &
    

    It's generally not recommended to run things in the background this way. It's better to run a service container alongside the job.

    The runner will kill any processes that are launched by a step.

    There is a way to work around the process-killing feature by resetting the environment variable that tracks the process identifiers.

    In Windows command shell this should look like:

        # ⚠️ untested
        - name: Start CoreServer.exe in the background
          shell: cmd
          run: |
              set RUNNER_TRACKING_ID=""
              .\Debug\CoreServer.exe -c -s &
    
    

    Or by combining all of the elements into a single script step:

        - name: Start CoreServer.exe in the background
          shell: cmd
          run: |
            .\Debug\CoreServer.exe -c -s &
            sleep 7
            netstat -ntp