0

I have recently set up my game server on a linux OS and am using SSH to run the java file which runs the game server. In order for me to be able to exit the SSH session but keep the server running, I am using nohup and creating a script which does this for me:

#!/bin/bash
cd "RSPS"
echo "Booting the RSPS..."
nohup java -Xmx220m -cp bin:lib/* com.ruseps.GameServer &

I can see the status of my server then at any time by using the tail command like so:

#!/bin/bash
cd "RSPS"
tail -f nohup.out

However, my server has shutdown hooks which expects the java file to be exited therefor running top and then killing the PID of the service ignored the shutdown hook thus breaking the 'save' state and making my server unbootable.

Is there a way I can reopen the process and softly kill the service, ie like exiting it so the file can run the shutdown hook?

What I am currently doing, which is not running the shutdown hook is using this via command line:

top
kill PID_HERE
Jaquarh
  • 6,493
  • 7
  • 34
  • 86

1 Answers1

3

In order for me to be able to exit the SSH session but keep the server running, I am using nohup and creating a script which does this for me:

First of all: Don't do that!

Implement a proper service. If the machine this is running on uses systemd all you have to do is writing a unit file https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-managing_services_with_systemd-unit_files

If your machine uses an init-script style init system, copy the init script of some similar network service, like a webserver and modify it for your needs.

Keep in mind that you should create a new user/groups for your server to run as and on startup have it check that it's not running as root (because that's always a bad idea).

However, my server has shutdown hooks

How are these hooks implemented? How does your server is notified to shut down? The SIGTERM signal (which is the default signal sent by kill, despite its name) is the standard way of informing processes, that they shall terminate gracefully. See the official Java documentation for details: https://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/signals.html

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Thanks for the information! My server is a raspberry pi, I just port forwarded ssh and it is only me with access! My shutdown hooks are attached to my Java code - I could call `System.exit()` and allow the game client to send the command to do so if they have the correct rights but that seems a lot of work for a simple task. I'll deffo look into using proper services! Thanks – Jaquarh Mar 09 '19 at 13:54
  • @Jaquarh: It doesn't matter that it's a R-Pi. What matter is, that you have a long running, network facing service, and that these things should be managed by the part of the system that has been put in place to exactly this kind of thing. Running it through the init system will give you proper logging, automatic respawn in case of crash, process monitoring, cgrouping, etc. All things you want to have. Also detaching from the controlling terminal (aka nohup) is a really bad thing to do, with respect to process management. Save yourself some trouble, say no to nohup. – datenwolf Mar 09 '19 at 14:18
  • @Jaquarh: "attached to your Java code…" and where can I read that? We *must* be able to read your code to give you programming advice. – datenwolf Mar 09 '19 at 14:19