Create A Service to Run Jar File In Background As System Service on AWS EC2 – Devstringx

Back to Blog
AWS ec2

Create A Service to Run Jar File In Background As System Service on AWS EC2 – Devstringx

Prerequisite:

  1. Java should already have
  2. Open and connect AWS ec2 instance using terminal
  3. Reach out root folder where the domain is pointing out for me it’s /var/www/HTML/
  4. Take pull the latest Spring Boot code from git https://bitbucket.org/***
  5. MVN clean install – this will convert java code with all its dependencies in a jar file
    (SUDO) java -jar target/YOUR_FILE_NAME.jar (target is a folder on root)
  • Problem: We found a challenge while running java -jar *.jar on the AWS ec2 instance in the background
  • Solution:  Create a system service to run the Jar file in the background Linux

Ubuntu/Linux has an in-built mechanism to create custom services and enable them to start on system boot time and start/stop them as a service.

Create a bash script to run jar file Linux:

Replace the customservicename with your own service name

  1. Create a vi or nano file under /usr/local/bin/ by running the following commandeg: sudo vi /usr/local/bin/customservicename. This will open a file with the name customservicename.sh.
  1. Paste the following Code in opened sh.
    In the below code replace the customservicename with your own service name,
    JAR_FILE_PATH(Absolute Path to your jar File), and choose a PID_NAME_PATH(just replace CUSTOM_SERVICE_NAME with your customservicename keeping -PID at the end ) for the file you are going to use to store your service ID.

The only changes needed are to the first 3 variables.

#!/bin/sh
CUSTOM_SERVICE_NAME=customservicename
JAR_FILE_PATH=/var/www/html/target/YourJavaApplication.jar
PID_NAME_PATH=/tmp/customservicename-pid
case $1 in
start)
   echo "Starting $CUSTOM_SERVICE_NAME ..."
   if [ ! -f $PID_NAME_PATH ]; then
      nohup java -jar $JAR_FILE_PATH /tmp 2>> /dev/null >>/dev/null & echo $! > $PID_NAME_PATH
      echo "$CUSTOM_SERVICE_NAME started ..."
   else
      echo "$CUSTOM_SERVICE_NAME is already running ..."
   fi
   ;;
stop)

if [ -f $PID_NAME_PATH ]; then
   PID=$(cat $PID_NAME_PATH);
      echo "$CUSTOM_SERVICE_NAME stopping ..."
   kill $PID;
   echo "$CUSTOM_SERVICE_NAME stopped ..."
   rm $PID_NAME_PATH
else
   echo "$CUSTOM_SERVICE_NAME is not running ..."
fi
;;

restart)
if [ -f $PID_NAME_PATH ]; then
   PID=$(cat $PID_NAME_PATH);
   echo "$CUSTOM_SERVICE_NAME stopping ...";
   kill $PID;
   echo "$CUSTOM_SERVICE_NAME stopped ...";
   rm $PID_NAME_PATH
   echo "$CUSTOM_SERVICE_NAME starting ..."
   nohup java -jar $JAR_FILE_PATH /tmp 2>> /dev/null >> /dev/null & echo $! > $PID_NAME_PATH
   echo "$CUSTOM_SERVICE_NAME started ..."
else
   echo "$CUSTOM_SERVICE_NAME is not running ..."
fi
;;
esac
    1. Write and quit the above file by pressing the ESC button then:wq! and give execution permissions :eg: sudo chmod +x /usr/local/bin/customservicename.sh

Read Also:- Setting Up Jenkins on Amazon EC2 Ubuntu Instance


Use The Following Commands to Test Whether Java Application Is Working:

     /usr/local/bin/./customservicename.sh start      
     /usr/local/bin/./customservicename.sh stop      
     /usr/local/bin/./customservicename.sh start      
     /usr/local/bin/./customservicename.sh restart

If all the above commands do not give an error then we move forward to create a service

  1. Create a file under /etc/system/system/ with nano or vi and paste the example script below.
         Eg. sudo vi /etc/systemd/system/customservicename.service
  2. Insert the following code in customservicename:
    [Unit]    
           Description = My Java Service    
           After network.target = customservicename.service 
    
    [Service]     
          Type = forking     
          Restart=always       
          RestartSec=1     
          SuccessExitStatus=143     
          ExecStart = /usr/local/bin/customservicename.sh start     
          ExecStop = /usr/local/bin/customservicename.sh stop     
          ExecReload = /usr/local/bin/customservicename.sh reload 
    
    [Install]     
      WantedBy=multi-user.target
  3. Write and quit this file by pressing the ESC button then:wq!
    Now Service is all setup.

You can enable/start/stop your Java Application as a Service using the following commands:

sudo systemctl daemon-reload
sudo systemctl enable customservicename
sudo systemctl start  customservicename
sudo systemctl stop   customservicename

This should get Java Application up and running in the background as a System Service.

FAQs

1) How do I upload files to the EC2 instance?

  • Method 1: FTP client-based file uploading. You can use FTP clients like FileZilla, WinSCP, etc. to upload your files to the EC2 instance.
  • Method 2: Windows-based file upload to an EC2 instance.
  • Method 3: Upload files through the command prompt.

2) What Is the Java JAR Command?

Based on the ZIP and ZLIB compression formats, the jar command is a general-purpose archiving and compression utility. The jar command was initially intended to package Java applets or applications; however, starting with JDK 9, users can use the jar command to construct modular JARs. Java applets are no longer supported as of
JDK 11.

3) Why jar files are used in Java?

You can use JAR files for activities like lossless data compression, archiving, decompression, and archive unpacking because they are packaged in the ZIP file format. These are some of the most typical tasks for JAR files, and by utilizing just these fundamental features, you can enjoy a wide range of JAR file advantages.


Related Post:-

Share this post

Back to Blog