Systemd Customization with Dynamic Parameters

Systemd is a very popular option for service / process management on linux systems. This tutorial is to deep dive into it a bit and show you how to dynamically pass in parameters to systemd managed processes while running systemctl and use systemd customization with Dynamic Parameters

Generally, when creating a systemd process. you will add a config file as below (e.g ubuntu)

/lib/systemd/system/echo.service

In the file, following configuration are specified.

[Unit]
Description=this is a echo service

[Service]
Type=oneshot
ExecStart=/bin/echo "helloworld"
StandardOutput=syslog

Then test the service using

systemctl restart echo
systemctl status echo

This will show message similar as below, this prints hellow

www systemd[1]: Starting this is a echo service...
www echo[...]: helloworld
www systemd[1]: echo.service: Succeeded.
www systemd[1]: Finished this is a echo service.

but as you can see, this is static configuration. how to dynamically pass in parameters to systemd and run it dynamically based on specific usage on different scenarios.

i.e using echo.service to display helloworld in case 1 and beauty in case 2.

to do that we need to use [email protected] syntax.

in case 1, e.g. the command will be

systemctl restart echo@helloworld

In case 2, e.g the command will be

systemctl restart echo@beauty

to resolve it, create a similar config

/lib/systemd/system/[email protected]

add configuration similar to below

notice: %i? will populate that with whatever follows the @ sign when the service is started.

[Unit]
Description=this is a echo service '%I'

[Service]
Type=oneshot
ExecStart=/bin/echo %i
StandardOutput=syslog

then run

systemctl status echo@helloword
www systemd[1]: Starting this is a echo service 'helloword'...
www echo[...]: helloword
www systemd[1]: [email protected]: Succeeded.
www systemd[1]: Finished this is a echo service 'helloword'.
systemctl status echo@beauty
www systemd[1]: Starting this is a echo service 'beauty'...
www echo[...]: beauty
www systemd[1]: [email protected]: Succeeded.
www systemd[1]: Finished this is a echo service 'beauty'.

Share This

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
*