A
A
Alex Raven2016-09-01 20:53:17
linux
Alex Raven, 2016-09-01 20:53:17

How to run a cron script?

Maybe I'm stupid. But I can't solve one problem. I made a Minecraft server for my son. The server, in principle, works stably, but my son (12 years old) likes to upload all sorts of "left" plugins and worlds. After that the server often crashes. I made a service script that starts, stops and restarts the server from /etc/init.d/minecraftpe:

/etc/init.d/minecraftpe start
/etc/init.d/minecraftpe stop
/etc/init.d/minecraftpe restart

Also, I made a script called /home/arik/pocketmine-new/keeprunning.sh:
#!/bin/bash
d=$(date +"%d/%m/%Y, %T")
/etc/init.d/minecraftpe status
if [ $? == 0 ]; then
    /etc/init.d/minecraftpe start
    echo "[ $d ] Restarting Minecraft" >> /var/log/minecraftpe.log
    /etc/init.d/minecraftpe status
    if [ $? == 0 ]; then
        echo "[ $d ] Could not restart the server" >> /var/log/minecraftpe.log
    else
        echo "[ $d ] Server was restarted successfully" >> /var/log/minecraftpe.log
    fi
else
    echo "[ $d ] Running" >> /var/log/minecraftpe.log
fi

And added it to cron (crontab -e under root):
*/1 * * * * root /home/arik/pocketmine-new/keeprunning.sh

Accordingly, once a minute it should check if the server is running, and if it has fallen, then restart it. In fact, the script writes to the log:
[ 29/08/2016, 20:13:01 ] Restarting Minecraft
[ 29/08/2016, 20:13:01 ] Could not restart the server

And if the same script /home/arik/pocketmine-new/keeprunning.sh is run from the console (under root), then the server restarts normally. That is, the script itself runs, but cannot restart the server. The rights to this script are 755 (although, in theory, to run under root, the main thing is that only +x be). How can you deal with this bug? I have been jumping with a tambourine for more than one day. At the same time, other cron scripts run normally.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex Raven, 2016-09-02
@alexraven

The question is closed. It was necessary to set absolute paths to programs. Specifically, /sbin/runuser instead of runuser.

A
Alexander Shurakov, 2016-09-02
@shurale43

If the script behaves differently when launched from the console and from under cron, it is most likely due to environment variables - PATH, etc.
Try setting the PATH in the script or, more radically, add the line at the beginning
. ~/.profile

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question