I have a local postgresql on each machine. I want to run maintenance scripts on shutdown, so users can click "Turn off" and go away while the machine does the maintenance and then actually shutdown when done.
The script must run before postgresql.service stops.
I also want to display a shutdown message informing the user that where will be a maintenance.
I have tried to create a service:
[Unit]
Description=PostgreSQL Maintenance
Requires=postgresql.service
Before=shutdown.target
[Service]
User=postgres
WorkingDirectory=/opt/postgres
ExecStart=/bin/true
ExecStop=/opt/postgres/run-maintenance.sh
[Install]
WantedBy=multi-user.target
and:
[Unit]
Description=PostgreSQL Maintenance
Requires=postgresql.service
[Service]
Type=oneshot
User=postgres
WorkingDirectory=/opt/postgres
ExecStart=/opt/postgres/run-maintenance.sh
[Install]
WantedBy=halt.target shutdown.target
They do not work.
The maintenance script is:
#!/usr/bin/env bash
plymouth display-message --text="Maintenance Message"
psql -d db -f /opt/postgres/maintenance.sql
maintenance.sql
reindex (verbose) database db;
vacuum (full, analyze, verbose);
I have found many similar questions, but did not find a definitive solution.
Before=shutdown.target reboot.target halt.target
in[Unit]
It might be halt that is used to shutdown Oh and maybe addRemainAfterExit=true
in[Service]
– Rinzwind Jul 31 '19 at 14:55