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;

1
2
3
4
5
6
lsb_release -a
Distributor ID: Ubuntu
Description: Ubuntu 16.04.2 LTS
Release: 16.04
Codename: xenial

Define Service

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.

1
cd /lib/systemd/system

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;

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=MeBlog
[Service]
EnvironmentFile=/etc/default/meblog
WorkingDirectory=/home/ubuntu/blog/meblogging
PIDFile=/var/run/meblog.pid
ExecStart=/usr/bin/hexo server &
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target

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

1
sudo systemctl enable myservice

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;

1
sudo service myservice start

You can also checkout the service status with;

1
service myservice status

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;

1
journalctl -u myservice.service

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;

1
systemctl daemon-reload

Try Restart On Failure

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;

1
2
3
4
ps -A | grep <processname> (ie. ps -A | grep hexo)
#Example Output:
3804 ? 00:00:01 hexo

Next we will kill our service by;

1
sudo kill -9 <pid> (ie. sudo kill -9 3804)

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.

1
2
3
4
5
6
7
8
9
10
11
service <servicename> status (ie. service meblog status)
#Example Output
● meblog.service - MeBlog
Loaded: loaded (/lib/systemd/system/meblog.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: signal) since Mon 2017-03-06 11:04:54 UTC; 1s ago
Process: 3762 ExecStart=/usr/bin/hexo server & (code=killed, signal=KILL)
Main PID: 3762 (code=killed, signal=KILL)
Mar 06 11:04:54 ip-172-31-36-176 systemd[1]: meblog.service: Main process exited, code=killed, status=9/KILL
Mar 06 11:04:54 ip-172-31-36-176 systemd[1]: meblog.service: Unit entered failed state.
Mar 06 11:04:54 ip-172-31-36-176 systemd[1]: meblog.service: Failed with result 'signal'.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
#Example Output:
● meblog.service - MeBlog
Loaded: loaded (/lib/systemd/system/meblog.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2017-03-06 11:04:59 UTC; 10min ago
Main PID: 3804 (hexo)
CGroup: /system.slice/meblog.service
└─3804 hexo
Mar 06 11:04:59 ip-172-31-36-176 systemd[1]: meblog.service: Service hold-off time over, scheduling restart.
Mar 06 11:04:59 ip-172-31-36-176 systemd[1]: Stopped MeBlog.
Mar 06 11:04:59 ip-172-31-36-176 systemd[1]: Started MeBlog.
Mar 06 11:05:00 ip-172-31-36-176 hexo[3804]: INFO Start processing
Mar 06 11:05:00 ip-172-31-36-176 hexo[3804]: INFO Hexo is running at http://localhost:4000/. Press Ctrl+C to stop.