Skip to main content

Start your service

After your wrote your first custom service, most likely you want to be sure that it will automatically start at every boot and restart in case of a failure or unexpected exit. One way to achive that is to exploit the potential of systemd, a suite of basic building blocks for a Linux system.

Let's create a systemd service for our diagnostic service developed in the previous section. Create a file called connhex-edge-diagnostic.service and place it in the /etc/systemd/system folder inside your edge device.

[Unit]
Description=connhex-edge-diagnostic
# Our service will start after the 'connhex-edge-export' service,
# a core connhex-edge service that is automatically installed on your device.
After=connhex-edge-export.service
Requires=connhex-edge-export.service

[Service]
Type=simple
Restart=always
RestartSec=15s
# The path where the source files of our service are located.
WorkingDirectory=/opt/connhex
# Command used to start our service.
ExecStart=/usr/bin/node /opt/connhex/diagnostic-service/src/index.js
ExecStop=/bin/kill -s SIGINT $MAINPID

[Install]
WantedBy=multi-user.target

We can now start our service:

$ systemctl start connhex-edge-diagnostic

And automatically get it to start on boot:

$ systemctl enable connhex-edge-diagnostic
Systemd service configuration

If you want a more in-depth description of all available systemd services configurations take a look here.