Jump to content

Linux mint 20 bash script to auto restart java executable

Hi I'm running a minecraft server .jar on Linux mint 20. I need help making a bash script that will automatically restart the process if it is closed or crashes (there are somewhat frequent crashes.) I'm currently just using the java -jar command with a few arguments in my current bash script for running the server. It's very annoying to have to manually restart the server every time it crashes, especially if my friends are trying to play and I'm not available to re start the server. Thank you for reading! 

Nude Fist 1: i5-4590-ASRock h97 Anniversary-16gb Samsung 1333mhz-MSI GTX 970-Corsair 300r-Seagate HDD(s)-EVGA SuperNOVA 750b2

Name comes from anagramed sticker for "TUF Inside" (A sticker that came with my original ASUS motherboard)

Link to comment
Share on other sites

Link to post
Share on other sites

Cannot set it as service via systemd? Don't quote me if it didn't work. 😆

 

Quote

Simple Setup

Note: Not for users of BungeeCord. Single server setups only!

  1. Place the minecraft.service file in your /etc/systemd/system/ directory
  2. Create a user called minecraft with its home pointed to /opt/minecraft
  3. Customize the file according to your requirements as documented within the file
  4. Install the vanilla server or Spigot in /opt/minecraft
  5. (Optional) Enable the service using systemctl enable minecraft
  6. Start the service using systemctl start minecraft

While the step of enabling the service is optional, you should do so to automatically start the server when the machine reboots.

 

Template: https://gist.githubusercontent.com/dotStart/ea0455714a0942474635/raw/aaa2d688e85e0a75a995f7f66f1e44c073736ec4/minecraft.service

Source: https://gist.github.com/dotStart/ea0455714a0942474635

| Intel i7-3770@4.2Ghz | Asus Z77-V | Zotac 980 Ti Amp! Omega | DDR3 1800mhz 4GB x4 | 300GB Intel DC S3500 SSD | 512GB Plextor M5 Pro | 2x 1TB WD Blue HDD |
 | Enermax NAXN82+ 650W 80Plus Bronze | Fiio E07K | Grado SR80i | Cooler Master XB HAF EVO | Logitech G27 | Logitech G600 | CM Storm Quickfire TK | DualShock 4 |

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, xAcid9 said:

If I create a service for it will it automatically restart itself any time the java program exits? I have seen this idea when I was looking online but I'm not fully aware of how systemd services work

Nude Fist 1: i5-4590-ASRock h97 Anniversary-16gb Samsung 1333mhz-MSI GTX 970-Corsair 300r-Seagate HDD(s)-EVGA SuperNOVA 750b2

Name comes from anagramed sticker for "TUF Inside" (A sticker that came with my original ASUS motherboard)

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, itachipirate said:

If I create a service for it will it automatically restart itself any time the java program exits? I have seen this idea when I was looking online but I'm not fully aware of how systemd services work

https://ma.ttias.be/auto-restart-crashed-service-systemd/ - Look at the guide here about that, it's pretty simple.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, itachipirate said:

If I create a service for it will it automatically restart itself any time the java program exits? I have seen this idea when I was looking online but I'm not fully aware of how systemd services work

Yes, check the template comment.

# You can customize the maximum amount of memory as well as the JVM flags here
ExecStart=/usr/bin/java -XX:+UseG1GC -Xmx3G -jar server.jar --nojline --noconsole

# Restart the server when it is stopped or crashed after 30 seconds
# Comment out RestartSec if you want to restart immediately
Restart=always
RestartSec=30

It's work similar to Service or Task Scheduler in Windows. 

| Intel i7-3770@4.2Ghz | Asus Z77-V | Zotac 980 Ti Amp! Omega | DDR3 1800mhz 4GB x4 | 300GB Intel DC S3500 SSD | 512GB Plextor M5 Pro | 2x 1TB WD Blue HDD |
 | Enermax NAXN82+ 650W 80Plus Bronze | Fiio E07K | Grado SR80i | Cooler Master XB HAF EVO | Logitech G27 | Logitech G600 | CM Storm Quickfire TK | DualShock 4 |

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/9/2021 at 3:37 AM, Alexeygridnev1993 said:

Thank you, it's great to actually understand how systemd works. I was unaware this was possible, as before i was just copy/pasting the info for .service files when I was setting up services. This would likely work perfectly for me, but I'm unsure how I'd be able to view the running terminal output of the service.

 

How I ended up doing it was with a shell script using a while loop. I put a 10 second pause in so I can easily stop the server without it instantly starting again incase I want to close the terminal window to stop the server from looping its restart. I manually turn on the autorestart script once after boot and it restarts. I would've used and until loop but the infrequent server crash im getting exits with code 0, so the until loop would assume the program was intentionally stopped. A while loop continues forever until you close the terminal window (close the autorestart bash script.)

 

I suppose it would be even easier with ssh to use yours and @xAcid9's suggestion of using a service. I could just systemctl start/stop in ssh if i need to put the server down for something. I really would need a way of seeing what the server normally outputs to the terminal window though. Is there a way to see that using a service? systemctl status wouldn't happen to show the terminal output for the program, would it? I would definitely use a service if I can easily ssh into and out of the terminal outputs for the server. I'd have no need to really VNC into the server, I could do everything from ssh

 

Thank you guys for the help

Nude Fist 1: i5-4590-ASRock h97 Anniversary-16gb Samsung 1333mhz-MSI GTX 970-Corsair 300r-Seagate HDD(s)-EVGA SuperNOVA 750b2

Name comes from anagramed sticker for "TUF Inside" (A sticker that came with my original ASUS motherboard)

Link to comment
Share on other sites

Link to post
Share on other sites

59 minutes ago, itachipirate said:

I really would need a way of seeing what the server normally outputs to the terminal window though. Is there a way to see that using a service?

Journald is a part of systemd which is constantly logging everything that happens to your services. Google for journald to learn the details.

 

Systemd was created precisely to solve the problems you are talking about, without the need for ad-hoc solutions like yours (nothing wrong with your solution though; if it does the job for you, then it's fine).

 

Don't forget that you can also always redirect stdout and stderr from any command to a file with just "your command > log.txt".

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×