systemctl 和 Oracle Linux 9 和curl

systemctl 和 Oracle Linux 9 和curl

我有一个 Oracle Linux 9(这是一个 Red Hat“克隆”),并且有一个 systemctl 服务,该服务应该在启动时执行curl。我收到此错误:

× gitlab-runner-curl.service - gitlab runner curl
     Loaded: loaded (/etc/systemd/system/gitlab-runner-curl.service; enabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Tue 2023-05-09 16:51:31 CEST; 27s ago
    Process: 998 ExecStart=/usr/bin/curl -v -k --fail -F token=glptt-35a1ab49139cf38005a38d547ae614cba7c28862 -F ref=acc-mi-1 https>
   Main PID: 998 (code=exited, status=6)
        CPU: 23ms

May 09 16:51:31 serverx systemd[1]: Starting gitlab runner curl...
May 09 16:51:31 serverx curl[998]:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
May 09 16:51:31 serverx curl[998]:                                  Dload  Upload   Total   Spent    Left  Speed
May 09 16:51:31 serverx curl[998]: [121B blob data]
May 09 16:51:31 serverx curl[998]: * Closing connection 0
May 09 16:51:31 serverx curl[998]: curl: (6) Could not resolve host: servery
May 09 16:51:31 serverx systemd[1]: gitlab-runner-curl.service: Main process exited, code=exited, status=6/NOTCONFIGURED
May 09 16:51:31 serverx systemd[1]: gitlab-runner-curl.service: Failed with result 'exit-code'.
May 09 16:51:31 serverx systemd[1]: Failed to start gitlab runner curl

所以显然它是在网络启动之前执行的(因为错误curl: (6) Could not resolve host: servery)。为什么是这样?这是我的服务文件:

[Unit]
Description=gitlab runner curl
After=gitlab-runner.service network.target

[Service]
Type=oneshot
User=gitlab-runner
ExecStart=/usr/bin/curl -v -k --fail -F token=xxxxxx -F ref=acc-mi-1 https://servery/api/v4/projects/128/trigger/pipeline
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

正如您所看到的,该服务应该在 network.start 之后启动。 curl 语句在 shell 中完美运行。

问候,

伊万

答案1

我不相信After=network.target要等到网络上线后才使用。它会导致服务在启​​动后随时运行network.target,但不一定在网络在线且可用于连接时运行。

根据network*.target有关各个单位的文档您可能想network-online.target在您的服务中使用:

[Unit]
Description=gitlab runner curl
After=gitlab-runner.service network-online.target
Wants=network-online.target

相关内容