Running Factorio as a windows service with NSSM

Anything that prevents you from playing the game properly. Do you have issues playing for the game, downloading it or successfully running it on your computer? Let us know here.
celludriel
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat May 27, 2017 12:33 am
Contact:

Running Factorio as a windows service with NSSM

Post by celludriel »

Hey,

I'm trying to run Factorio as a windows service, I found NSSM http://nssm.cc/. This wraps factorio as a windows service and it works. The service runs and I can connect , my friend can connect. But it has one major flaw. For some reason it cannot write any savegames. No autosaves, on shutdown not the regular savegame. The log updates on startup but doesn't seem to update afterwards.

Is there anyone that managed to setup factorio as a windows service ? It doesn't have to be with nssm I'm open to anything. My goal is simple when the pc reboots factorio should boot up normally. Preferably as a clean windows service as it should be.

Any help would be appreciated I don't know where to look anymore, for the past hours I was focussing on windows directory security but that doesn't seem to be it. Cause well the log gets created on startup so there is write access in the folder
User avatar
mexmer
Filter Inserter
Filter Inserter
Posts: 870
Joined: Wed Aug 03, 2016 2:00 pm
Contact:

Re: Running Factorio as a windows service with NSSM

Post by mexmer »

what type of install you use for factorio as service?

it's preferable to use ZIP version, since you can deploy it anywhere, and it saves to "data" folder that is inside your unzipped game folder.

make sure server-config.json has properly set paths, and that also service has properly set working directory (eg. folder where you unzipped factorio), also make sure, that account under which is service running has write access to that folder (that's why i suggest using zip version, and not normal install)
celludriel
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat May 27, 2017 12:33 am
Contact:

Re: Running Factorio as a windows service with NSSM

Post by celludriel »

I got it to work eventually :) Had to wait from work to do a writeup so others can enjoy the procedure. I'm using the steam install experimental version of factorio myself but it should work with any. I like to post this on the wiki I asked for an account so if I get granted one I can post it there but here goes.

NSSM headless server setup
========================
On a windows machine you ideally want each server you have running as a service instead of just in a cmd window. It has the advantage to startup with windows, and when windows closes it will receive a clean shutdown command, which in case of factorio is needed to save the map. With the thousand windows updates and reboots this is no unneeded luxury.

Preparation
===========
- Create a new windows user account with admin privileges for running factorio
- Download the latest version of NSSM, it's pretty stable so you can use the development build (https://nssm.cc/builds)
- Unzip NSSM in a convenient directory I chose: C:\Tools\NSSM
- Create a directory to hold all your server data with the new user fe: C:\FactorioData\server01 (the server01 subdir is if you are interested in running multiple server instances)
- Create a server-settings.json file with how you want your server to be configured for more information check the factorio wiki

Setup
=====
- Update config.ini in %appdata%\Roaming\Factorio\config so it reflects the following in it's [path] settings (the write-data is the dir you chosen during preparation)

[Path]
read-data=___PATH___system-read-data
write-data=C:\FactorioData\server01

- Go to your factorio folder where the exe is located
- Create a new game: factorio --create saves/my-save.zip --map-gen-settings path-to-file-with-desired-map-generation-settings.json (for more information check: https://wiki.factorio.com/Multiplayer#S ... layer_Game)
- Create a new file server01.cmd
- Edit this file with the following: factorio --start-server-load-latest --server-settings "C:\FactorioData\server01\server-settings.json"
- Go to the directory where you installed NSSM in my case C:\Tools\NSSM
- Execute the following cmd: nssm install FactorioServer01
- A dialog will open with a few tabs
- In the application tab for path browse to the newly created server01.cmd file and choose that file, in the startup directory path choose the directory where factorio.exe is located
- Go to the Log on tab this step is very important
- Check the third option and for This account choose the newly created account for factorio like so .\FactorioServerAccount and for password fill in twice the password for the account. Why is this important, Factorio needs write access to the data directory you created earlier with this account, running the service with this account will provide that
- Go to the Shutdown tab
- check generate CTRL+C and Terminate process and Kill process tree uncheck the others, the Timeout for CTRL+C should be 10 seconds (gives it ample time to write the savegame, for bigger saves you might want to increase this even to 30) if the save does not succeed before the timeout is reached the process will be killed and you lose your save !
- Click on save\edit service and you are good to go

notes
=====
- Don't move NSSM from it's location since the service will use that nssm.exe to wrap your server01.cmd file and run it as a service, move it and the config will be up and your service is broken
- You need to make the server01.cmd file and not call factorio.exe directly in nssm cause factorio is detached from the console, it will never recieve the CTRL+C command on shutdown. By wrapping it in a cmd file, the cmd has a console and will forward CTRL+C to factorio. This took me a while to figure out it IS important

If my tutorial is incomplete or you get stuck let me know and I'll try to make adjustments. Hope it helps people out
Last edited by celludriel on Mon Sep 27, 2021 8:40 am, edited 1 time in total.
User avatar
mexmer
Filter Inserter
Filter Inserter
Posts: 870
Joined: Wed Aug 03, 2016 2:00 pm
Contact:

Re: Running Factorio as a windows service with NSSM

Post by mexmer »

i would not recommend doing it as you did, unless you have dedicated machine or account, that runs server as service, what you did also affects regular game client, because you changed settings in %appdata%/Factorio

it's better to separate server and game client. you will need to update them both, when new version comes out, but still it's better, than having other issues, that can and will come from sharing configuration data between client and server.
celludriel
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat May 27, 2017 12:33 am
Contact:

Re: Running Factorio as a windows service with NSSM

Post by celludriel »

Well I see what you mean. This is why I made a different windows account though, using it's own %appdata%. In principle for each server you could make a seperate windows account if I'm not mistaken.

However I'm open for suggestions to improve on this, very open even :)
User avatar
mexmer
Filter Inserter
Filter Inserter
Posts: 870
Joined: Wed Aug 03, 2016 2:00 pm
Contact:

Re: Running Factorio as a windows service with NSSM

Post by mexmer »

celludriel wrote:Well I see what you mean. This is why I made a different windows account though, using it's own %appdata%. In principle for each server you could make a seperate windows account if I'm not mistaken.

However I'm open for suggestions to improve on this, very open even :)
if you running in under different account, then there should not be problem. it's common practice on server, was in impression, you doing that on desktop.
Steffel
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sat Jan 01, 2022 2:53 pm
Contact:

Re: Running Factorio as a windows service with NSSM

Post by Steffel »

A solution based on .NET6 can be found here: pi19.de/factorio
Post Reply

Return to “Technical Help”