Ubuntu 22.04 下 IGH EtherCAT 主站永久性开机自启

一、前置准备:清理旧配置(避免冲突)

先移除之前通过update-rc.d/systemd 设置的无效自启配置,确保仅保留rc.local的自启逻辑:

1. 移除 SysVinit 风格的自启软链接

复制代码
sudo gedit /etc/rc.local
  1. 写入完整自启脚本(复刻手动启动逻辑)

    #!/bin/bash

    延迟20秒:确保开机后内核、网卡、网络全就绪(可根据机器调整)

    sleep 20

    核心操作:启动EtherCAT主站(与手动执行的命令一致)

    /etc/init.d/ethercat start

    兜底:验证/dev/EtherCAT0是否创建,未创建则手动补(极端情况防护)

    if [ ! -c /dev/EtherCAT0 ]; then
    # IGH EtherCAT主站设备号固定:主240,次0
    sudo mknod /dev/EtherCAT0 c 240 0
    sudo chmod 666 /dev/EtherCAT0
    fi

    记录开机日志(方便排查问题)

    echo "EtherCAT boot success - $(date)" >> /var/log/ethercat_boot.log
    lsmod | grep ec_ >> /var/log/ethercat_boot.log
    ls /dev/EtherCAT0 >> /var/log/ethercat_boot.log 2>&1

    exit 0

  2. 添加执行权限(必须)

    sudo chmod +x /etc/rc.local

    验证权限(输出含x=成功)

    ls -l /etc/rc.local

三、核心步骤 2:修复 rc-local.service(实现永久启用)

补充[Install]段,解决 Ubuntu 22.04 默认rc-local.service"临时启用(enabled-runtime)" 的问题:

1. 备份并编辑rc-local.service配置

复制代码
# 备份原配置(防止出错)
sudo cp /lib/systemd/system/rc-local.service /lib/systemd/system/rc-local.service.bak

# 编辑配置文件
sudo nano /lib/systemd/system/rc-local.service
  1. 写入完整生效配置

    SPDX-License-Identifier: LGPL-2.1-or-later

    This file is part of systemd.

    systemd is free software; you can redistribute it and/or modify it

    under the terms of the GNU Lesser General Public License as published by

    the Free Software Foundation; either version 2.1 of the License, or

    (at your option) any later version.

    This unit gets pulled automatically into multi-user.target by

    systemd-rc-local-generator if /etc/rc.local is executable.

    [Unit]
    Description=/etc/rc.local Compatibility
    Documentation=man:systemd-rc-local-generator(8)
    ConditionFileIsExecutable=/etc/rc.local
    After=network.target

    [Service]
    Type=forking
    ExecStart=/etc/rc.local start
    TimeoutSec=0
    RemainAfterExit=yes
    GuessMainPID=no

    [Install]
    WantedBy=multi-user.target

  2. 重新加载配置并永久启用

    重新加载systemd配置

    sudo systemctl daemon-reload

    先取消临时启用,再永久启用

    sudo systemctl disable rc-local
    sudo systemctl enable rc-local

    验证启用状态(输出enabled=永久生效)

    sudo systemctl is-enabled rc-local

  3. 重启 rc-local 服务(使配置生效)

    sudo systemctl restart rc-local

    验证服务状态(输出active=成功)

    sudo systemctl status rc-local

验证永久性启用

复制代码
# 1. 检查rc-local启用状态(永久enabled)
sudo systemctl is-enabled rc-local

# 2. 检查开机日志(确认自启执行)
cat /var/log/ethercat_boot.log
相关推荐
小稻穗5 分钟前
市场调研样本选型坐标系:2026六家国内样本平台能力横向校验
大数据·运维·数据分析
栩栩云生14 分钟前
AI 写代码犯的错,早被写进了错题集
linux·安全·ai编程
AOwhisky26 分钟前
Python 学习笔记(第十三期)——运维自动化(下·前篇):远程命令执行——paramiko基础篇
运维·python·学习·云原生·自动化·运维开发·paramiko
AOwhisky1 小时前
Python 学习笔记(第十四期)——运维自动化(下·中篇):远程文件传输——paramiko进阶篇
运维·python·学习·云原生·自动化·文件传输·paramiko
imc.111 小时前
linux基础IO
linux·运维·服务器
躺不平的理查德1 小时前
让Ubuntu 的第二块网卡 ens37 永久固定为 192.168.30.111
运维·服务器
bitbrowser2 小时前
Facebook 验证反复回到原页面,应该从哪里排查
运维·服务器·facebook
Land03292 小时前
AI网页元素变化无法自动修复?自带元素自愈的自动化解决方案
运维·人工智能·ai·自动化·rpa
fengyehongWorld2 小时前
Jenkins 安装与简单配置
运维·jenkins
云智慧AIOps社区2 小时前
2026 国产化 ITSM 替代指南:横向测评 ServiceNow、轻帆云、Jira等五款主流IT服务管理平台
运维·人工智能·运维开发·it服务管理·itsm平台