System: ubuntu 04/16/1 ( systemd used to start services).

We have: an application service that depends on mongodb . The application and mongodb are launched automatically, at the start of ubuntu. The dependency is registered in the .service file of the application:

 After=network.target mongod.service 

In this case, probably due to the fact that mongodb is not ready to accept connections right after the start, the application crashes with the error "unable to connect to the database".

How is it better to perform a delay before the full start of mongodb, or in another way to determine the readiness of mongodb using the capabilities of systemd ?

  • This is not what you are looking for: unix.stackexchange.com/questions/247755/… ? - nobody
  • @nobody found this question, thanks. But in the Monga docks about the Type nothing is said, and ideally, of course, it would be to use Type=notify when the service itself sends a notification that it is ready. But again I did not find confirmation that Mong sends some notifications to systemd. - Vladimir Gamalyan

1 answer 1

In this case, use ExecStartPost and netcat:

 ExecStart= ExecStartPost=/bin/sleep 1s ExecStartPost=/usr/bin/nc -z host port 

Where host port is the mongodb port and address.

systemd will wait for all commands to execute before running dependent services.

If the start time can "float" will help:

 until [[ $(nc -z <host> <port>) -eq 1 ]]; do sleep 1s; done 

until will sleep until netcat returns -1.

ExecStartPre and ExecStartPost part of the startup life cycle of unita

ExecStartPre =, ExecStartPost =

Additional commands that are executed before or after the command in ExecStart =, respectively. It is a syntax for serial code.

It is considered that it has failed.

ExecStart = commands are only run after all ExecStartPre = commands that were not prefixed with a "-" exit successfully.

If you haven’t been successful, you’ll be able to get it. = forking, "READY = 1" is sent for Type = notify, or the BusName = has been taken for Type = dbus).

Note: ExecStartPre = may not be used to start long-running processes. All processes forked off by processes invoked via ExecStartPre = will be killed.

ExecStartPre =, ExecStart =, or ExecStartPost = (see above) or ExecStartPrePreader = , the commands in ExecStop = are skipped.

If between units the dependency is Before or After Systemd postpones the start of the dependent service until another starts:

Before =, After =

A space-separated list of unit names. Configures ordering dependencies between units. If you’re not ready, you’ll have started, you’ll have it, It requires that as it is not approved. It is configured with these options. This option may have been created more than once. If you have been configured, you must always be able to. The order of the order is applied. If you are shut down. If there is a need for a unit, it should be noted that it has been ordered before the start-up. It doesn't matter if the ordering dependency is After = or Before =. It also doesn’t matter. The shutdown is ordered before the start-up in all cases. If two units have no ordering dependencies, it takes no ordering.

  • Is it necessary to add this to the Mongovsky .service file? - Vladimir Gamalyan
  • Yes - in the unit file mongodb. If this unit is in / usr, then you can write a drop-in (So when updating mongodb, the changes will not be lost). - Alex
  • In the first example, the line ExecStartPost=/usr/bin/nc -z host port is for insurance? (as far as I understood, the sense is in a pause of 1 second .) - Vladimir Gamalyan
  • In the first example, wait a second for insurance. If during this time mongodb does not start listening on the port, netcat will return -1 and the unit will go to the error state. If the start time is always different, it is better to use the until command. Using it, the unit will be able to start up until: mongodb starts listening on the port, time expires on the unit’s start; - Alex
  • Understood thanks. You can still poke into the documentation, I can not find confirmation that subsequent dependent services start after a successful ExecStartPost (and not immediately after ExecStart). - Vladimir Gamalyan