Puncte:0

Multiple instances of systemd unit not writing to log files

drapel jp

I'm trying to create a systemd unit file that will take an argument for a php script.

The small php script:

<?php
$val = $argv[1];

while(true) {
        echo "Argument = $val\n";
        sleep(1);
}
?>

Devs are working on the real script. All I need to do to test is take an argument, which is passed in the systemd unit file. Here it is:

[email protected]

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

[Service]
Environment=ARGS=%I
ExecStart=/usr/bin/php /home/myself/test_systemd.php $ARGS
StandardOutput=file:/var/log/test-multiple-systemd-$ARGS.log

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

I start the multiple instances with systemctl start [email protected] and systemctl start [email protected]. When checking the services, I see:

root@servername:/lib/systemd/system# systemctl --type=service | grep test
[email protected]      loaded active running Systemd Params Test                                                          
[email protected]      loaded active running Systemd Params Test 

It is not creating the different log files. It's only creating a single one, strangely with quotes around it:

-rw-r--r-- 1 root root 285 Dec 8 08:15 'test-multiple-systemd-$ARGS.log'

And the content of the log file is mostly "Argument = bar". Only a few instances of "Argument = foo" for some reason:

cat /var/log/test-multiple-systemd-\$ARGS.log

Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = bar
Argument = foo <--- WHY???
Argument = foo
Argument = foo
Argument = foo
Argument = foo
Argument = foo

Is it possible to write to different log files for each instance of the systemd unit? I would like to see test-multiple-systemd-foo.log and test-multiple-systemd-bar.log if those are the arguments I pass. I've tried escaping the $ in the log path to no avail. I've tried removing and adding quotes to the Environment="$ARGS=%I" also to no avail. The log always shows with quotes with the literal "$ARGS" in the name.

UPDATE

It appears I cannot create the multiple log files dynamically, per this answer.

So, I created the log writing in the php script with:

echo file_put_contents("/var/log/test-multiple-systemd-$val.log", "Argument=$val", FILE_APPEND);

Will this suffice? I'm getting the multiple log files now and they are writing the argument correctly to the log file.

Gerrit avatar
drapel cn
În timp ce pornirea unui fișier din scriptul dvs. funcționează, aș spune că utilizarea directorului /var/log la nivel de sistem pentru un script bash ar putea fi nesigură și încă trebuie să vă confruntați cu stderr/stdout neașteptat care vine din script. Te-ai gândit să folosești doar jurnalul și `systemd-cat` sau `logger` în scriptul tău?
DevOpsSauce avatar
drapel jp
Aceasta nu este o cale de jurnal permanentă. Testez doar pe un server de dezvoltare. :) Jurnalele sunt într-un subdirector al /var/log/. Ele există de ceva timp, dar acum își doresc mai multe instanțe ale aceluiași script.
Gerrit avatar
drapel cn
Probabil că nu are sens să puneți ARGS în mediu, dacă includeți deja aceste informații pe linia ExecStart. Folosiți „%i” în linia ExecStart și terminați. Preferiți %i aici, pentru că altfel ați putea scăpa de ghilimele și ghilimele nedorite și împărțirea cuvintelor de către shell.Nu este o problemă dacă utilizați doar numere, dar este foarte recomandat pentru utilizarea mai generală a acestor metode de transmitere a variabilei de instanță.
Gerrit avatar
drapel cn
De asemenea, căutați aici o idee despre cum să vă împachetați instanțele într-o țintă care poate rula un set definit de ele: https://www.stevenrombauts.be/2019/01/run-multiple-instances-of-the-same-systemd -unitate/
Puncte:0
drapel sa

Ar trebui să-ți repari .serviciu fișier pentru mai multe fișiere jurnal. Variabilele ar trebui să fie cu bretele {}. Ca aceasta:

ExecStart=/usr/bin/php /home/myself/test_systemd.php ${ARGS}
StandardOutput=file:/var/log/test-multiple-systemd-${ARGS}.log

Postează un răspuns

Majoritatea oamenilor nu înțeleg că a pune multe întrebări deblochează învățarea și îmbunătățește legătura interpersonală. În studiile lui Alison, de exemplu, deși oamenii își puteau aminti cu exactitate câte întrebări au fost puse în conversațiile lor, ei nu au intuit legătura dintre întrebări și apreciere. În patru studii, în care participanții au fost implicați în conversații ei înșiși sau au citit transcrieri ale conversațiilor altora, oamenii au avut tendința să nu realizeze că întrebarea ar influența – sau ar fi influențat – nivelul de prietenie dintre conversatori.