OpenPLC在PREEMPT_RT实时Linux上部署
Web 服务器与plc_main绑定在一个CPU上
1. 找到 OpenPLC 的 systemd 服务文件
通常位于/etc/systemd/system/openplc.service
2. 编辑服务文件
找到文件路径后,使用vim编辑:
vim /etc/systemd/system/openplc.service
3. 在 [Service] 部分添加/修改以下指令
[Unit] Description=OpenPLC Runtime v4 Service After=network.target [Service] Type=simple Restart=always RestartSec=5 User=root Group=root # 设置实时调度策略和优先级 (99 为最高) CPUSchedulingPolicy=fifo CPUSchedulingPriority=99 # 绑定到特定 CPU 核心 (例如 CPU 2) CPUAffinity=2 # 允许锁定内存,防止被换出 LimitMEMLOCK=infinity WorkingDirectory=/run/media/mmcblk2p12/openplc-runtime ExecStart=/run/media/mmcblk2p12/openplc-runtime/start_openplc.sh [Install] WantedBy=multi-user.target
4. 重启 systemd 并启动服务
systemctl daemon-reload systemctl restart openplc-runtime.service
5. 验证是否生效
chrt -p $(pgrep plc_main)
输出中应显示pid 1714's current scheduling policy: SCHED_FIFO。
Web 服务器与plc_main绑定在不同的CPU上
1. 创建正确的openplc.service
tee /etc/systemd/system/openplc.service << 'EOF' [Unit] Description=OpenPLC v4 - Web Server After=network.target PartOf=openplc.target [Service] Type=simple User=root WorkingDirectory=/run/media/mmcblk2p12/openplc-runtime CPUAffinity=0 Environment="PYTHONPATH=/run/media/mmcblk2p12/openplc-runtime" ExecStart=/run/media/mmcblk2p12/openplc-runtime/venvs/runtime/bin/python /run/media/mmcblk2p12/openplc-runtime/webserver/app.py ExecStop=/usr/bin/pkill -f "app.py" Restart=on-failure RestartSec=5 [Install] WantedBy=openplc.target EOF
2. 创建正确的openplc-plc.service
tee /etc/systemd/system/openplc-plc.service << 'EOF' [Unit] Description=OpenPLC v4 - PLC Core After=network.target openplc.service PartOf=openplc.target Wants=openplc.service [Service] Type=simple User=root WorkingDirectory=/run/media/mmcblk2p12/openplc-runtime CPUAffinity=2 CPUSchedulingPolicy=fifo CPUSchedulingPriority=99 LimitMEMLOCK=infinity ExecStart=/run/media/mmcblk2p12/openplc-runtime/build/plc_main ExecStop=/usr/bin/pkill -f "plc_main" Restart=on-failure RestartSec=5 [Install] WantedBy=openplc.target EOF
4. 创建 target 文件(可选,用于统一管理)
tee /etc/systemd/system/openplc.target << 'EOF' [Unit] Description=OpenPLC v4 - All Services Requires=openplc.service openplc-plc.service After=openplc.service openplc-plc.service EOF
🚀 使用 target 的日常管理命令
| 操作 | 命令 |
|---|---|
| 启动所有服务 | systemctl start openplc.target |
| 停止所有服务 | systemctl stop openplc.target |
| 重启所有服务 | systemctl restart openplc.target |
| 查看整体状态 | systemctl status openplc.target |
| 查看依赖的服务 | systemctl list-dependencies openplc.target |
5. 重新加载 systemd
systemctl daemon-reload
6. 验证文件已被识别
systemctl list-unit-files | grep openplc
应该看到:
text
openplc-plc.service disabled disabled openplc-runtime.service enabled enabled openplc.service disabled disabled openplc.target static -
target 不需要 enable
7. 启用并启动服务
systemctl enable openplc.service systemctl enable openplc-plc.service systemctl start openplc.service systemctl start openplc-plc.service
8. 检查状态
sudo systemctl status openplc.service sudo systemctl status openplc-plc.service
📌 如果openplc-runtime.service与你冲突
如果openplc-runtime.service也会启动 OpenPLC,你可能需要先停止并禁用它,避免重复启动:
systemctl stop openplc-runtime.service systemctl disable openplc-runtime.service
验证 target 是否工作
# 启动所有服务 sudo systemctl start openplc.target # 检查状态 sudo systemctl status openplc.target sudo systemctl status openplc.service sudo systemctl status openplc-plc.service # 确认进程 ps -eo pid,comm,psr,rtprio,policy,args | grep -E "app|webserver|openplc" | grep -v grep
