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


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, 2018 at 14:11
  • 1
    @bertieb I added the content from the link as blockquote :thumbsup: Sep 17, 2018 at 12:15

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 :
  fork: true
  pidFilePath: /run/mongodb/mongod.pid
  • add file /etc/systemd/system/mongod.service.d/processManagement-fork.conf

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


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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .