Hermit Notebook

Create a custom systemd service.

Reminder: you can left-scroll and right-scroll the long code blocks

This article shows how to create custom service running under systemd on Linux. This way, you can use the same command you use to manage an Apache or Nginx services, but this time to manage you own services.

Create a cusom systemd service

  1. Create a hermit.service file in the directory /etc/systemd/system/. In the exemple above, other options for ̀Type (under [Service]) include forking, and other options for Restart include always and on-abort.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [Unit]
    Description=My Custom Service
    After=network.target

    [Service]
    Type=simple
    User=hermit
    WorkingDirectory=/home/hermit
    ExecStart=/home/hermit/bin/hermit_deamon
    Restart=on-failure

    [Install]
    WantedBy=multi-user.target
  2. Start the service:

    1
    systemctl start hermit.service
  3. Let the service start at boot:

    1
    systemctl enable hermit.service

Basic service management

Enable or disable “start on boot”

1
2
3
systemctl enable hermit.service

systemctl disable hermit.service

Start or stop a service

1
2
3
systemctl start hermit.service

systemctl stop hermit.service

Reload or restart a service

1
2
3
systemctl daemon-reload hermit.service

systemctl restart hermit.service

Check the status of a services

This command will let you see if the service is running, show you its uptime and display its latest logs.

1
systemctl status hermit.service

Check the logs of the services

This command get the last 100 logs from the given service.

1
journalctl -n 100 -u hermit.service

Contents

  1. 1. Create a cusom systemd service
  2. 2. Basic service management
    1. 2.1. Enable or disable “start on boot”
    2. 2.2. Start or stop a service
    3. 2.3. Reload or restart a service
    4. 2.4. Check the status of a services
    5. 2.5. Check the logs of the services