시스템 서비스가 시작/중지/다시 시작되는 시기를 확인하는 방법은 무엇입니까?

시스템 서비스가 시작/중지/다시 시작되는 시기를 확인하는 방법은 무엇입니까?

Debian(Jessie) 서버에서 실행되는 서비스(직접 작성)가 있고 서비스 자체 로그에 특정 시간에 다시 시작되었음을 나타냅니다. 세그폴트나 기타 충돌 징후가 없으므로 이제 응용 프로그램이 어떻게든 작동하는지 알아내려고 노력 중입니다.아무 말 않고실패하고 systemd에 의해 다시 생성되거나 사용자가 의도적으로 전달하는 경우 systemctl.

셸 기록에는 그러한 활동이 표시되지 않지만 export HISTCONTROL=ignorebothSSH 세션이 시간 초과되어 이전 로그인의 bash 기록이 디스크에 기록되지 않을 수 있으므로 이는 결론적이지 않습니다. 그 당시에는 서버가 다시 시작되지 않았습니다.

하지만 systemd 자체에서 서비스가 시작된 시기를 나타내는 로그를 유지했으면 좋겠습니다.고의로재시작. 놀랍게도 journalctl그러한 로그를 얻는 방법에 대한 문서를 찾을 수 없습니다(예:).

다른 게시물(예:일반 사용자 시스템 서비스는 어디에 있습니까? 로그가 없는 이유는 무엇입니까?) 다음과 같은 로그 메시지가 있어야 함을 나타내는 것 같습니다.

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.

하지만 내 시스템에는 그러한 로그 메시지가 표시되지 않습니다.

시스템 서비스가 언제 시작, 중지 또는 다시 시작되는지 알아내는 방법이 있습니까?

편집하다: 사람들이 겪을 수 있는 일반적인 문제는 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.

이것은 약간 혼란 스럽습니다.

루트로 로그인한 경우 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.

kill -9를 사용하여 명시적으로 바인딩9를 종료하면 journalctl --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.

/etc/systemd/system.confSystemd 및 systemd-journald에는 변경이 가능한 기본 컴파일 구성이 있습니다 /etc/systemd/journald.conf.

/run기본적으로 systemd-journald는 로그를 . 아래에 저장하므로 tmpfs재부팅 후에는 사라진다는 점을 알아두면 유용할 수 있습니다 . 이는 마지막 부팅보다 오래된 로그 메시지를 얻으려면 시스템 로그 파일을 확인해야 함을 의미합니다. 이 경우, 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 status또는 를 사용하십시오 systemctl 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

관련 정보