systemctl do something between stop and start during restart or kill

2

I have a C++ program that runs as a non-daemon (but could be changed), and I'm controlling it with systemd. That works. Albeit with the ugly that to restart it I need to either be root (to use systemctl restart) or kill -9 (since it runs as me). I'm running Centos 6.

What I would like to do when the program restarts is zip up and remove the log files. I have the zipping part working, but the log files are often locked while the program runs, and I can't work out how to get my service file to:

  1. stop the program
  2. zip the log files
  3. start the program

My listener.service file looks like this:

[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

Whether I use the sleep commands, the RestartSec commands or leave them both out I get a /var/log/messages sequence like this (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                        ***

Lines marked with *** are output from my program. Note the log timestamps) and my program says it shut down then immediately restarted, with the ExecStopPost and ExecStartPre commands only happening after the startup.

I hope there's something obvious that I'm missing here.

Code Abominator

Posted 2015-09-01T22:19:32.393

Reputation: 148

FWIW my eventual solution to this was to make the "listener" program create a directory and move the old log files into that before it starts logging. This isn't ideal but it works better than any of my other ideas. – Code Abominator – 2017-01-12T00:43:14.870

No answers