Điểm:0

Multiple instances of systemd unit not writing to log files

lá cờ 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
lá cờ cn
Trong khi bắt đầu một tệp từ tập lệnh của bạn hoạt động, tôi có thể nói rằng việc sử dụng thư mục /var/log trên toàn hệ thống cho tập lệnh bash có thể không an toàn và bạn vẫn phải đối mặt với lỗi xuất chuẩn không mong muốn đến từ tập lệnh. Bạn có cân nhắc việc chỉ sử dụng nhật ký và `systemd-cat` hoặc `logger` trong tập lệnh của mình không?
DevOpsSauce avatar
lá cờ jp
Đây không phải là đường dẫn nhật ký vĩnh viễn. Tôi chỉ đang thử nghiệm trên máy chủ dev. :) Nhật ký nằm trong thư mục con của /var/log/. Chúng đã tồn tại khá lâu, nhưng bây giờ chúng muốn có nhiều phiên bản của cùng một tập lệnh.
Gerrit avatar
lá cờ cn
Có thể không hợp lý khi đặt ARGS vào môi trường, nếu bạn đã đưa thông tin này vào dòng ExecStart. Chỉ cần sử dụng '%i" trong dòng ExecStart và thế là xong. Ưu tiên sử dụng %i ở đây, vì nếu không, bạn có thể thoát khỏi các dấu ngoặc kép và hiện tượng toàn cầu hóa và tách từ không mong muốn bởi trình bao.Không thành vấn đề nếu bạn chỉ sử dụng các số, nhưng chúng tôi đặc biệt khuyên bạn nên sử dụng các phương thức truyền biến thể hiện này một cách tổng quát hơn.
Gerrit avatar
lá cờ cn
Ngoài ra, hãy xem tại đây để biết ý tưởng về cách đóng gói các phiên bản của bạn thành một mục tiêu có thể chạy một bộ đã xác định của chúng: https://www.stevenrombauts.be/2019/01/run-multiple-instances-of-the-same-systemd -đơn vị/
Điểm:0
lá cờ sa

Bạn nên sửa chữa của bạn .dịch vụ file cho một số tệp nhật ký. Các biến phải có dấu ngoặc nhọn {}. Như thế này:

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

Đăng câu trả lời

Hầu hết mọi người không hiểu rằng việc đặt nhiều câu hỏi sẽ mở ra cơ hội học hỏi và cải thiện mối quan hệ giữa các cá nhân. Ví dụ, trong các nghiên cứu của Alison, mặc dù mọi người có thể nhớ chính xác có bao nhiêu câu hỏi đã được đặt ra trong các cuộc trò chuyện của họ, nhưng họ không trực giác nhận ra mối liên hệ giữa câu hỏi và sự yêu thích. Qua bốn nghiên cứu, trong đó những người tham gia tự tham gia vào các cuộc trò chuyện hoặc đọc bản ghi lại các cuộc trò chuyện của người khác, mọi người có xu hướng không nhận ra rằng việc đặt câu hỏi sẽ ảnh hưởng—hoặc đã ảnh hưởng—mức độ thân thiện giữa những người đối thoại.