Short version:

Is there any simple way how to set up systemd so it runs a script or a unit when system boots after a crash ? And possibly have other units wait on its completion? I suppose I could write a unit that will set some sort of flag on shutdown and check that on boot but I was wondering whether systemd does not have something like that already implemented...

Longer version:

I ran into funny problem with postgresql not starting after crash because of PID files mismatch and I would like to delete the PID files before the postgresql services starts.

I have two instances of postgresql and as the boot process is cca same every time the server boots, the postmasters gets same PIDs every time - but in undefined order. And if there are stale PID files, sometimes one of the servers starts (clears its PID file) but the other sees its stale PID file and even postmaster process with that PID (the other instance) and it refuses to start...

Details: Fedora 20, systemd-208

Footnote: I know that it would be better to avoid the crashes but in this case its more important that the server runs than its data (I can replicate them from base server)

  • Your PID files are in the wrong location. They should be in subdirectories of /run and nowhere else. Placing them back here, where they belong, will also solve this problem. – Michael Hampton Aug 17 '15 at 15:49
  • That is actually not a bad idea. However the PID files are located in postgresql datadir and that seems to be a default location (not sure whether its Fedora default or Postgresql default) - there is no PID file related option in ps -ax and the program having trouble with them is pg_ctl itself... – Radek Hladík Aug 17 '15 at 18:36
  • If you can't move the pid files to the correct tmpfs location you could use a script, which tests the pid files and deletes them if invalid (no process or wrong cmdline) before systemd starts postgres. In order not to mess up the installed unit files place the invocation under /etc/systemd/system/<service-name>.d/pidfile-hack.conf (systemd.unit(1) has the details) into an ExecStartPre directive (systemd.service(1)). I guess checking pid files was common for sysvinit scripts, so you can probably just copy the the code from somewhere. Not sure whether I have seen the cmdline test, though. – Uwe Geuder Aug 25 '15 at 15:57
  • Thanks for your suggestion. I will probably do some sort of workaround like that. The only issue I am having is that the system is out-of-the-box Fedora, all I did was second unit file that inherits (hope it's the right systemd term) postgres unit and changes datadir so I am not sure how much "unofficial: solution I want to use... I will probably open bug in Fedora bugzilla and see what happens. – Radek Hladík Aug 26 '15 at 19:49

Your Answer

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

Browse other questions tagged or ask your own question.