177 lines
4.6 KiB
Markdown
177 lines
4.6 KiB
Markdown
|
|
# 开机自启动脚本配置操作流程
|
|||
|
|
|
|||
|
|
## 一、环境说明
|
|||
|
|
- 操作系统:Ubuntu 20.04
|
|||
|
|
- 目标脚本:can.sh、lidar128.sh、start.sh(pubJM.sh嵌套在start.sh中,无需单独配置)
|
|||
|
|
- 启动顺序要求:can.sh → lidar128.sh → start.sh
|
|||
|
|
- 配置工具:systemctl(systemd服务管理)
|
|||
|
|
|
|||
|
|
|
|||
|
|
## 二、准备工作
|
|||
|
|
1. **确认脚本路径**
|
|||
|
|
记录当前脚本所在的完整路径(`minibus`目录的绝对路径),示例:
|
|||
|
|
`/home/nvidia/Downloads/minibus`
|
|||
|
|
(若主机更换,只需替换`minibus`之前的可变路径部分)
|
|||
|
|
|
|||
|
|
2. **验证脚本可执行性**
|
|||
|
|
确保所有脚本具有执行权限(若已配置可跳过):
|
|||
|
|
```bash
|
|||
|
|
cd /home/nvidia/Downloads/minibus # 替换为实际路径
|
|||
|
|
chmod +x can.sh lidar128.sh start.sh pubJM.sh
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 三、创建 systemd 服务文件
|
|||
|
|
1. 创建 can 服务(can.service)
|
|||
|
|
```bash
|
|||
|
|
sudo gedit /etc/systemd/system/can.service
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
粘贴以下内容(注意修改ExecStart路径):
|
|||
|
|
|
|||
|
|
```ini
|
|||
|
|
[Unit]
|
|||
|
|
Description=Configure CAN interface (can0)
|
|||
|
|
After=network.target
|
|||
|
|
Documentation=https://www.freedesktop.org/wiki/Software/systemd/
|
|||
|
|
|
|||
|
|
[Service]
|
|||
|
|
Type=oneshot
|
|||
|
|
# 替换为实际的can.sh路径
|
|||
|
|
ExecStart=/home/nvidia/Downloads/minibus/can.sh
|
|||
|
|
User=root # 必须root权限(配置CAN接口和设备权限)
|
|||
|
|
RemainAfterExit=yes # 确保服务状态在执行后保持为active
|
|||
|
|
|
|||
|
|
[Install]
|
|||
|
|
WantedBy=multi-user.target
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
2. 创建 lidar128 服务(lidar128.service)
|
|||
|
|
```bash
|
|||
|
|
sudo gedit /etc/systemd/system/lidar128.service
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
粘贴以下内容(注意修改ExecStart路径):
|
|||
|
|
|
|||
|
|
```ini
|
|||
|
|
[Unit]
|
|||
|
|
Description=Start lidar128 configuration and pcap replay
|
|||
|
|
After=network.target
|
|||
|
|
Documentation=https://www.freedesktop.org/wiki/Software/systemd/
|
|||
|
|
|
|||
|
|
[Service]
|
|||
|
|
Type=simple # 因tcpreplay是持续运行的进程,需用simple类型
|
|||
|
|
# 替换为实际的lidar128.sh路径
|
|||
|
|
ExecStart=/home/nvidia/Downloads/minibus/lidar128.sh
|
|||
|
|
User=root # 需root权限(配置网络接口和vlan)
|
|||
|
|
Restart=on-failure # 进程意外退出时自动重启
|
|||
|
|
|
|||
|
|
[Install]
|
|||
|
|
WantedBy=multi-user.target
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
3. 创建 start 服务(start.service)
|
|||
|
|
```bash
|
|||
|
|
sudo gedit /etc/systemd/system/start.service
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
粘贴以下内容(注意修改ExecStart路径和 User):
|
|||
|
|
|
|||
|
|
```ini
|
|||
|
|
[Unit]
|
|||
|
|
Description=Start ROS2 nodes
|
|||
|
|
After=can.service lidar128.service network.target
|
|||
|
|
Requires=can.service lidar128.service
|
|||
|
|
|
|||
|
|
[Service]
|
|||
|
|
Type=simple
|
|||
|
|
User=nvidia
|
|||
|
|
WorkingDirectory=/home/nvidia/Downloads/minibus
|
|||
|
|
ExecStart=/home/nvidia/Downloads/minibus/start.sh
|
|||
|
|
Restart=on-failure
|
|||
|
|
Environment=RMW_IMPLEMENTATION=rmw_fastrtps_cpp
|
|||
|
|
# 如果需要ROS_DOMAIN_ID等环境变量,也在这里加 Environment=
|
|||
|
|
|
|||
|
|
[Install]
|
|||
|
|
WantedBy=multi-user.target
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 四、配置服务权限与生效
|
|||
|
|
|
|||
|
|
1. 设置服务文件权限
|
|||
|
|
```bash
|
|||
|
|
sudo chmod 644 /etc/systemd/system/can.service
|
|||
|
|
sudo chmod 644 /etc/systemd/system/lidar128.service
|
|||
|
|
sudo chmod 644 /etc/systemd/system/start.service
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
重新加载 systemd 配置
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
sudo systemctl daemon-reload
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
启用开机自启动
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
sudo systemctl enable can.service
|
|||
|
|
sudo systemctl enable lidar128.service
|
|||
|
|
sudo systemctl enable start.service
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 五、验证配置
|
|||
|
|
1. 检查服务状态
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
sudo systemctl status can.service
|
|||
|
|
sudo systemctl status lidar128.service
|
|||
|
|
sudo systemctl status start.service
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
(正常状态应为inactive,未启动)
|
|||
|
|
|
|||
|
|
2. 手动测试启动顺序
|
|||
|
|
```bash
|
|||
|
|
# 依次启动服务,验证依赖关系
|
|||
|
|
sudo systemctl start can.service
|
|||
|
|
sudo systemctl start lidar128.service
|
|||
|
|
sudo systemctl start start.service
|
|||
|
|
|
|||
|
|
# 检查启动结果(若有图形界面,会看到多个gnome-terminal窗口弹出)
|
|||
|
|
```
|
|||
|
|
3. 重启测试
|
|||
|
|
```bash
|
|||
|
|
sudo reboot
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
重启后验证脚本是否按顺序自动执行,可通过以下命令检查日志:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
journalctl -u can.service -f # 查看can服务日志
|
|||
|
|
journalctl -u lidar128.service -f # 查看lidar128服务日志
|
|||
|
|
journalctl -u start.service -f # 查看start服务日志
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 六、注意事项
|
|||
|
|
|
|||
|
|
1. 路径变更处理
|
|||
|
|
|
|||
|
|
若主机更换导致minibus目录路径变化,需重新修改 3 个服务文件中的ExecStart路径,并执行:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
sudo systemctl daemon-reload
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
2. ROS2 环境依赖
|
|||
|
|
|
|||
|
|
确保start.sh和pubJM.sh中source install/setup.bash的路径正确(相对于脚本所在目录),否则 ROS2 节点无法启动。
|
|||
|
|
|
|||
|
|
3. 图形界面权限
|
|||
|
|
|
|||
|
|
若gnome-terminal无法启动,可能是 X11 权限问题,需确认start.service中XAUTHORITY的路径是否与当前用户匹配(替换lyq为实际用户名)。
|
|||
|
|
|
|||
|
|
4. 服务类型说明
|
|||
|
|
|
|||
|
|
- can.service用oneshot(一次性执行)
|
|||
|
|
- lidar128.service用simple(持续运行 tcpreplay 进程)
|
|||
|
|
- start.service用oneshot(启动所有节点后完成)
|