Issue
I’m trying to run a shell-script and a command at the start of my Ubuntu server.
Here is my CRON
@reboot /home/steam/check.sh
@reboot screen -d -S up -m node /var/www/html/Up1/server/server.js
What I’m getting in the logs:
grep CRON /var/log/syslog
Jul 19 19:48:28 vc1s cron[3185]: (CRON) INFO (pidfile fd = 3)
Jul 19 19:48:28 vc1s cron[3185]: (CRON) INFO (Running @reboot jobs)
Jul 19 19:48:28 vc1s CRON[3209]: (root) CMD (screen -d -S up -m node /var/www/html/Up1/server/server.js)
Jul 19 19:48:28 vc1s CRON[3211]: (root) CMD (/home/steam/check.sh)
Jul 19 19:51:20 vc1s cron[3779]: (CRON) DEATH (can't lock /var/run/crond.pid, otherpid may be 3185: Resource temporarily unavailable)
Jul 19 19:55:01 vc1s CRON[3996]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
My check.sh
.
#!/bin/bash
until screen -d -S unturned -m /home/steam/start.sh; do
echo "Server 'myserver' crashed with exit code $?. Respawning.." >&2
sleep 1
done
My start.sh
. It’s for launching an Unturned game server. I don’t think this script is important but I guess I should show you.
#!/bin/bash
# This script starts a Unturned 3 server on Linux machines
# Syntax: start.sh <instance name>
# Author: fr34kyn01535
#CONFIG
INSTANCE_NAME=1
STEAMCMD_HOME="./steamcmd"
UNTURNED_HOME="./unturned"
#COLORS
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLLOW='\033[0;33m'
NC='\033[0m'
#Steam checks
STEAMCMD_API=$STEAMCMD_HOME/linux32/steamclient.so
UNTURNED_API=$UNTURNED_HOME/Unturned_Data/Plugins/x86/steamclient.so
printf "Steam: "
if [ -f $STEAMCMD_API ]; then
if diff $STEAMCMD_API $UNTURNED_API >/dev/null ; then
printf "${GREEN}UP TO DATE${NC}\n\n"
else
cp $STEAMCMD_API $UNTURNED_API
printf "${YELLLOW}UPDATING${NC}\n\n"
fi
else
printf "${RED}NOT FOUND${NC}\n\n"
fi
cd $UNTURNED_HOME
if [ -f RocketLauncher.exe ]; then
ulimit -n 2048
mono RocketLauncher.exe $INSTANCE_NAME
else
echo "RocketLauncher not found."
fi
The thing is that if I execute ./check.sh
from /home/steam
It works fine. The bad news is that the @reboot
doesn’t work for me when I reboot my VPS.
screen -list
doesn’t throw anything if I reboot.
I’ve tried multiple things but didn’t work, the last thing I changed was adding -d
parameter in the screen
commands so the server wouldn’t need a terminal
to write down the start-up.
I’m not sure how much can be done here to make @reboot
work as expected.
How can I make my scripts run on boot? Are there any other alternatives to CRON
‘s @reboot
?
Thanks in advance.
Solution
Take a look at the systemd.service manpage. It describes how to configure systemd to manage a service. I am sure you will find examples for your system in /usr/lib/systemd/system
or similar paths.
In your case, the service would look somewhat like this:
[Unit]
Description=Unturned Game Server
[Install]
WantedBy=multi-user.target
[Service]
ExecStart=/bin/bash /home/steam/start.sh
Type=simple
User=steam
Group=steam
WorkingDirectory=/home/steam
Restart=on-failure
Put this in a file /etc/systemd/system/unturned.service
. Then run systemctl daemon-reload
(once, and whenever you change unturned.service
to tell systemd to re-read the configuration) and systemctl start unturned.service
to start the game server.
If that works as expected, you can use systemctl enable unturned.service
to make sure it starts at boot.
A few notes on the options used:
- If start.sh is not supposed to run as user/group
steam
, edit appropriately. WantedBy
in theInstall
section tells systemd which “target” (see man systemd.target) pulls the service in when you enable it using systemctl enable.Restart
defines under which circumstances systemd will automatically restart the service. There are more restart-related options, which you may or may not want to change; see the systemd.service man page.
Answered By – Jonas Schäfer
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0