CodexBloom - Programming Q&A Platform

implementing systemd service dependencies causing delayed start on Arch Linux

πŸ‘€ Views: 65 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-19
linux systemd postgresql bash

I've been struggling with this for a few days now and could really use some help. I just started working with I've searched everywhere and can't find a clear answer... I'm experiencing a question with a custom systemd service on Arch Linux that seems to start too late, which causes it to unexpected result to connect to a PostgreSQL database at boot. The service is meant to start after the network is available, but it appears to be delaying due to its dependency on another service that hasn't fully initialized yet. Here’s the relevant part of my service file located at `/etc/systemd/system/my-service.service`: ```ini [Unit] Description=My Custom Service After=network.target postgresql.service Requires=postgresql.service [Service] ExecStart=/usr/local/bin/my-custom-script Restart=on-failure [Install] WantedBy=multi-user.target ``` I’ve confirmed that the PostgreSQL service is running by the time my service attempts to start, but it seems that my service is not able to connect to the database because it tries to run immediately after the network comes up rather than waiting for PostgreSQL to be fully ready. I’ve tried adding `Wants=postgresql.service` and `WantedBy=postgresql.service` but it still doesn't work as expected. I've also checked the logs using `journalctl -u my-service` and I see errors like `database connection failed: could not connect to server: Connection refused`. Is there a way to ensure that my service waits until PostgreSQL is fully ready to accept connections? Any advice on how to handle service dependencies better in systemd would be greatly appreciated. My development environment is Linux. Any ideas what could be causing this? What am I doing wrong? I'm on Ubuntu 20.04 using the latest version of Bash. What's the best practice here? I've been using Bash for about a year now. I'd really appreciate any guidance on this.