创建用于在系统启动时更改网络参数的服务出现问题

创建用于在系统启动时更改网络参数的服务出现问题

我创建了一个脚本,在系统启动时将网卡设置为1G。我为此创建了一项新服务,但它不起作用。

我执行启动并收到此错误,并且网络速度没有改变。

$ systemctl start service activar_a_1G_enp3s0.service
Failed to start service.service: Unit service.service not found.
$ 

这是我的 .service 文件(我已经用 3 之后我听到了)

cat activar_a_1G_enp3s0.service
[Unit]
Description=Activa1GRed
#After=multi-user.target
#After=network.target 
After=NetworkManager.service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/activar_a_1G_enp3s0.sh

[Install]
WantedBy=default.target

sh 文件是(如果我手动执行它工作正常)

$ ls -la /usr/local/bin/activar_a_1G_enp3s0.sh
-rwxr-xr-x. 1 root root 433 nov 22 22:43 /usr/local/bin/activar_a_1G_enp3s0.sh

$ cat /usr/local/bin/activar_a_1G_enp3s0.sh
#!/bin/bash

# Cambiamos la velocidad de la tarjeta a 1Gb
/usr/sbin/ethtool -s enp3s0 autoneg on 

编辑:

systemctl启动activar_a_1G_enp3s0.service服务手动工作正常,但当我重新启动机器时,它不起作用,并且出现此错误

$ systemctl status activar_a_1G_enp3s0.service
● activar_a_1G_enp3s0.service - Activa1GRed
     Loaded: loaded (/etc/systemd/system/activar_a_1G_enp3s0.service; enabled; vendor preset: disabled)
     Active: inactive (dead) since Mon 2020-11-23 02:34:11 CET; 3min 4s ago
    Process: 1185 ExecStart=/usr/local/bin/activar_a_1G_enp3s0.sh (code=exited, status=0/SUCCESS)
   Main PID: 1185 (code=exited, status=0/SUCCESS)
        CPU: 11ms

nov 23 02:34:05 aqua systemd[1]: Starting Activa1GRed...
nov 23 02:34:06 aqua activar_a_1G_enp3s0.sh[1189]: netlink error: failed to retrieve link settings
nov 23 02:34:06 aqua activar_a_1G_enp3s0.sh[1189]: netlink error: No such device
nov 23 02:34:11 aqua systemd[1]: activar_a_1G_enp3s0.service: Succeeded.
nov 23 02:34:11 aqua systemd[1]: Finished Activa1GRed.

如何将我的服务的执行延迟到网络恢复正常为止?

编辑2:

我已按照您的指示进行操作,启动已停止 2 分钟等待网络。之后系统已启动,但 1G 配置不起作用。速度为100Mb/s开机后。

    Settings for enp3s0:
...
        Speed: 100Mb/s
        Duplex: Half
...
        Link detected: yes
enp3s0: 100 Mbit, half duplex, link ok

启动后我手动运行服务的启动

    $ systemctl start activar_a_1G_enp3s0.service
    $ systemctl status activar_a_1G_enp3s0.service
    ● activar_a_1G_enp3s0.service - Activa1GRed
     Loaded: loaded (/etc/systemd/system/activar_a_1G_enp3s0.service; enabled; vendor preset: disabled)
     Active: inactive (dead) since Mon 2020-11-23 23:06:53 CET; 3s ago
    Process: 5891 ExecStart=/usr/local/bin/activar_a_1G_enp3s0.sh (code=exited, status=0/SUCCESS)

    nov 23 23:06:48 aqua systemd[1]: Starting Activa1GRed...
    nov 23 23:06:53 aqua systemd[1]: activar_a_1G_enp3s0.service: Succeeded.
    nov 23 23:06:53 aqua systemd[1]: Finished Activa1GRed.

现在我有了正确的速度(1000Mb/秒)

Settings for enp3s0:
...
        Speed: 1000Mb/s
        Duplex: Full
...
        Link detected: yes
enp3s0: negotiated 1000baseT-FD flow-control, link ok

       Main PID: 5891 (code=exited, status=0/SUCCESS)
            CPU: 11ms
    

这是 enp3s0 的 dmesg 命令的输出

$ dmesg|grep enp3s0
[    2.168785] r8169 0000:03:00.0 enp3s0: renamed from eth0
[    4.401733] systemd[1]: Configuration file /etc/systemd/system/activar_a_1G_enp3s0.service is marked world-inaccessible. This has no effect as configuration data is accessible via APIs without restrictions. Proceeding anyway.
[    9.127366] r8169 0000:03:00.0 enp3s0: Link is Down
[   11.801640] r8169 0000:03:00.0 enp3s0: Link is Up - 1Gbps/Full - flow control off
[   11.801647] IPv6: ADDRCONF(NETDEV_CHANGE): enp3s0: link becomes ready
[  129.159287] r8169 0000:03:00.0 enp3s0: Link is Down
[  131.368490] r8169 0000:03:00.0 enp3s0: Link is Up - 100Mbps/Half - flow control off
[  414.549164] r8169 0000:03:00.0 enp3s0: Link is Down
[  417.332359] r8169 0000:03:00.0 enp3s0: Link is Up - 1Gbps/Full - flow control off

谢谢

答案1

systemctl start service activar_a_1G_enp3s0.service


                ^^^^^^^

这就是你的问题——解决掉它service

Edit1:尝试帮助满足更改后的需求。

请尝试以下操作:

systemctl enable systemd-networkd.service systemd-networkd-wait-online.service

并在您的单元文件中添加:

After=systemd-networkd-wait-online.service
Wants=systemd-networkd-wait-online.service

当然,我不能保证这有效 - systemd 下的事件顺序可能有点变化无常。

相关内容