1

Can someone let me know how can I make a systemd service to be stopped first before the unmounting of the file system starts during the shutdown of a host?

Unit File looks like below

[Unit]
Description=FDB Service
After=network-online.target
Wants=network-online.target

[Service]
Environment=FDB_PID_DIR=/var/run/fdb/
Environment=LOCKFILE=/var/run/fdb/fdbmonitor.pid

Type=simple
User=ubuntu
Group=ubuntu
IgnoreSIGPIPE=false

ExecStart=/bin/bash /home/ubuntu/build-target/fdb/fdb-sysd-start.sh
ExecStopPost=-/home/ubuntu/build-target/fdb/fdb-sysd-poststop.sh

RestartSec=2s
Restart=always

[Install]
WantedBy=multi-user.target
  • OS - Ubuntu 16.04.6
tuk
  • 293
  • 4
  • 16
  • 2
    This should happen automatically unless you've created some oddball filesystems, remote mounts, or something else strange. What are your filesystems? And what is the unit file of the affected service? – Michael Hampton Jun 16 '20 at 19:59
  • Ok but as per http://manpages.ubuntu.com/manpages/xenial/man8/systemd-halt.service.8.html second para in the description, it states that unmount filesystem and then kill all services. – tuk Jun 17 '20 at 06:35
  • huh? That man page doesn't say anything like that. It's also not relevant because you don't call that service directly. It is called by systemd itself after all services are stopped. – Michael Hampton Jun 17 '20 at 17:08

1 Answers1

1

It is almost certainly a problem with your service (fdb). systemd will first try to stop all services and then do the unmount. Make sure your service stops in a reasonable time - if it needs more time to stop gracefully, you may set TimeoutStopSec= to a higher value. Also, make sure Type=simple is appropriate, from what it looks Type=forking might be better. When switching to Type=forking make use of PID file by adding PIDFile=/var/run/fdb/fdbmonitor.pid - if it actually contains the main process PID.

If you are using network-mounted shares, systemd should detect them and unmount before shutting down the network. If that does not happen, add _netdev mount option to tell systemd this is actually a network share.

user999441
  • 26
  • 3
  • Can you explain why Type=forking will be better? – tuk Jun 18 '20 at 03:25
  • 1
    See https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type= `[...] The parent process is expected to exit when start-up is complete and all communication channels are set up. The child continues to run as the main service process, and the service manager will consider the unit started when the parent process exits. This is the behavior of traditional UNIX services. [...]` This looks as if this .sh script starts some other process, that's why I took a guess – user999441 Jun 18 '20 at 11:34