0

I am needing to create multiple instances of a systemd service. Is it possible to pass the parameters when starting the service, which would be the params for a php script?

For example, let's say I have a script, test_systemd.php, with two parameters, mod and rem respectively.

The php:

<?php
$val = getopt(null, ['mod:','rem:']);
echo $val['mod']."\n";
echo $val['rem']."\n";
echo "\n";

?>

If I run that via php, I just do php test_systemd.php --mod foo --rem bar

Which would echo out

foo
bar

According to the systemd docs, I'm supposed to use Environment=, but I am unsure of how to do that. Let's say I have a systemd unit file called test-systemd-params.service:

Description=Systemd Params Test
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/bin/php -f /path/to/test_systemd.php
StandardOutput=file:/var/log/test-multiple-systemd.log
Environment=???? <--- WHAT GOES HERE?

[Install]
#Start after boot
WantedBy=multi-user.target

I don't understand what I need tp place here. Is that even possible for the two params (mod and rem) to be passed when starting an instance of the service?

I found this example, and it has two Environment lines, but one of them is a static value! I need two dynamic values, like:

Environment=mod=%i Environmen=rem=%i

The example snippet looks like:

[Service]
Type=notify
Environment=LANG=C <--- STATIC, but need another dynamic
Environment=HTTPD_INSTANCE=%i <--- dynamic, great, but need another
ExecStartPre=/bin/mkdir -m 710 -p /run/httpd/instance-%i
ExecStartPre=/bin/chown root.apache /run/httpd/instance-%i
ExecStartPre=/bin/mkdir -m 700 -p /var/lib/httpd/instance-%i
ExecStartPre=/bin/chown apache.apache /var/lib/httpd/instance-%i
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND -f conf/%i.conf
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful -f conf/%i.conf
Chris
  • 282
  • 2
  • 9
DevOpsSauce
  • 348
  • 1
  • 5
  • 22
  • The example snippet you give refers to [service templates](https://www.freedesktop.org/software/systemd/man/systemd.service.html#Service%20Templates). You can pass only one *variable* this way *It is possible for systemd services to take a single argument via the "service@argument.service" syntax.* – Chris Dec 03 '21 at 20:18
  • So I can't have two arguments for the php script? – DevOpsSauce Dec 03 '21 at 20:32

1 Answers1

0

According to the systemd docs, I'm supposed to use Environment=, but I am unsure of how to do that. Let's say I have a systemd unit file called test-systemd-params.service:

This is an example using Environment=

Description=Systemd Params Test
Wants=network-online.target
After=network-online.target

[Service]
Environment="MOD=foo" "REM=bar"
ExecStart=/usr/bin/php -f /path/to/test_systemd.php --mod ${MOD} --rem ${REM}
StandardOutput=file:/var/log/test-multiple-systemd.log

[Install]
#Start after boot
WantedBy=multi-user.target

This is not what you are looking because variables are static values.


I found this example, and it has two Environment lines, but one of them is a static value! I need two dynamic values, like:

The example snippet you give refers to service templates. You can pass only one variable this way. It is possible for systemd services to take a single argument via the "service@argument.service" syntax.

Here is an example implementation

Description=Systemd Params Test
Wants=network-online.target
After=network-online.target

[Service]
Environment=MOD=%i
Environment=REM=bar
ExecStart=/usr/bin/php -f /path/to/test_systemd.php --mod ${MOD} --rem ${REM}
StandardOutput=file:/var/log/test-multiple-systemd.log

[Install]
#Start after boot
WantedBy=multi-user.target

Then you start the services like this:

systemctl start test-systemd-params@foo.service
systemctl start test-systemd-params@bar.service

So it's still not exactly what you are looking for, because you still have one static variable.


The only way I see (not tested) to pass multiple variables would be to use EnvironmentFile= directive and separated config files with dynamic name using the @argument synthax.

[Service]
EnvironmentFile=/path/to/config/%i
ExecStart=/usr/bin/php -f /path/to/test_systemd.php --mod ${MOD} --rem ${REM}
StandardOutput=file:/var/log/test-multiple-systemd.log

Example config file in /path/to/config/, let's say conf1:

MOD=foo
REM=bar

Then you start the services like this:

systemctl start test-systemd-params@conf1.service
systemctl start test-systemd-params@conf2.service
...
Chris
  • 282
  • 2
  • 9