0

I have been having troubles setting up an auto reboot for my mc server.

My .sh file I have made works perfectly whenever I execute it, but when it comes to using crontab it seems to just do nothing (excluding one time), I have tried restarting & reinstalling it... it is safe to say I am out of my element as I am completely new to all of this.

The crontab line is this

0 20 * * * /VHServer/reboot.sh

nothing has changed since I made that line of code, it worked the first time and then never again.

The .sh file looks like this and it runs perfectly with a manual command.


echo "Server reboot STARTED"

Letting people know that the server is going to restart

tmux send-keys -t VHS "/say §c Server restarting in §r§6§l30 minutes. §r§cPleas>

Wait for 1770 seconds (25 minutes)

sleep 1500s

Send messages to the server indicating the restart countdown

tmux send-keys -t VHS "/say §c Server is restarting in §r§6§l5 minutes! §r§cThe> sleep 270s

tmux send-keys -t VHS "/say §c Server is restarting in §r§6§l30 seconds! §r§cTh> sleep 20s

tmux send-keys -t VHS "/say §c Server is restarting in §r§6§l10 seconds!" Enter sleep 5s

tmux send-keys -t VHS "/say §c Server is restarting in §r§6§l5 seconds!" Enter sleep 1s

tmux send-keys -t VHS "/say §c Server is restarting in §r§6§l4 seconds!" Enter sleep 1s

tmux send-keys -t VHS "/say §c Server is restarting in §r§6§l3 seconds!" Enter sleep 1s

tmux send-keys -t VHS "/say §c Server is restarting in §r§6§l2 seconds!" Enter sleep 1s

tmux send-keys -t VHS "/say §c Server is restarting in §r§6§l1 second!" Enter sleep 1s

Stop the Minecraft server

tmux send-keys -t VHS "/stop" Enter sleep 60s

Detatch from the tmux session

tmux send-keys VHS C-b d sleep 15s

Check if the session exists and kills session

if tmux has-session -t VHS> /dev/null; then echo "Killing tmux session 'VHS'." tmux kill-session -t VHS else echo "Tmux session 'VHS' does not exist." fi sleep 15s

Check if a session with the same name already exists

if tmux has-session -t VHS> /dev/null; then echo "Tmux session 'VHS' already exists. Attaching to it." tmux attach-session -t VHS else echo "Creating new tmux session 'VHS'." tmux new-session -s VHS fi sleep 30s

Restart the server using the run.sh script

tmux send-keys -t VHS "/home/ubuntu/VHServer/run.sh" Enter

echo "Server Rebooting" sleep 60s

echo "reboot COMPLETE"

I have had suggestions to run the crontab using sudo and also adding either sudo or the username "ubuntu" to the line of code within it, I have tried and there has been no output whatsoever.

I ran:

tail -f /var/log/syslog

and this only came back with the edits of the crontab, not once did the cron execute any command.

I hope this is enough information to provide a fix, I am at a complete loss and still learning about all of this, so I am sure I am not understanding something within all of this.

  • cron will discard any output unless you redirect the output of each command like echo "Server Rebooting" &>> /home/ubuntu/logfile or the whole script in crontab like 0 20 * * * /VHServer/reboot.sh &>> /home/ubuntu/logfile ... See also https://askubuntu.com/a/1448126 and https://askubuntu.com/a/1452020 – Raffa Jun 11 '23 at 09:59
  • ... unless your crontab explicitly sets SHELL=/bin/bash, use /VHServer/reboot.sh >> /home/username/logfile 2>&1 though - the default is to run crontab jobs in /bin/sh which won't recognize &>> as stdout+stderr redirection – steeldriver Jun 11 '23 at 12:26

1 Answers1

3

Your script should begin with:

#!/bin/bash

Replace tmux with the absolute path (type -p tmux). cron jobs have a different runtime environment, different $PATH, no $DISPLAY, etc. Read man -a crontab.

waltinator
  • 36,399
  • the script does begin with #!/bin/bash I failed to copy that in sorry!

    I have done as explained, changed all the "tmux" commands to the path type -p tmux provided which looked like this: /usr/bin/tmux"

    The crontab seems to still have no output.

    – PerplexApple Jun 11 '23 at 08:15