Run the command in the background

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:
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 + 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.
nohup:
nohup ./my_script.sh &
nohup ./my_script.sh > output.log 2>&1 &
systemctl

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

image.png
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

image.png
Review our journal: journalctl

journalctl -u factorio.service -f
# -u: unit name
# -f follow
image.png

Leave a Reply

Your email address will not be published. Required fields are marked *