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