• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Linux Restarter TFS on crash and reboot

Merrok

Magic Tomato
Joined
Jun 18, 2009
Messages
176
Solutions
7
Reaction score
209
Since there have been many questions on how to make a restarter, I thought I might just make a quick tutorial on how to write a service in linux.

A service always restarts itself when crashed and automatically starts when the server booted.

First make a script that automatically restarts tfs, here you can get creative (for example including PIDs, config and ini files, etc) but to keep it simple lets do it like this:

Bash:
#!/bin/bash
while true; do /path/to/tfs; sleep 1; done

Now to make this automatically execute, we will create a service
To do so, create a file called (for example) TFS in /etc/init.d
touch /etc/init.d/TFS
This file should contain the following code -- Modify the paths, script, user, etc so it fits your server!
Bash:
#!/bin/sh
### BEGIN INIT INFO
# Provides:          TFS
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       foo
### END INIT INFO

SCRIPT="screen -dmS tfs && screen -S tfs -X stuff '/path/to/your/restart/script\n'" #Important: dont delete \n
RUNAS=tfs

PIDFILE=/var/run/TFS.pid
LOGFILE=/var/log/TFS.log

start() {
  echo 'Starting service…' >&2
  local CMD="$SCRIPT > \"$LOGFILE\" & echo \$!"
  su -s "/bin/bash" -c "script /dev/null && $CMD" $RUNAS > "$PIDFILE"
  echo 'Service started' >&2
}

stop() {
  echo 'Stopping service…' >&2
  rm -f "$PIDFILE"
  su -s "/bin/bash" -c "script /dev/null && screen -S tfs -X quit" $RUNAS
  echo 'Service stopped' >&2
}

uninstall() {
  echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
  local SURE
  read SURE
  if [ "$SURE" = "yes" ]; then
    stop
    rm -f "$PIDFILE"
    echo "Notice: log file is not be removed: '$LOGFILE'" >&2
    update-rc.d -f TFS remove
    rm -fv "$0"
  fi
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  uninstall)
    uninstall
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|uninstall}"
esac

Make sure the user chosen has access to the PID and the LOG file.

Now to enable this you need to do the following:
sudo chmod +x /etc/init.d/TFS sudo update-rc.d TFS defaults sudo update-rc.d TFS enable sudo systemctl daemon-reload sudo service TFS start

You can now start/stop/restart/status/uninstall your server using
sudo service TFS start/stop/restart/status

If you uninstall the service, also do sudo systemctl daemon-reload

You can also see all logs in /var/log/TFS.log

I tested the script with a different process. Should work fine for TFS as well.
 
Last edited:
Hi,
Where do I exactly put the user in this script ? As far as i understand you are using account tfs so i made one and gave him permission to home folder
Also when I execute a command
update-rc.d TFS enable

I get this

Failed to enable unit: Unit /run/systemd/generator.late/TFS.service is transient or generated.

My configs:
SCRIPT="screen -dmS tfs && screen -S tfs -X stuff '/home/restarter\n'" #Important: dont delete \n

file in home:

#!/bin/bash
while true; do /ots/tfs; sleep 1; done

My ots location /home/ots/ tfs file is there
 
Last edited:
Back
Top