3

ubuntu16.04.1, systemd

An application depends on mongodb. The application and mongodb both start on boot as services. app.service containts next line:

After=network.target mongod.service

Unfortunately, the application fails (with connection to database error), I guess, because the mongodb needs some time to startup and be ready to connections (if I start the app later, everything is ok).

It looks like Type=notify (in [Service] section of mongod.service) will solve the problem if mongodb sends apropriate notification to systemd. But I did not find any documentation about it.

The queston is, how to autostart the application, after mongodb will be ready for incoming connections.

3 Answers 3

3

I ran into similar issue... I wanted to launch Mongo-Connector once MongoDB is ready to accept connection.

I found the fix after googling around a bit. The fix was to added the following ExecStartPost line after ExecStart line in mongodb unit file.

ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
ExecStartPost=/bin/sh -c 'while ! /usr/bin/mongo --eval "db.version()" > /dev/null 2>&1; do sleep 0.1; done'

This is tested on my Ubuntu 16.04 server successfully.

3

For future reference, I found this solution to work: https://gist.github.com/jwilm/5842956#gistcomment-2706797 That way mongodb blocks until the database is reachable before systemd starts the next service. And it does not involve a sleep.

If you have services that depend on mongodb, the config is slightly more difficult because the default mongodb service returns before the database is online. That can cause quick applications to try and fail to connect to the database. To prevent this, add the following to /etc/mongod.conf:

processManagement:
    fork: true

Next, create /etc/systemd/system/mongod.service.d/mongod.conf (which extends the existing /lib/systemd/system/mongod.service) with the following content:

[Service]
Type=oneshot
RemainAfterExit=yes

That causes sudo systemctl start mongod to start the server in a forked process and return only AFTER the database is up and ready for connections. This way, dependent services only get invoked after the db is live.

2
  • 1
    Welcome to Super User! Can you include the relevant steps from the solution here? :)
    – bertieb
    Sep 14, 2018 at 14:11
  • 1
    @bertieb I added the content from the link as blockquote :thumbsup: Sep 17, 2018 at 12:15
3

As seen in another answer, a good solution is:

  • tell mongodb server to fork. "That way mongodb blocks until the database is reachable"
  • tell systemd to wait for the fork

That is exactly what is done by default for RedHat (cf mongodb.conf & mongodb.service)

So let's do the same on debian/ubuntu:

  • create directory for pid file: install -d -o mongodb /run/mongodb
  • add to /etc/mongod.conf :
processManagement:
  fork: true
  pidFilePath: /run/mongodb/mongod.pid
  • add file /etc/systemd/system/mongod.service.d/processManagement-fork.conf
[Service]
PIDFile=/run/mongodb/mongod.pid
Type=forking

(NB: I created another answer since PIDFILE + Type=forking is better than RemainAfterExit + Type=oneshot)

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.