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. – phemmer Jun 23 '16 at 12:18@Wieland, thanks for pointing out to the already existing service file for this purpose, it gives me a clarity about where should i start from.
Patrick, i did play around with the User= parameter but found that the processes are coming up as owned by the 'root' user which is not what i wanted. I wanted the processes to be owned by the 'postgres' user or any other user which will own the PostgreSQL database directory.
– Nawaz Ahmed Jul 11 '16 at 23:54