systemd 单元的多个实例未写入日志文件

systemd 单元的多个实例未写入日志文件

我正在尝试创建一个 systemd 单元文件,它将接受 php 脚本的参数。

小型 php 脚本:

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

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

开发人员正在编写真正的脚本。我所需要做的就是测试一个参数,该参数在systemd单元文件中传递。如下所示:

[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

我使用和启动多个实例。检查服务时,我看到:systemctl start [email protected]systemctl start [email protected]

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 

它不会创建不同的日志文件。它只会创建一个日志文件,奇怪的是,它被引号括起来了:

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

日志文件的内容大部分是。由于某些原因,"Argument = bar"只有少数几个是:"Argument = foo"

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

是否可以为 systemd 单元的每个实例写入不同的日志文件?我想看看test-multiple-systemd-foo.log这些test-multiple-systemd-bar.log是否是我传递的参数。我尝试过$在日志路径中转义,但没有成功。我尝试过删除并添加引号,但Environment="$ARGS=%I"同样没有成功。日志始终显示"$ARGS"名称中的文字带有引号。

更新

看来我无法动态创建多个日志文件,这个答案

因此,我使用以下命令在 php 脚本中创建日志写入:

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

这样就够了吗?我现在正在获取多个日志文件,它们正在将参数正确地写入日志文件。

答案1

您应该修复.service多个日志文件的文件。变量应该用括号括起来{}。如下所示:

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

相关内容