시스템 시작-중지-재로드 syslog 메시지를 파일로 보내는 방법

시스템 시작-중지-재로드 syslog 메시지를 파일로 보내는 방법

특정 파일에 systemd 시작/중지/다시 로드 메시지를 보내고 싶습니다. 특히, mysqld는 데몬 시작/중지 메시지를 파일에 보내지 않는 것 같아서 log-error로그에 성공/실패를 나타내는 내용을 담고 싶습니다.

나는 현재 및 을 조작하고 있지만 ExecStartPostExecStopPost좋은 방법이 있을 것입니다.영감은 여기에 있습니다.

답변1

이것은 다음과 잘 작동하는 것 같습니다 /lib/systemd/system/mysql.service.

[Service]
ExecStart=/usr/sbin/mysqld --daemonize
ExecStartPost=/bin/bash -c "DATE=$(date --iso-8601=seconds); \
    PID=$(/usr/bin/systemctl show -p MainPID mysql |sed 's/MainPID=//'); \
    /usr/bin/echo $${DATE} '[systemd] Starting MySQL Server... PID:' $${PID} \
    >> /var/log/error-mysql.log"
ExecStopPost=/bin/bash -c "DATE=$(date --iso-8601=seconds); \
    PID=$(/usr/bin/systemctl show -p MainPID mysql |sed 's/MainPID=//'); \
    /usr/bin/echo $${DATE} '[systemd] Stopping MySQL Server... PID:' $${PID} \
    >> /var/log/error-mysql.log"

산출:

2022-03-18T08:59:18-04:00 [systemd] Starting MySQL Server... PID: 1166
2022-03-18T08:59:28-04:00 [systemd] Stopping MySQL Server... PID: 0
2022-03-18T08:59:46-04:00 [systemd] Starting MySQL Server... PID: 1216
2022-03-18T08:59:58-04:00 [systemd] Stopping MySQL Server... PID: 0
2022-03-18T08:59:59-04:00 [systemd] Starting MySQL Server... PID: 1263

관련 정보