Hi I am running a Minecraft server with a restart plugin but it requires the program to restart itself, so I used a While true sleep 5 loop, and it seems to work fine but after some time it just starts opening again and again and there by it at some point gets to use 100% cpu usage all the time.
How do I make it not start the program again before the server closes or crashes?
#!/bin/sh
while true
do
java -Xms1G -Xmx6G -jar server.jar
sleep 5
done
Since sleep is set to 5 after it keeps opening again and again I see that is says failed to start the minecraft server every 5 secs but it is already running, and I want it to only run the while loop if the program is not running.
Using pgrep with the -f
flag you can search for a running process with some arguments. You can use this to check if a server is already running.
For example:
#!/bin/bash
while true
do
if ! pgrep -f server.jar
then
java -Xms1G -Xmx6G -jar server.jar
fi
sleep 5
done