Define Service on Ubuntu
This blog is running over hexo blog framework which is powered by NodeJS.
The first thing, I did is to make hexo run at the boot of Ubuntu server or after hexo crashes somehow. There are multiple ways to do this, but here I will describe the latest practice over Ubuntu server.
I will describe for Ubuntu 16.04.2 LTS. To learn your ubuntu distribution version use command below;
To define a service under Ubuntu, you need to register this service / daemon to it with a bash script. It was very complex in the past, but when you do once than it was a copy paste routine.
However generating a huge script for such an easy task was feeling wrong. Right now, it is pretty straight forward. In this example, I will describe how to define this blog hexo
as a service for ubuntu. When an unhandled exception occurs in your program that is running as a service, you can configure the service to restart it also.
Here you will see all the defined services under Ubuntu. To create a new service just create another myservice.service file and add the following into it. I will describe what each line meant for. Below is the configuration file for this blog service;
Description=MeBlog : This line is a human readable description for our service.
EnvironmentFile=/etc/default/meblog : If there are environment variables need to be set, than you can define those under this file.
PIDFile=/var/run/meblog.pid : This is an important parameter, since Ubuntu tracks the executable via this process id recorded under /var/run/meblog.pid
ExecStart=/usr/bin/hexo server & : Telling Ubuntu how to run our executable. You can define any parameter here and no need for double quote here.
Restart=on-failure : What to do when our executable crashes. Here I choose restart. You can also use Restart=always just to be on the safe side.
RestartSec=5s : How long shall Ubuntu wait after failure or stop to restart your service.
WantedBy=multi-user.target : This is required by systemctl when enabling and disabling your service. For user services multi-user.target location is appropriate.
After our file is in place with the necessary configurations set, we need to enable this service
This will generate a symbolic link under /etc/systemd/system/multi-user.target.wants/ for your service, targeting to your configuration file. To start the service;
You can also checkout the service status with;
There is also built-in logging for your services - journal. This is very helpful to see what is happening on your application. Journal logs the standard output, so if you don’t have complex log for your application, journal is more than enough. For NodeJS applications, this corresponds to your console.log messages. To see your log use the below shell command;
One last note is that; if you change your service configuration file (myservice.service), than to be able to load the latest settings please run following;
Let’s try the service auto restart functionality. The easiest way to do this is to kill your service pid. In our example it is the process hexo. To get the pid of your process simply execute;
Next we will kill our service by;
Then you to see the status of your service as killed with signal 9. After a while, in our case in 5 seconds, you should see your service restarted.
The service restarted and I again provide the example output for service status. You can also restart your Ubuntu instance and see that the service is started after boot.