søndag 8. mai 2016

How to add a systemctl service to a Ubuntu server

I have lately been using Vert.x for simple web servers/services, and found running them directly on my server was just as convenient as trying to set up Docker...

As you may know Vert.x is a framework for writing reactive applications than runs on the Java Virtual Machine (JVM). Either you can install the Vert.x framework on your server and run your application in a Vert.x context, or you can package everything (both your own code and the framework) in a single (fat) jar and run it directly.

Run command:

 sudo java -jar myjarfile.jar

This command will run the service, but it will be terminated as soon as you exit out of the terminal window. To make it run as a service follow the steps below.

Note that a JVM needs to be installed on your server. To install Oracle Java 8 on Ubuntu follow this guide.


Create a folder for the files that make up your service


The "standard" folder for your own services on a Ubuntu server would normally be /usr/local, but you can use a different folder if this better suits your needs.
Use the commands in the code section below to create a folder for your service.

 cd /usr/local  
 sudo mkdir myservice  
 cd myservice  


Add the files for your site/service


Adding the files that makes up your service depends on how you work when you are doing your development. I usually write and test my code locally, storing my files in a git repository. I then use git from the command line to update site content.

Command to install git client on Ubuntu:

 sudo apt-get install git  

Command to clone your git repository to the server:

 git clone https://<username>@<server>/<path-to-repo.git>

Note that when you clone a repository the repository will be cloned to the current directory.


Command to retrieve updates from  your git repository.

 git pull https://<username>@<server>/<path-to-repo.git>



Create a bash script for starting your service


To create the bash script I usually prefer nano.

 sudo nano runmyservice.sh

This is the file that eventually starts your service. To start a JVM, use something similar to the command below.

 #!/bin/bash  
 cd /usr/local/myservice/<name-of-repo-folder>
 java -jar ./<name-of-jar>.jar  

Make the script executable:

 chmod 755 ./runmyservice.sh  


Add a service file for your service


You must then create a service-file in the /lib/systemd/system directory.

 cd /lib/systemd/system
 sudo nano myservice.service


Add the following text:

 [Unit]  
 Description=MyService
 [Service]  
 ExecStart=/usr/local/myservice/runmyservice.sh  
 Type=simple  
 User=root
 Restart=always
 [Install]  
 WantedBy=multi-user.target


This file is what enables systemctl to start, stop and generally handle your service.



Enable your service so that it starts when the server starts


 sudo systemctl enable myservice.service

You can now operate your service using systemctl.


Some useful commands


Start service

 sudo systemctl start myservice.service

Stop service

 sudo systemctl stop myservice.service