Factorio Control Script (Nasty Linux Bash)

Enhance Your Overall Game-Play.
Mod-Database, Calculators, Cheatsheets, Multiplayer, Scripts, Libs and Other Useful Stuff.
Post Reply
ChoMar
Long Handed Inserter
Long Handed Inserter
Posts: 88
Joined: Sun Aug 07, 2016 2:00 am

Factorio Control Script (Nasty Linux Bash)

Post by ChoMar » Thu Nov 30, 2017 7:13 am

I have written a (nasty) Linux Bash script to automate our Servers.
Maybe someone finds this useful and/or can improve on it.
For the Update Function I use narc0tiqs Python Updater: https://github.com/narc0tiq/factorio-updater
It runs in a Standalone folder (can be used for multiple Servers on the same Machine), runs Factorio in a Screen and can do the following things:
start: Stop a running Server, check for updates and start (+ stuff commands in, like for creative mode, disabled here)
stop: Stop a running Server
reset: Generate a new Map
message: Send a Message to the Server Console (or the Game)
update: Check for Updates, If updates are availible, do a 5 min Countdown, then stop, install updates and restart
I call the Update every 30 mins or so via Crontab. Since it only downloads when a new Update is availible, this shouldnt be a Problem.

Again, this thing is NOT good, It just works (for me). Feel free to modify and improve it, maybe reply with better versions...

Code: Select all

#!/bin/bash
script=/dev/null
# Variablen
# Name of Screen
screenName="Factorioserver2"  
# Path_To_Factorio
pfad="/home/factorio/server/Server2/"
# Path to Savegame
savegame="/home/factorio/server/Server2/saves/Server2.zip"
# Serverport
port="39100"
# Path to update script
update="/home/factorio/server/update_factorio.py"
# Commands to run after Start
# commands="/c remote.call(\"creative-mode\", \"enable\") ^M /c remote.call(\"creative-mode\", \"enable\") ^M"

# Commands
# update
# update="python /home/factorio/server/update_factorio.py -xa ${pfad}/bin/x64/factorio"
# start
start="${pfad}bin/x64/factorio --start-server ${savegame} --server-settings ${pfad}data/server-settings.json --port $port"
# create screen
scr="screen -d -m -S $screenName"

# Befehl
echo "$1"
case "$1" in
# START
	start)
		echo stoppe Factorio auf $screenName - Bitte warten
 		screen -S $screenName -X stuff ^C
 		sleep 30s
		echo beende alle Screens ${screenName}
		screen -ls | grep ${screenName} | cut -d. -f1 | tr --delete "\t" | xargs kill -9; screen -wipe;
		echo screens
		echo ${screenName}
		sleep 1s
		echo checking updates and starting on screen ${screenName}
		${scr}
		python ${update} -xa ${pfad}/bin/x64/factorio
		wait
		screen -S $screenName -X screen ${start}
		sleep 30s
		screen -S $screenName -X stuff "${commands}"
		echo Factorioserver gestartet
		screen -ls | grep ${screenName}
	;;
# STOP
	stop)
		screen -S $screenName -X stuff ^C
		sleep 30s
		screen -ls | grep ${screenName} | cut -d. -f1 | tr --delete "\t" | xargs kill -9; screen -wipe;
	;;
# RESET
	reset)
# server Stoppen
	screen -S $screenName -X stuff ^C
	sleep 30s
	screen -ls | grep ${screenName} | cut -d. -f1 | tr --delete "\t" | xargs kill -9; screen -wipe;
# altes Save löschen
	rm ${savegame}
# neues Save anlegen
	${pfad}bin/x64/factorio --create ${savegame}
# server starten
		${scr}
		screen -S $screenName -X screen ${update}
		screen -S $screenName -X screen ${start}
	;;
# MESSAGE
	message)
	screen -S $screenName -X stuff "$2 ^M"
	;;
# Check Update
	update)
	rm ${pfad}/update.h1
	mv ${pfad}/update.h ${pfad}/update.h1
	python /home/factorio/server/update_factorio.py -xa ${pfad}/bin/x64/factorio > ${pfad}/update.h
	updy=$(tail -n 1 ${pfad}/update.h | cut -c1-2)
	echo ${updy}
	if [ "${updy}" == "No" ]
	then
		echo No Updates availible 
	else
		screen -S $screenName -X stuff "the Server is going down for Updates in 15 Minutes. ^M"
		sleep 5m
		screen -S $screenName -X stuff "the Server is going down for Updates in 10 Minutes. ^M"
		sleep 5m
		screen -S $screenName -X stuff "the Server is going down for Updates in 5 Minutes. ^M"
		sleep 4m
		screen -S $screenName -X stuff "the Server is going down for Updates in 1 Minutes. ^M"
		sleep 30s
		screen -S $screenName -X stuff "the Server is going down for Updates in 30 Seconds. ^M"
		sleep 25s
		screen -S $screenName -X stuff "the Server is going down for Updates now. ^M"
		sleep 5s
		screen -S $screenName -X stuff ^C
 		sleep 30s
		screen -ls | grep ${screenName} | cut -d. -f1 | tr --delete "\t" | xargs kill -9; screen -wipe;
		echo screens
		echo ${screenName}
		sleep 1s
		${scr}
		python ${update} -xa ${pfad}/bin/x64/factorio
		wait
		screen -S $screenName -X screen ${start}
		sleep 30s
		screen -S $screenName -X stuff "${commands}"
	fi
	;;
# SONST
	*)
	echo "start/stop/reset/message/update"
	exit 1
	;;
esac
Mytronix Entertainment

robobenklein
Manual Inserter
Manual Inserter
Posts: 1
Joined: Wed May 18, 2016 4:44 pm

Re: Factorio Control Script (Nasty Linux Bash)

Post by robobenklein » Fri Dec 01, 2017 8:14 pm

Have you taken a look at LinuxGSM?

They have factorio server support now: https://gameservermanagers.com/lgsm/fctrserver/

I like the countdown, perhaps you could make a PR onto LinuxGSM to add some of the functionality of your script?

ChoMar
Long Handed Inserter
Long Handed Inserter
Posts: 88
Joined: Sun Aug 07, 2016 2:00 am

Re: Factorio Control Script (Nasty Linux Bash)

Post by ChoMar » Mon Dec 11, 2017 4:05 pm

The GSM uses Steam and I dont ;-)
Not saying what is better here (We also have Gameservers that use steam), but its a different approach.
I might look into LinuxGSM, but usually im too lazy to do anything unless something stops working.
Mytronix Entertainment

Post Reply

Return to “Tools”