Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a Java Application as a Service on Linux

I have written a Java server application that runs on a standard virtual hosted Linux solution. The application runs all the time listening for socket connections and creating new handlers for them. It is a server side implementation to a client-server application.

The way I start it is by including it in the start up rc.local script of the server. However once started I do not know how to access it to stop it and if I want to install an update, so I have to restart the server in order to restart the application.

On a windows PC, for this type of application I might create a windows service and then I can stop and start it as I want. Is there anything like that on a Linux box so that if I start this application I can stop it and restart it without doing a complete restart of the server.

My application is called WebServer.exe. It is started on server startup by including it in my rc.local as such:

java -jar /var/www/vhosts/myweb.com/phpserv/WebServer.jar & 

I am a bit of a noob at Linux so any example would be appreciated with any posts. However I do have SSH, and full FTP access to the box to install any updates as well as access to a Plesk panel.

like image 638
dreza Avatar asked Jun 26 '12 08:06

dreza


People also ask

Can we run java program in Linux?

Java is the world's popular software development platform that James Gosling develops. It is designed to support multiple platforms like Linux, macOS and Windows. Mobile and Desktop applications can also be developed using Java language.


2 Answers

I wrote another simple wrapper here:

#!/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  

You can follow a full tutorial for init.d here and for systemd (ubuntu 16+) here

If you need the output log replace the 2

nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null & 

lines for

nohup java -jar $PATH_TO_JAR >> myService.out 2>&1& 
like image 178
PbxMan Avatar answered Sep 19 '22 23:09

PbxMan


A simple solution is to create a script start.sh that runs Java through nohup and then stores the PID to a file:

nohup java -jar myapplication.jar > log.txt 2> errors.txt < /dev/null & PID=$! echo $PID > pid.txt 

Then your stop script stop.sh would read the PID from the file and kill the application:

PID=$(cat pid.txt) kill $PID 

Of course I've left out some details, like checking whether the process exists and removing pid.txt if you're done.

like image 30
Wernsey Avatar answered Sep 22 '22 23:09

Wernsey