Friday, September 2, 2022

[SOLVED] How to Create Bash Restart Script With Input

Issue

I want to create a restart script in bash so that when the process ends the process will be restarted.

I did it like this:

#!/bin/bash
while true
do
java -XX:+UseG1GC -Xmx512M -jar Server.jar -o true
sleep 1
done

So far so good. However, when the server ends I want to ask for an input (Y or N) if the server should even restart and if there is none within a few seconds the server should automatically restart, else the script should end.

Nevertheless, I am clueless how I should code this part.

Thanks for your advice.


Solution

Try with this:

#!/bin/bash

while true;do
  java -XX:+UseG1GC -Xmx512M -jar Server.jar -o true

  read -p "Do you wish to run it again (y/N)? " QUESTION
  
  case "${QUESTION}" in
    [Yy] ) 
      echo "Restarting process..."      
      ;;

    * ) 
      echo "Bye!"
      exit
      ;;
  esac
done


Answered By - Antonio Petricca
Answer Checked By - Cary Denson (WPSolving Admin)