Introduction

Sometimes, you really need those server-wide program to run in background, because you need to avoid some accidents to interrupt our service program.

There are some methods I will introduce here:

1. tmux:

  1. tmux new -s [tty-name]: start a new tty window with your set name

  2. tmux attach -t [tty-name]: attach a existing tmux tty with name

  3. crtl + b and then d: exit tmux tty but keep it in background

LATER but this command or we call it shortcut sometimes don’t work, if I find the reason, I will update here.

2. nohup:

  1. nohup ./my_script.sh &

  2. nohup ./my_script.sh > output.log 2>&1 &

3. systemctl(Systemd)

TODO: MacOS: launchctl(launchd)

I recommend you use systemctl, but that is not absolute, different methods have different using scenario. Because we can easily control services via simple commands, and all the journal will be set at journalctl. But if you don’t have root authority, you should give up this method.

Make sure your basic service command is workable.

1
./bin/x64/factorio --start-server ./saves/SA_Multi.zip

Register this command to systemctl

1
2
3
4
# /etc/systemd/system
# This folder contains all the services that can be managed by systemctl
cd /etc/systemd/system
ls

Make our own service unit:

1
touch factorio.service

and put this example into this file:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=Factorio Server
After=network.target

[Service]
Type=simple
User=root
#WorkingDirectory=/opt/factorio/ # this is your program working directioy
ExecStart=/opt/factorio/bin/x64/factorio --start-server /opt/factorio/saves/SA_Multi.zip
Restart=on-failure

[Install]
WantedBy=multi-user.target

Reload systemctl daemon:

1
systemctl daemon-reload

Start our service:

1
2
3
4
systemctl start factorio
# If you would like set your service startup automatically when OS boot.
# $ systemctl enable factorio
systemctl status factorio

Review our journal: journalctl

1
2
3
journalctl -u factorio.service -f
# -u: unit name
# -f follow