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:
-
tmux new -s [tty-name]: start a new tty window with your set name -
tmux attach -t [tty-name]: attach a existing tmux tty with name -
crtl + band thend: 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:
-
nohup ./my_script.sh & -
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.
./bin/x64/factorio --start-server ./saves/SA_Multi.zip
Register this command to systemctl
# /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:
touch factorio.service
and put this example into this file:
[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:
systemctl daemon-reload
Start our service:
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
journalctl -u factorio.service -f
# -u: unit name
# -f follow