companion icon indicating copy to clipboard operation
companion copied to clipboard

Start as a windows service

Open brianteeman opened this issue 1 year ago • 7 comments

Is this a feature relevant to companion itself, and not a module?

  • [X] I believe this to be a feature for companion, not a module

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the feature

The ability to run companion as a windows service would be great. Then we can start companion automatically with windows and not have to login

Usecases

The remote PC used for companion will regularly reboot and sit at the windows login screen. Any attached surface or comapnion satellite will be unable to work until someone gets to the PC and logs in. (assuming they even know why nothing is working).

brianteeman avatar Feb 20 '24 20:02 brianteeman

I think it is entirely possible to do this already, with some effort. I'm not sure what we should do on this here, other than perhaps a wiki page describing the process? Do you fancy drafting a guide for setting this up?

I think the process will be to:

  • install companion like normal
  • Use some tool (not sure if there is anything builtin to windows for this, or if something needs to be found) to create the service
    • This should run c:\program files\companion\resources\node-runtime\node.exe c:\program files\companion\resources\main.js
    • Some arguments will be needed to that command.
      • Possibly just --config-dir c:\some-dir-here using some directory that this service will have write access to and should use to store the config
      • --admin-address or --admin-interface may want to be set, depending on your needs
    • The service may need to be run as a specific user, probably doesnt need to be an administrator

It is also worth noting, that this companion will not appear in the tray, but the interface will be accessible in a browser at http://localhost:8000 like normal

Julusian avatar Feb 25 '24 18:02 Julusian

Thanks - will try that

brianteeman avatar Feb 25 '24 19:02 brianteeman

@brianteeman where you able to get this to work as a service in windows?

swamiforlife2 avatar Mar 12 '24 07:03 swamiforlife2

Also interested in if you were able to get this to work.

lfrisk avatar Mar 24 '24 15:03 lfrisk

Hey ... i think i found a solution/workaround...

As far as I've read it's not possible to run node as a native windows service. But ...

You can use a wrapper like WinSW (https://github.com/winsw/winsw) to run companion as Service. I used the following steps:

  1. Download and install Companion as usual. Disable companion's Autostart in your user if already enabled.
  2. Create a folder to store the wrapper and the companion config data -> C:\ProgramData\companion
  3. Download Winsw to C:\ProgramData\companion and rename it to companion.exe
  4. Create the winsw config file C:\ProgramData\companion\companion.xml. I've uploaded mine with this Comment.
  5. Start a cmd with admin privileges and cd into C:\ProgramData\companion.
  6. Run companion.exe install ... This now creates the service called companion which can be found in the services.msc.
  7. Now run companion.exe start to start the service. At this point you should be able to open http://127.0.0.1:8000/ with your web browser :)

My companion.xml:

<service>
  <id>companion</id>
  <name>Companion</name>
  <description>Run Bitfocus Companion as Service</description>
  <executable>"c:\program files\companion\resources\node-runtime\node.exe"</executable>
  <arguments>"c:\program files\companion\resources\main.js" "--config-dir" "C:\ProgramData\companion" "--admin-address" "127.0.0.1"</arguments>
</service>

For more info on WinSW have a look at their Readme and the doc folder inside the project.

IMBArator avatar Apr 10 '24 17:04 IMBArator

Really sorry about the delay in replying. Life got in the way - these are my notes from doing it using nssm

Download the latest version and extract the contents to a directory of your choice.

  • Open a Command Prompt with Administrator Privileges:

  • Navigate to the NSSM Directory:

  • Install the Node.js Service: nssm install YourServiceName "C:\Program Files\Companion\Resources\node-runtime\node.exe" "C:\Program Files\Companion\Resources\main.js" Replace YourServiceName with the desired name for your service.

  • Configure Service Parameters: A graphical interface will appear. Here, you can configure parameters like startup directory, dependencies, etc. Make sure to set the correct paths and settings for your Node.js script.

  • Save Configuration:

  • Click on the "Install Service" button to save your configuration and install the service.

  • Start the Service: net start YourServiceName Replace YourServiceName with the name you provided before.

Now, your Node.js script should be running as a service on your Windows 11 system. You can manage the service using the "Services" application (services.msc) or other service management tools.

brianteeman avatar Apr 10 '24 17:04 brianteeman

@Julusian, @swamiforlife2, @lfrisk, @brianteeman

I'm not sure if Companion is running as a service, but here are the steps I do to have Companion start with a machine running Windows.

  1. Install Companion as normal.
  2. Uncheck the Run at Login box (to avoid the error Companion is already running).
  3. Change the GUI interface dropdown to All Interfaces 0.0.0.0.
  4. Open Task Scheduler.
  5. Create a Task
    • Choose a task trigger as "At startup". (Choose a delay if you have services or connections that need to run on that machine before starting Companion).
    • Choose to start a program from a location such as "C:\Program Files\Companion\Companion.exe".
    • Modify Conditions or Settings as needed. I suggest checking "Allow task to be run on demand", and "If the running task does not end when requested, force it to stop".
    • After creating the task, modify the General properties to select "Run whether the user is logged on or not. You don't have to use as password unless you would need Companion to access more resources than normal.

Notes: Companion does not show up in the system tray. If you need to change the GUI interface, you will need to stop the Companion task from inside task schedular and start Companion from the app list/program location.

mwb9aa avatar Apr 17 '24 17:04 mwb9aa