2

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?

https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemd

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.

  • You could just use one of the existing postgresql service files (for example the one from Arch Linux or Debian) instead of reinventing your own one. Also, having a service file just to shut down a service is not correct, you should have one service file that handles starting, stopping and reloading the service – Wieland Jun 23 '16 at 11:19
  • Your stop command looks like you're trying to shut something down. You'd likely be much better off going with standard practices of using a unit file to manage the whole service, both start and stop, not just stopping it. ExecStop would then do what you want without any hackery. Also, you shouldn't use su in a unit script, this is what the User= parameter is for. – Patrick Jun 23 '16 at 12:18
  • sorry for the late reply folks, i got tied up and juggling around other things @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
1

systemd is designed to cleanly handle this case. You can do so by creating a small file specifies a command you want to run as part of stopping the PostgreSQL service. First, create a directory named:

/etc/systemd/system/postgresql.d/

In it, create a file with any name that ends in .conf, like custom-shutdown.conf. Add to that file a couple lines like this:

[Service]
ExecStop=/usr/bin/su - postgres -c "/opt/postgres-95/bin/pg_ctl -D /database/inst1/data -w stop &" 

You may wish to use ExecStopPost= depending your case.

See official docs for Extending the Default Configuration and also official docs for the ExecStop= and ExecStopPost=.

  • 1
    thanks for the response, I shall try these steps and get back you all. Thank you all for your help guys, I appreciate it. – Nawaz Ahmed Jul 11 '16 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.