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.


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.


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:

    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:


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.

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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