Getting started with systemd

This document provides simple introduction of systemd and steps for your 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 user account.

Table of Contents

Handy generate Command

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

# run anywhere
aah generate script --name systemd --importpath

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

# OR short form
aah g s -n 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, 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 systemctl start aahwebsite

# OR
sudo service aahwebsite start

Stop your application

sudo systemctl stop aahwebsite.service

# OR
sudo systemctl stop aahwebsite

# OR
sudo service aahwebsite stop

Restart your application

sudo systemctl restart aahwebsite.service

# OR
sudo systemctl restart aahwebsite

# OR
sudo service aahwebsite restart

Reload your application

Since v0.10 Reload functionality available.

sudo systemctl reload aahwebsite.service

# OR
sudo systemctl reload aahwebsite

# OR
sudo service aahwebsite reload

Status of your application

sudo systemctl status aahwebsite.service

# OR
sudo systemctl status aahwebsite

# OR
sudo service aahwebsite status

You will get similar output:

● aahwebsite.service -
   Loaded: loaded (/etc/systemd/system/aahwebsite.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2017-09-22 17:05:20 PDT; 6s ago
  Process: 16623 ExecStart=/home/aah/website/ start (code=exited, status=0/SUCCESS)
 Main PID: 16629 (aahwebsite)
    Tasks: 6
   Memory: 8.4M
      CPU: 748ms
   CGroup: /system.slice/aahwebsite.service
           └─16629 /home/aah/website/bin/aahwebsite -profile=prod

Sep 22 17:05:20 systemd[1]: Starting
Sep 22 17:05:20[16623]: aahwebsite started.
Sep 22 17:05:20 systemd[1]: Started systemd service file

For your reference. This is website service file. aah application deployed/extracted at location /home/aah/website.


ExecStart=/home/aah/website/ start
ExecStop=/home/aah/website/ stop
ExecReload=/bin/kill -HUP $MAINPID