设置开机自启动
1. 通过gnome-terminal实现开机自启动
1.1 创建一个启动脚本
cd ~
touch start_joy.sh
chmod +x start_joy.sh
1.2 编辑脚本文件
vim start_joy.sh
#!/bin/zsh
source /opt/ros/noetic/setup.zsh
source /home/nv/ants/devel/setup.zsh
roslaunch vehicle_rea joy_control.launch
1.3 配置自动启动
- 创建
~/.config/autostart目录(如果它不存在的话) - 使用
gnome-terminal打开一个新的终端窗口并执行上述脚本。编辑~/.config/autostart目录下的.desktop文件来实现这一点
mkdir -p ~/.config/autostart
创建一个新的 .desktop 文件,例如 start_joy.desktop
vim ~/.config/autostart/start_joy.desktop
[Desktop Entry]
Type=Application
Exec=gnome-terminal -- zsh -c "~/start_joy.sh; exec zsh"
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=Start Joy
Name=Start Joy
Comment[en_US]=Run start_joy.sh on startup
Comment=Run start_joy.sh on startup
其中zsh可以替换为bash
gnome-terminal可以替换为terminator
2. 通过systemd实现开机自启动
2.1 创建systemd服务文件
在/etc/systemd/system/目录下创建.service文件来定义服务
sudo vim /etc/systemd/system/roslaunch-test.service
示例
[Unit]
Description=Auto-start ROS launch (test/test.launch)
After=network.target # 确保网络启动后再运行(若依赖网络,如激光雷达IP通信)
Requires=roscore.service # 可选:若需单独启动roscore,需先配置roscore.service
[Service]
User=USERNAME # 替换为你的用户名(如 fc1),避免以root运行ROS
Group=USERNAME
WorkingDirectory=/home/USERNAME # 工作目录(可改为ROS工作空间路径)
Environment="ROS_DISTRO=noetic" # 替换为你的ROS版本(如 melodic/noetic/foxy)
Environment="CATKIN_WS=/home/USERNAME/catkin_ws" # 替换为你的工作空间路径
# 核心命令:加载ROS环境并启动launch文件
ExecStart=/bin/bash -c " \
source /opt/ros/$ROS_DISTRO/setup.bash && \
source $CATKIN_WS/devel/setup.bash && \
roslaunch test test.launch \
"
# 重启策略:若服务崩溃,自动重启(可选)
Restart=on-failure
RestartSec=5 # 崩溃后5秒重启
# 日志输出:默认写入systemd日志(journalctl查看)
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target # 多用户模式下开机启动
2.2 配置与启动服务
修改.service文件后,需刷新 systemd守护进程
sudo systemctl daemon-reload
设置开机自启并启动服务
# 启用开机自启
sudo systemctl enable roslaunch-test.service
# 立即启动服务(无需重启测试)
sudo systemctl start roslaunch-test.service
2.3 验证与调试
检查服务状态
sudo systemctl status roslaunch-test.service
停止/禁用服务
# 停止当前服务
sudo systemctl stop roslaunch-test.service
# 禁用开机自启
sudo systemctl disable roslaunch-test.service
3. docker开机自启动
3.1 容器自启动
在创建容器时,可以使用--restart参数来设置容器的重启策略
docker run -d --restart=always --name 容器名 镜像名
如果在创建容器时未指定--restart=always,可以使用 docker update命令来设置已有容器的重启策略
docker update --restart=always 容器ID或容器名
如果需要取消容器的自启动,可以使用
docker update --restart=no 容器ID或容器名
3.2 程序自启动
Docker容器为了轻量化,默认不会运行完整的systemd服务,推荐用supervisord
这里有个问题,Docker只会自动重启PID 1,并且PID 1在容器创建那一刻就确定了无法更改,默认为\bin\bash
安装supervisord
apt update && apt install -y supervisor
创建supervisord配置文件
vim /etc/supervisor/conf.d/hexo.conf
添加内容
[program:hexo]
# 进程名称
name=hexo-server
# Hexo 启动命令(指定端口 5000)
command=hexo server -p 5000
# Hexo 工作目录(替换为你的实际目录,比如 /root/hexo)
directory=/root/hexo
# 运行用户(容器中通常用 root)
user=root
# 自动启动
autostart=true
# 自动重启(进程崩溃/退出时重启)
autorestart=true
# 重启延迟(秒)
startretries=3
# 日志输出
stdout_logfile=/var/log/hexo.log
stderr_logfile=/var/log/hexo.err.log
# 日志文件大小限制
stdout_logfile_maxbytes=10MB
启动supervisord并验证
# 启动 supervisord(容器启动时会自动运行,无需额外配置)
supervisord -c /etc/supervisor/supervisord.conf
# 查看 Hexo 进程状态
supervisorctl status hexo
# 常用管理命令
# 启动 Hexo:supervisorctl start hexo
# 重启 Hexo:supervisorctl restart hexo
# 停止 Hexo:supervisorctl stop hexo
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 雯欂の修仙笔记!