使用 systemd 將輸出管道傳輸到程式

使用 systemd 將輸出管道傳輸到程式

這看起來應該很簡單,但我正在努力使其發揮作用。我正在嘗試設定一個 systemd 服務,以便從記憶體中輸出日誌條目的日誌記錄程式(varnishncsa)的輸出可以透過管道傳輸到 cronolog。我能夠使它工作的唯一方法是在前台。我正在嘗試使其作為系統服務運行。接管前台的腳本是:

#!/bin/bash
/usr/bin/varnishncsa -F '%{X-Real-IP}i %l %u %t "%r" %s %b "%{Referer}i" "%{User-agent}i"' -q "ReqHeader:Host ~ '(^|\.)example\.com$'" -C |/usr/sbin/cronolog "/example/varnish_access_log.%Y-%m-%d"

我正在嘗試的 systemd 服務設定是:

[Unit]
Description=Example website Varnish Cache HTTP accelerator NCSA logging daemon
After=varnish.service

[Service]
RuntimeDirectory=varnishncsa
Type=forking
PIDFile=/run/varnishncsa/varnishncsa-example.pid
User=varnish
Group=varnish
ExecStart=/usr/local/bin/varnishncsa-example.sh
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

它基於提供的 systemd 服務,用於作為守護程序運行並寫入文件(我無法使用 -D 和 -P 直接作為守護程序運行它,並通過管道輸出):

[Unit]
Description=Varnish Cache HTTP accelerator NCSA logging daemon
After=varnish.service

[Service]
RuntimeDirectory=varnishncsa
Type=forking
PIDFile=/run/varnishncsa/varnishncsa.pid
User=varnish
Group=varnish
ExecStart=/usr/bin/varnishncsa -a -w /var/log/varnish/varnishncsa.log -D -P /run/varnishncsa/varnishncsa.pid
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

我嘗試了很多選項來讓它在後台運行,但無法讓任何東西工作,並且在網上找不到任何專門處理這種情況的東西。可能是因為 systemd 還很新,或是其他這樣做的人更了解他們在做什麼!

非常感謝任何幫助。

答案1

您已刪除該-D選項,因此程式將不再自行守護。您也刪除了將-P進程 ID 儲存在檔案中的選項。因此,您的單位不應繼續說它是分叉類型,也不應提供您未填寫的虛假 pid 檔案。請嘗試Type=simple刪除PIDFile=...ExecReload=...,因為您的腳本無法處理日誌檔案的輪換。

相關內容