shell script not running screen session with cron tab
0
votes
0
answers
270
views
I'm running a Minecraft Bedrock server for my kids on an Almalinux VPS. It's not exactly supported but it runs. It shuts down about twice a day. I'm trying to automate the restarting of the server. I have a cron job that runs every minute, which is intended to restart the server. The command line to restart works if I execute it from terminal, but not from crontab. The echo message gets delivered to me by email, so the script itself is executing. I've tried several iterations of the screen command, including writing a log file, which doesn't get created.
My crontab entry:
* * * * * root /bin/bash /home/mcserver/minecraft_bedrock/restart.sh
My file, restart.sh:
#!/bin/bash
ps_out=
ps -ef | grep bedrock_server | grep -v 'grep' | grep -v $0
result=$(echo $ps_out | grep bedrock_server)
if [[ "$result" == "" ]]; then
echo "attempting restart"
screen -d -m /home/mcserver/minecraft_bedrock/bedrock_server
fi
I appreciate any guidance in the right direction!
EDIT: After trying some more things, I was able to get my script to work. I'm not going to answer the question myself, because I think there are some things I don't understand, and I'd like to award the answer to the user who explains how to use screen, or why it's not working. In the code below (working), I've commented out what I tried that did not work (using screen and tmux). I made some traction just trying to execute the startup file, ./bedrock_server, but I previously got errors telling me I needed to use a terminal, which is what led me to use screen to begin with. Based on my latest errors, I determined that I needed to change directory to the absolute directory, then execute ./bedrock_server. Below is the working code:
#!/bin/bash
ps_out=ps -ef | grep bedrock_server | grep -v 'grep' | grep -v $0
result=$(echo $ps_out | grep bedrock_server)
if [[ "$result" == "" ]]; then
# screen -d -m /home/mcserver/minecraft_bedrock/bedrock_server
#tmux new-session -d -s mc_session '/home/mcserver/minecraft_bedrock/bedrock_server'
cd /home/mcserver/minecraft_bedrock/
./bedrock_server
fi
Asked by Jason
(111 rep)
Feb 8, 2023, 01:38 AM
Last activity: Feb 8, 2023, 03:29 AM
Last activity: Feb 8, 2023, 03:29 AM