Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@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.

like image 329
Jose Serodio Avatar asked Jul 19 '16 18:07

Jose Serodio


2 Answers

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
like image 199
Patrickz Avatar answered Oct 25 '22 15:10

Patrickz


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.

like image 29
billq Avatar answered Oct 25 '22 17:10

billq