Jump to content

Need help with linux code/program

Khoomn

So I bought a VPS from a VPS provider today and i need to run a java program on that server, the program connects to a website and if even a single packet is lost on the VPS, the entire program shuts down and i have to manually restart it. Is there any way I could make a script that runs that pings the website and when there is no reply back from the ping it automatically restarts the program?

 

I tired out systemd and it didn't work. It launches the program and acts like normal, it just doesn't restart the program on failure. Are there any other programs or code that can restart on failure or code that can restart it on a time setting, like restart every 30 minutes?

Link to comment
Share on other sites

Link to post
Share on other sites

By "shuts down", do you mean it crashes back to the terminal? In that case running the program as a service (i.e. systemd) should solve your issue. That can be set to automatically restart your service if it crashes.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, Eigenvektor said:

By "shuts down", do you mean it crashes back to the terminal? In that case running the program as a service (i.e. systemd) should solve your issue. That can be set to automatically restart your service if it crashes.

So to run the program i do "java -jar program.jar" and it starts up the program, so once it "shuts down" it basically cancels that command and opens a new line for me to type on as if i pressed ctrl+c to stop it

 

Edit: I run the code in screen so i can close out of the terminal, its supposed to be running at all times

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, Khoomn said:

Edit: I run the code in screen so i can close out of the terminal, its supposed to be running at all times

So yes, the program is supposed to behave like a service. As I said, use systemd to run it in the background. Systemd can automatically restart a service if it fails.

 

https://computingforgeeks.com/how-to-run-java-jar-application-with-systemd-on-linux/

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Eigenvektor said:

So yes, the program is supposed to behave like a service. As I said, use systemd to run it in the background. Systemd can automatically restart a service if it fails.

 

https://computingforgeeks.com/how-to-run-java-jar-application-with-systemd-on-linux/

Thanks for that, ill look into it sometime tomorrow as it is late, ill get back to you if anything is wrong.

Link to comment
Share on other sites

Link to post
Share on other sites

22 hours ago, Eigenvektor said:

So yes, the program is supposed to behave like a service. As I said, use systemd to run it in the background. Systemd can automatically restart a service if it fails.

 

https://computingforgeeks.com/how-to-run-java-jar-application-with-systemd-on-linux/

Hey so I tired out systemd and it didn't work. It launches the program and acts like normal, it just doesn't restart the program on failure. Do you have any other programs or code that can restart on failure or code that can restart it on a time setting, like restart every 30 minutes?

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, Khoomn said:

Hey so I tired out systemd and it didn't work. It launches the program and acts like normal, it just doesn't restart the program on failure. Do you have any other programs or code that can restart on failure or code that can restart it on a time setting, like restart every 30 minutes?

You could try changing "Restart=on-failure" to "Restart=always". The first option will only restart the service, if it fails with an error code, which it might not do. The example also uses "RestartSec=10" so it'll wait 10 seconds before restarting the service. Note that systemd doesn't restart your service if you manually stop it, so to test it, you have to kill the process (e.g. kill -9 <pid>).

 

You can also configure systemd to restart your service periodically: How can I configure a systemd service to restart periodically?

 

But to be honest, if a service requires this... I'd rather fix the service. Where did this .jar file come from? Because a program that's supposed to do networking and crashes on network errors sounds buggy to me.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Eigenvektor said:

You could try changing "Restart=on-failure" to "Restart=always". The first option will only restart the service, if it fails with an error code, which it might not do. The example also uses "RestartSec=10" so it'll wait 10 seconds before restarting the service. Note that systemd doesn't restart your service if you manually stop it, so to test it, you have to kill the process (e.g. kill -9 <pid>).

 

You can also configure systemd to restart your service periodically: How can I configure a systemd service to restart periodically?

 

But to be honest, if a service requires this... I'd rather fix the service. Where did this .jar file come from? Because a program that's supposed to do networking and crashes on network errors sounds buggy to me.

I bought this program from someone i know is a good coder, i never had this problem on my PC or on NFO but it happens on this vps and ovh

 

Also i currently already have RestartSec=10 but what does Restart=always mean/do?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Khoomn said:

I bought this program from someone i know is a good coder, i never had this problem on my PC or on NFO but it happens on this vps and ovh

 

Also i currently already have RestartSec=10 but what does Restart=always mean/do?

If the program can write log files, you may want to make them available to the developer you bought it from, maybe they can determine the cause and fix it.

 

As I said above, "on-error" means "restart if the program quits with an error code" and "always" means "restart if the program quits for any reason". The only time the service isn't restarted by systemd is if you explicitly stop it with "systemctl stop <my>.service".

 

You may want to take a look at the systemd man page: https://www.freedesktop.org/software/systemd/man/systemd.service.html

Search for "Restart="

Remember to either quote or @mention others, so they are notified of your reply

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

×