@reboot is not working in CRON

37,082

Solution 1

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.

Solution 2

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
Share:
37,082
Jose Serodio
Author by

Jose Serodio

Academic Qualifications Certificate of Higher Education (HNC) in Computer System Administrator. Certificate of Higher Education (HNC) in Computer Applications Development. Currently pursuing a Engineer’s Degree in Computer Systems and Management.

Updated on November 30, 2021

Comments

  • Jose Serodio
    Jose Serodio over 2 years

    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.

  • Jose Serodio
    Jose Serodio almost 8 years
    Looking nice, I'll check this tomorrow as soon as I can, and if it works check the answer as valid. Thanks.
  • Jose Serodio
    Jose Serodio almost 8 years
    It worked for all my scripts ❤, I'm using Ubuntu Server 14.04
  • Jose Serodio
    Jose Serodio over 7 years
    If more people are wondering on how to delete a service. superuser.com/questions/513159/…
  • João Lourenço
    João Lourenço about 3 years
    This did the trick for me, thank you for this simple solution