There is a simple java server on sockets. I run it via ssh on the ubuntu server, on which there is only jre, everything is fine, it works. Well, as I close the ssh session, my java server is closed. What needs to be done to ensure that the application server is constantly running? And in the future, how to implement a server status view, for example, the number of connected clients? In what way are such things done?

  • A good application should be able to be demonized, i.e. go into background execution. The c-shny programs for this are untied from the controlling terminal, like java xs. if an approximation does not know how, then launch it using the sreen program that the virtual terminal creates (to which you can connect). Well, on the second question - either put the info in the file, or give it to the connected client by special request - Mike

1 answer 1

You need to create a wraper script to run your application as a service. The script is placed in the /etc/init.d/ folder. Sample script:

#!/bin/sh SERVICE_NAME=MyService PATH_TO_JAR=/usr/local/MyProject/MyJar.jar PID_PATH_NAME=/tmp/MyService-pid case $1 in start) echo "Starting $SERVICE_NAME ..." if [ ! -f $PID_PATH_NAME ]; then nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null & echo $! > $PID_PATH_NAME echo "$SERVICE_NAME started ..." else echo "$SERVICE_NAME is already running ..." fi ;; stop) if [ -f $PID_PATH_NAME ]; then PID=$(cat $PID_PATH_NAME); echo "$SERVICE_NAME stoping ..." kill $PID; echo "$SERVICE_NAME stopped ..." rm $PID_PATH_NAME else echo "$SERVICE_NAME is not running ..." fi ;; restart) if [ -f $PID_PATH_NAME ]; then PID=$(cat $PID_PATH_NAME); echo "$SERVICE_NAME stopping ..."; kill $PID; echo "$SERVICE_NAME stopped ..."; rm $PID_PATH_NAME echo "$SERVICE_NAME starting ..." nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null & echo $! > $PID_PATH_NAME echo "$SERVICE_NAME started ..." else echo "$SERVICE_NAME is not running ..." fi ;; esac 

Be sure to give the script permissions to execute:

 sudo chmod +x /etc/init.d/mytestserv 

Then the following commands are available to you:

 sudo service mytestserv start sudo service mytestserv stop sudo service mytestserv restart 

Adding to autoload:

 sudo update-rc.d mytestserv defaults 

More details can be found here.