Starting systemd service after network-online.target but DNS is still not available

Question related to starting a systemd service after the network is online, even when DNS may not be available immediately. If you're facing a similar issue, here are some possible solutions:


1. **Wait for DNS Resolution in Your Service Script**:

   Inside your systemd service unit, you can specify `ExecStartPre` to execute a script before your service starts. You can use this to check for DNS availability by trying to resolve a well-known domain name and waiting until it succeeds. Here's an example of what it could look like:


   ```ini

   [Service]

   ExecStartPre=/path/to/script.sh

   ExecStart=/your/actual/service

   ```


   In the `script.sh`, you can use a loop to check for DNS resolution:


   ```bash

   #!/bin/bash

   while ! host -t A google.com; do

     sleep 1

   done

   ```


2. **Use `After` and `Wants` Directives**:

   You can adjust your systemd service unit by adding `After` and `Wants` directives to make your service depend on network-online.target but not directly on network.target. This can help ensure that your service starts after the network is online but doesn't necessarily require DNS to be fully functional:


   ```ini

   [Unit]

   Wants=network-online.target

   After=network-online.target

   ```


3. **Custom Network Check**:

   If the `network-online.target` does not suit your requirements, you can create a custom network check service and make your service depend on it. This custom service can incorporate more specific checks, including DNS resolution.


   For example, you could create a custom service that runs a script similar to the one mentioned in option 1 to verify network availability.


4. **DNS Service Dependency**:

   You can make your service depend explicitly on a DNS service. However, this approach assumes that you have a DNS service unit defined in your systemd setup. Here's an example of how your service unit might look:


   ```ini

   [Unit]

   After=network-online.target

   Wants=network-online.target

   After=dns.service


   [Service]

   ExecStart=/your/actual/service

   ```


The approach you choose may depend on your specific requirements and how your system is configured. Options 1 and 2 are more general approaches and should work on most systemd-based systems. Options 3 and 4 require more customization and may depend on the specifics of your environment.

Comments

Popular posts from this blog

bad character U+002D '-' in my helm template

GitLab pipeline stopped working with invalid yaml error

How do I add a printer in OpenSUSE which is being shared by a CUPS print server?