systemctl 在重启或终止期间在停止和启动之间做一些事情

systemctl 在重启或终止期间在停止和启动之间做一些事情

我有一个以非守护进程运行的 C++ 程序(但可以更改),我用 systemd 控制它。这样就行了。尽管要重新启动它,我需要成为 root(使用 systemctl restart)或 kill -9(因为它以我的身份运行),但情况很糟糕。我正在运行 Centos 6。

我想在程序重新启动时压缩并删除日志文件。压缩部分可以正常工作,但程序运行时日志文件经常被锁定,我不知道如何获取我的服务文件:

  1. 停止程序
  2. 压缩日志文件
  3. 启动程序

我的 listener.service 文件如下所示:

[Unit]
Description=Listener - main Digiflex server program
# start only once the network subsystems are available
After=network.target

[Service]
Type=simple
# ExecStartPre commands are executed in order given
ExecStartPre=-/usr/bin/echo "ExecStartPre first"
ExecStartPre=-/usr/bin/bash -c "/usr/bin/zip -D -9 -x*.zip -x*.sql -x*.gz /code/logs/%H-$(TZ=Australia/Sydney date +%Y-%%m-%d__%%H.%M.%S ).zip /code/logs/*"
#ExecStartPre=-/usr/bin/echo "ExecStartPre before sleep"
#ExecStartPre=-/usr/bin/sleep 1
ExecStartPre=-/usr/bin/echo "ExecStartPre last"

ExecStart=/code/listener

ExecStartPost=-/usr/bin/echo "ExecStartPost"
#ExecStopPost=-/usr/bin/echo "ExecStopPost before sleep"
#ExecStopPost=-/usr/bin/sleep 1
ExecStopPost=-/usr/bin/echo "ExecStopPost"

# keep alive - always restart it if it dies
Restart=always
# wait 5 seconds between restarts
RestartSec=5
# wait 5s for start and stop
TimeoutSec=10

[Install]
WantedBy=multi-user.target

无论我使用 sleep 命令、RestartSec 命令还是省略它们,我都会得到这样的 /var/log/messages 序列 (sudo grep listener -b5 -a5 /var/log/messages):

270521-Sep  1 06:24:16 dfxmyalarmsqlb1 systemd: Reloading.
270715-Sep  1 06:24:16 dfxmyalarmsqlb1 systemd: Stopping Listener - main Digiflex server program...
270808:Sep  1 06:24:16 dfxmyalarmsqlb1 listener: stopping...!
270809:Sep  1 06:24:16 dfxmyalarmsqlb1 listener: Listener shut down complete  build date 20150901 commit 1,474       ***
270924:Sep  1 06:24:16 dfxmyalarmsqlb1 listener: Listener starting: build date 20150901 commit 1,474                 ***
271018-Sep  1 06:24:16 dfxmyalarmsqlb1 echo: ExecStopPost before sleep
271082-Sep  1 06:24:17 dfxmyalarmsqlb1 echo: ExecStopPost
271133-Sep  1 06:24:17 dfxmyalarmsqlb1 systemd: Starting Listener - main Digiflex server program...
271226-Sep  1 06:24:17 dfxmyalarmsqlb1 echo: ExecStartPre first
271283:Sep  1 06:24:17 dfxmyalarmsqlb1 bash: adding: code/logs/listener-app-forwarded-errors.log (stored 0%)
271385:Sep  1 06:24:17 dfxmyalarmsqlb1 bash: adding: code/logs/listener-errors.log (deflated 93%)
271476:Sep  1 06:24:17 dfxmyalarmsqlb1 bash: adding: code/logs/listener.log (deflated 94%)
271560:Sep  1 06:24:17 dfxmyalarmsqlb1 bash: adding: code/logs/listener-packets.log (deflated 97%)
271652:Sep  1 06:24:17 dfxmyalarmsqlb1 bash: adding: code/logs/listener-quickfail.log (stored 0%)
271743:Sep  1 06:24:17 dfxmyalarmsqlb1 bash: adding: code/logs/listener-skinny-polls.log (stored 0%)
271837:Sep  1 06:24:17 dfxmyalarmsqlb1 bash: adding: code/logs/listener-zero-sesson.log (stored 0%)
271930-Sep  1 06:24:17 dfxmyalarmsqlb1 echo: ExecStartPre before sleep
271994-Sep  1 06:24:18 dfxmyalarmsqlb1 echo: ExecStartPre last
272050-Sep  1 06:24:18 dfxmyalarmsqlb1 echo: ExecStartPost
272102-Sep  1 06:24:18 dfxmyalarmsqlb1 systemd: Started Listener - main Digiflex server program.
272192:Sep  1 06:24:18 dfxmyalarmsqlb1 listener: logfile config from /code/listener.zlog.conf                        ***

标有 *** 的行是我的程序的输出。请注意日志时间戳)并且我的程序显示它已关闭然后立即重新启动,ExecStopPost 和 ExecStartPre 命令仅在启动后发生。

我希望这里我遗漏了一些明显的东西。

相关内容