I am developing a systemd shutdown script (unit file) for a postgresql database and I followed the below two posts but could not get it to work.
How to run a script with systemd right before shutdown?
Here is what I am doing with my postgresql installation: I have written a startup script that starts the postgresql database cluster at boot time but the shutdown script shown below does not seem to work at shutdown time. While it works fine if I execute it with systemctl start service_name.
When I looked at the database log, I could see a message "LOG: received smart shutdown request" which is the message when database cluster goes down in normal mode which is what the shutting down operating system does when I shutdown the host with a "shutdown -h now". What I want to see is "LOG: received fast shutdown request" which is a fast (immediate mode) shutdown which occurs when I run the script with a systemctl start service_name
Here is the unit file definition
[Unit]
Description=Database Shutdown
Before=shutdown.target reboot.target halt.target
DefaultDependencies=no
[Service]
ExecStart=/usr/bin/su - postgres -c "/opt/postgres-95/bin/pg_ctl -D /database/inst1/data -w stop &"
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=shutdown.target
I also tried to ditch the [Install]
section but the systemctl would not let me enable the service without the [Install]
section. could someone please point me in the right direction.
ExecStop
would then do what you want without any hackery. Also, you shouldn't usesu
in a unit script, this is what theUser=
parameter is for.