如何查看 systemd 服務何時啟動/停止/重新啟動?

如何查看 systemd 服務何時啟動/停止/重新啟動?

我有一個服務(由我自己編寫)在 Debian (Jessie) 伺服器上運行,而該服務自己的日誌恰好表明它在特定時間重新啟動。沒有跡象表明存在段落錯誤或其他崩潰,所以我現在試圖弄清楚應用程式是否以某種方式默默失敗並被 systemd 重生,或使用者是否故意通過systemctl.

shell 歷史記錄並沒有顯示此類活動,但這並不是結論性的export HISTCONTROL=ignoreboth,因為 SSH 工作階段可能剛剛逾時,阻止了先前登入的 bash 歷史記錄寫入磁碟。當時伺服器沒有重新啟動。

但我希望 systemd 本身應該保留一個日誌,指示服務何時啟動故意重新啟動。令我驚訝的是,我無法找到任何journalctl有關如何獲取此類日誌的文檔(例如 )。

其他一些帖子(例如普通用戶 systemd 服務在哪裡/為什麼沒有日誌?)似乎表明應該有這樣的日誌訊息:

Jan 15 19:28:08 qbd-x230-suse.site systemd[1]: Starting chatty.service...
Jan 15 19:28:08 qbd-x230-suse.site systemd[1]: Started chatty.service.

但我在我的系統上沒有看到此類日誌訊息。

有沒有辦法找出 systemd 服務何時啟動、停止或重新啟動?

編輯:人們可能遇到的典型問題似乎是他們journalctl以非特權使用者身分運作。我的情況並非如此,我一直在操作root。作為對評論的回應,跑步grep systemd /var/log/syslog只給了我這個:

Jun  6 09:28:35 server systemd[22057]: Starting Paths.
Jun  6 09:28:35 server systemd[22057]: Reached target Paths.
Jun  6 09:28:35 server systemd[22057]: Starting Timers.
Jun  6 09:28:35 server systemd[22057]: Reached target Timers.
Jun  6 09:28:35 server systemd[22057]: Starting Sockets.
Jun  6 09:28:35 server systemd[22057]: Reached target Sockets.
Jun  6 09:28:35 server systemd[22057]: Starting Basic System.
Jun  6 09:28:35 server systemd[22057]: Reached target Basic System.
Jun  6 09:28:35 server systemd[22057]: Starting Default.
Jun  6 09:28:35 server systemd[22057]: Reached target Default.
Jun  6 09:28:35 server systemd[22057]: Startup finished in 59ms.
Jun  6 09:37:08 server systemd[1]: Reexecuting.

答案1

如果您需要編寫腳本,您應該考慮使用該systemctl show 命令。對於腳本來說,它比嘗試解析status.例如,要尋找服務上次啟動的時間,您可以使用:

$ systemctl show systemd-journald --property=ActiveEnterTimestamp
ActiveEnterTimestamp=Wed 2017-11-08 05:55:17 UTC

如果您想查看所有可用的屬性,只需省略該標誌,它就會將它們全部轉儲出來。

$ systemctl show <service_name>

可以找到這些屬性的文檔這裡

答案2

使用 Debian 上的預設配置,非特權使用者將無法存取 systemd-journald 和 syslog 日誌。如果以普通用戶身分登入,您將收到來自journalctl的以下回應:

$ journalctl 
No journal files were found.

這有點令人困惑。

如果您以 root 身分登錄,journalctl --unit=yourservice應該會提供您所需的資訊。在我的伺服器上之後systemctl restart bind9,我得到了這個journalctl --unit=bind9

Jun 03 18:20:24 ns systemd[1]: Stopping BIND Domain Name Server...
Jun 03 18:20:24 ns named[27605]: received control channel command 'stop'
Jun 03 18:20:24 ns systemd[1]: Starting BIND Domain Name Server...
Jun 03 18:20:24 ns systemd[1]: Started BIND Domain Name Server.

如果我用 , 明確殺死bind9 kill -9journalctl --unit=bind9給:

Jun 03 18:46:25 ns systemd[1]: bind9.service: main process exited, code=killed, status=9/KILL
Jun 03 18:46:25 ns rndc[28028]: rndc: connect failed: 127.0.0.1#953: connection refused
Jun 03 18:46:25 ns systemd[1]: bind9.service: control process exited, code=exited status=1
Jun 03 18:46:25 ns systemd[1]: Unit bind9.service entered failed state.
Jun 03 18:46:25 ns systemd[1]: bind9.service holdoff time over, scheduling restart.
Jun 03 18:46:25 ns systemd[1]: Stopping BIND Domain Name Server...
Jun 03 18:46:25 ns systemd[1]: Starting BIND Domain Name Server...
Jun 03 18:46:25 ns systemd[1]: Started BIND Domain Name Server.

第一行表明進程因被殺死而死亡。

systemd-journald 還將所有日誌訊息轉發到 syslog,因此您也應該在/var/log/syslog.

Systemd 和 systemd-journald 有一個預設編譯配置,可以在/etc/systemd/system.conf和中更改/etc/systemd/journald.conf

了解預設情況下 systemd-journald 將日誌儲存在 下可能會很有用/runtmpfs因此在重新啟動後會消失。這意味著為了取得早於上次啟動的日誌訊息,您必須查看系統日誌檔案。在這種情況下,journalctl 不會為您提供早於上次啟動的日誌。這可以/etc/systemd/journald.conf透過設定來更改Storage=persistent

記錄這一點的手冊頁是:

man 8 systemd-journald
man 5 journald.conf
man 5 systemd-system.conf
man 5 systemd-user.conf

另請注意,為了讓 systemd 自動重新啟動服務,必須在其.service檔案中進行設定。從man 5 systemd.service

   Restart=
       Configures whether the service shall be
       restarted when the service process exits, is
       killed, or a timeout is reached. The service
       process may be the main service process, but it
       may also be one of the processes specified with
       ExecStartPre=, ExecStartPost=, ExecStop=,
       ExecStopPost=, or ExecReload=. When the death
       of the process is a result of systemd operation
       (e.g. service stop or restart), the service
       will not be restarted. Timeouts include missing
       the watchdog "keep-alive ping" deadline and a
       service start, reload, and stop operation
       timeouts.

       Takes one of no, on-success, on-failure,
       on-abnormal, on-watchdog, on-abort, or always.
       If set to no (the default), the service will
       not be restarted.

答案3

您可以查看服務上次啟動或重新啟動的時間。使用service chatty statussystemctl status chatty.以下是 apache2 或 httpd 服務的範例:

# service apache2 status
● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2)
  Drop-In: /lib/systemd/system/apache2.service.d
       └─forking.conf
   Active: active (running) since ven. 2017-06-02 15:53:01 CEST; 21min ago
  Process: 14773 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
  Process: 22912 ExecReload=/etc/init.d/apache2 reload (code=exited, status=0/SUCCESS)
  Process: 14880 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/apache2.service

該行Active: active (running) since Wen. 2017-06-02 15:53:01 CEST; 21min ago顯示了服務如何運行,但我不知道您是否可以像“列表”一樣顯示您正在尋找的內容。

# systemctl status httpd

● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-10-11 00:35:58 EEST; 1 weeks 3 days ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 29728 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
 Main PID: 10722 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   Memory: 8.7M

相關內容