Getting started with systemd

This document provides simple introduction of systemd and configuration steps for aah application to accomplish it.

systemd is an init system that provides many powerful features for starting, stopping, reload configuration and managing processes. Refer to systemd man page.

systemd consists of two main concepts:

  • A unit is a configuration file that describes the properties of the process that you’d like to run.
  • A target is a grouping mechanism that allows systemd to start up groups of processes at the same time. This happens at every boot as processes are started at different run levels.

Note: Ensure the rights are set correctly and provide ownership to the user you want to use. Here, I'll use the aah as a user account.

Table of Contents

Handy generate Command

Since v0.10 aah CLI includes generate command to help developer in application development. To generate systemd service file for application, run below command.

# short form
aah g s -n systemd -i github.com/user/appname

# fullname form
aah generate script --name systemd --importpath github.com/user/appname

# if you're in the app directory or path
aah generate script --name systemd

Steps to Configure and Enable

  1. Place your service file at /etc/systemd/system/. For e.g.: aahwebsite.service
  2. We are gonna reload it, execute sudo systemctl daemon-reload.
  3. Enable aahwebsite.service with systemctl, execute sudo systemctl enable aahwebsite.service.
  4. Congrats, you have successfully created and enabled your application service.

How to Start, Stop, Restart, Reload and Status

Now let’s see how to utilize the service file. Don’t forget to replace your service file/name with below example commands 😄.

Start your application

sudo systemctl start aahwebsite.service 

# OR
sudo service aahwebsite start

Stop your application

sudo systemctl stop aahwebsite.service

# OR
sudo service aahwebsite stop

Restart your application

sudo systemctl restart aahwebsite.service

# OR
sudo service aahwebsite restart

Reload your application

Since v0.10 aah application binary supports hot-reload (to reflect changes done in config, views, i18n, etc) without application restart via SIGHUP signal.

sudo systemctl reload aahwebsite.service

# OR
sudo service aahwebsite reload

Status of your application

sudo systemctl status aahwebsite.service

# OR
sudo service aahwebsite status

aahframework.org systemd service file

For reference. This is aahframework.org website service file. aah application deployed at location /home/aah/website.

# GENERATED BY aah CLI - Feel free to customization it.
# FILE: aahwebsite.service
# DATE: Sat, 19 May 2018 21:34:31 -0700
# DESC: aah application systemd service file

[Unit]
Description=aahwebsite application
After=network.target

[Service]
User=aah
Group=aah
EnvironmentFile=/home/aah/website/env_values
ExecStart=/home/aah/website/bin/aahwebsite -profile prod
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target