Score:0

How to pass variables for a systemd unit file that calls a php script?

jp flag

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 avatar
it flag
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 "[email protected]" syntax.*
DevOpsSauce avatar
jp flag
So I can't have two arguments for the php script?
Score:0
it flag

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 "[email protected]" 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 [email protected]
systemctl start [email protected]

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 [email protected]
systemctl start [email protected]
...
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.