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.

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.

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.

I guess the answer to this question is already at serverfault. In short: maybe you should use Requires instead of After. Requires means that your service requires the other to work properly, not that is should "just be started after".

https://serverfault.com/questions/812584/in-systemd-whats-the-difference-between-after-and-requires

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.