Ubuntu java service wrapper

January 06, 2019 Roy 0 Comments

This is a simple wrapper to run a Java program as service. You need to be a root user. In this example SYSTEMD is used instead of init.d because it's becoming obsolete


Instructions:
  1. Create a file under  /etc/systemd/system/   with nano or vi and paste the example script below. eg.  sudo vi /etc/systemd/system/MyService.service
  2. Paste the code below in your new file:



  • Create a file with under /usr/local/bin/ eg. sudo vi /usr/local/bin/MyService.sh
  • Paste the Code example below
  • Modify the SERVICE_NAME, PATH_TO_JAR, and choose a PID_PATH_NAME for the file you are going to use to store your service ID.
  • Write the file and give execution permisions ex. sudo chmod +x /usr/local/bin/MyService.sh
  • Test that it runs ex. /usr/local/bin/./MyService.sh start
  • Test that it stops ex. /usr/local/bin/./MyService.sh stop
  • Test that it restarts ex. /usr/local/bin/./MyService.sh restart
  • Enable the service with the command sudo systemctl enable MyService
  • To run the service sudo systemctl start MyService.service
  • To stop the service sudo systemctl stop MyService.service
Copy the code example:


0 comments: