Search code examples
linuxdebiansystemd

Debian systemd service starts before network is ready


OS: Debian Jessie

I want to update git repository in user folder each time system start.

I tried this with cron @reboot entry. Cron started too early and in result mailed me with "SSH: Could not resolve hostname ..."

Then I tried SysV init scripts. Effect - the same.

Currently I'm trying systemd services with the same faulty reports.

/usr/bin/git_repo

#! /bin/sh
# Description:       Updates local git repository with latest content
#cd /home/tanglor/repo
su -c'cd repo;git pull' - tanglor

/etc/systemd/system/repo.service

[Unit]
Description = Updates local git repository with latest content
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/bin/repo
[Install]
WantedBy=multi-user.target

Still no result. I mean in syslog I find:

Sep 30 19:41:59 Khlavan repo[422]: ssh: Could not resolve hostname bitbucket.org: Name or service not known
Sep 30 19:41:59 Khlavan repo[422]: fatal: Could not read from remote repository.
Sep 30 19:41:59 Khlavan repo[422]: Please make sure you have the correct access rights
Sep 30 19:41:59 Khlavan repo[422]: and the repository exists.
Sep 30 19:41:59 Khlavan systemd[1]: repo.service: main process exited, code=exited, status=1/FAILURE
Sep 30 19:41:59 Khlavan systemd[1]: Failed to start Updates local git repository with latest content.
Sep 30 19:41:59 Khlavan systemd[1]: Unit repo.service entered failed state.

How to achieve repository updates with each system start?

One solution is to use script with sleep command which will wait till some ssh test will succeed and then complete. But this is the last resort, I would like to solve this by-the-book.


Solution

  • You should probably check systemd documentation for the solution that suits you best, but here are few links that may help you.

    Many network management solutions provide a way to unconditionally pull in network-online.target, and thus upgrading the effect of network.target to the effect of network-online.target.

    If you use NetworkManager you can do this by enabling NetworkManager-wait-online.service:

    systemctl enable NetworkManager-wait-online.service

    If you use systemd-networkd you can do this by enabling systemd-networkd-wait-online.service:

    systemctl enable systemd-networkd-wait-online.service

    This will ensure that all configured network devices are up and have an IP address assigned before boot continues. This service will time out after 90s. Enabling this service might considerably delay your boot even if the timeout is not reached. Both services are disabled by default.

    Alternatively, you can change your service that needs the network to be up, to include

    After=network-online.target
    Wants=network-online.target
    

    (freedesktop.org, 2015):

    Check for the Before=, After= selection options in man pages. These are among those options to change the order the Units are launched by the systemd and then select after which units you have to launch yours.


    Hope this will help you if not to solve your problem, but to find the way to solution.