Jose Serodio
Jose Serodio

Reputation: 1449

@reboot is not working in CRON

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.

Upvotes: 19

Views: 79179

Answers (4)

billq
billq

Reputation: 111

Check the critical-chain for crond.service, as asked and answered in This StackExchange post

Also, reference this FreeDesktop article addressing this issue.

In general, systemd is configured to have very limited dependencies, starting many daemons in parallel to reduce time spent booting. Services that are not necessarily dependent on the network being fully up and functional will fail if there are components that assume the network is in a stable state. Failures such as this may be difficult to diagnose, but using systemd-analyze critical-chain and journalctl -xel output can lead you to the root cause of your issue.

Upvotes: 1

Marzycielx
Marzycielx

Reputation: 21

in my case I had a script in /usr/local/bin and tried to do @reboot namescript it was enough to enter the full path @reboot /usr/local/bin/namescript

Upvotes: 0

Patrickz
Patrickz

Reputation: 760

Try man 5 crontab. If your crontab supported, you should see @reboot, @yearly, @monthly,.,,,

then try add some sleep for moment may can help.

@reboot sleep 60;/root/s3-mount.sh

Upvotes: 36

Jonas Sch&#228;fer
Jonas Sch&#228;fer

Reputation: 20738

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 the Install 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.

Upvotes: 12

Related Questions