Register Mojolicious application with systemd

Register your Mojolicious application with systemd. With systemd management, you can start / stop / restart using the systemctl command. You can also have the Mojolicious application restart automatically when you restart the OS.

Information required for systemd configuration file

Please note that the path of hypnotoad and the environment variables loaded by the user must be specified in the systemd configuration file as well.

hypnotoad path

Find out the path of hypnotoad.

which hypnotoad

It was displayed as follows.

~ / perl5 / perlbrew / perls / perl-5.20.3 / bin / hypnotoad

Since the "~" part is changed to the home directory, check the home directory name.

#Home directory name
env | grep -P'^ HOME'

It was displayed as follows.

HOME = / home / myapp

The path for hypnotoad is as follows.

/home/myapp/perl5/perlbrew/perls/perl-5.20.3/bin/hypnotoad

Web application path

The path of the web application program. If you made it using the Mojolicious template, it will be in the following location. Suppose you created it with the name "Webapp".

/ home / myapp / webapp / script / webapp

Web application process ID file

The path to the process ID file for the web application.

/home/myapp/webapp/script/hypnotoad.pid

Environment variables

This time, I will write an article assuming that the following environment variables are set in "~ / .bashrc".

export MY_SERVER_ENV = my_dev

You need to write this environment variable in the systemd configuration file.

systemd configuration file

This is a systemd configuration file. Give it the following name.

sudo vi /lib/systemd/system/webapp.service

vi Open the command and write the following content.

[Unit]
Description = My Mojolicious application
After = network.target

[Service]
Type = forking
PIDFile = / home / myapp / webapp / script / hypnotoad.pid
ExecStart = / home / myapp / perl5 / perlbrew / perls / perl-5.20.3 / bin / hypnotoad / home / myapp / webapp / script / webapp
ExecReload = / home / myapp / perl5 / perlbrew / perls / perl-5.20.3 / bin / hypnotoad / home / myapp / webapp / script / webapp
ExecStop = / home / myapp / perl5 / perlbrew / perls / perl-5.20.3 / bin / hypnotoad / home / myapp / webapp / script / webapp --stop
KillMode = process
User = myapp
Group = myapp-group
Environment = MY_SERVER_ENV = my_dev

[Install]
WantedBy = multi-user.target

Service activation

Let's activate the service.

sudo systemctl enable webapp

If the Mojolicious application is already running, stop it.

/home/myapp/perl5/perlbrew/perls/perl-5.20.3/bin/hypnotoad /home/myapp/webapp/script/webapp --stop

Start your Mojolicious application under systemd's control.

sudo systemctl start webapp

Let's check if the Mojolicious application is running.

sudo systemctl status webapp

Check if the Mojolicious application can be restarted.

sudo systemctl reload webapp

Let's see if the Mojolicious application can be stopped.

sudo systemctl stop webapp

OS restart

Perform Restart OS and see if the Mojolicious application is automatically restarted.

sudo reboot

Associated Information