ubuntu20.04设置开机自启动服务(ros)
ubuntu20.04设置开机自启动服务(ros)
为基于ros的升降云台http-server设置自启动
创建service文件
/lib/systemd/system下创建lift_demo.service文件
sudo vim /etc/systemd/system/lift_demo.service
[Unit]
Description=lifter_server
[Service]
Type=forking
#EnvironmentFile=/etc/systemd/test.conf
ExecStart=/bin/bash -c "/opt/systemd-sh/lift_demo.sh &"
ExecReload=/bin/kill -SIGHUP $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target
说明
由于ros程序会一直运行,如果在后台服务脚本中直接执行该脚本,就会收到超时信息,服务自动终止。
Job for xxx.service failed because a timeout was exceeded. See "systemctl status leanote.service" and "journalctl -xe" for details.
xxx.service start operation timed out. Terminating.
Failed to start xxx.service.
Unit xxx.service entered failed state.
解决:
/bin/bash -c "/opt/systemd-sh/lift_demo.sh &"
因此,“bash -c”,bash -c的作用是将一个长字符串当做一条完整的命令来执行,如果在脚本路径后面加上后台运行符号(&),run.sh脚本就会在后台运行,不会一直处于挂起状态,systemd也就不会一直等待run.sh执行完成了。经过测试,完全符合预期,因此,最终的解决方案是:
重新加载service配置文件
sudo systemctl daemon-reload
创建lift_demo.sh
sudo vim /opt/systemd-sh/lift_demo.sh
#!/bin/bash
sleep 2
source /home/woosh/catkin_ws/devel/setup.bash
rosrun test lift lift_demo
赋予可执行权限
sudo chmod +x /opt/systemd-sh/lift_demo.sh
设置开机自启动
systemctl enable lift_demo.service
提示如下信息,正常情况
Created symlink /etc/systemd/system/multi-user.target.wants/lift_demo.service → /lib/systemd/system/lift_demo.service.
reboot重启生效
systemctl相关命令
# 设置开机自启动
systemctl enable nginx.service
# 停止开机自启动
systemctl disable nginx.service
# 验证一下是否为开机启动
systemctl is-enabled nginx
# 启动nginx服务
systemctl start nginx.service
# 停止nginx服务
systemctl stop nginx.service
# 重启nginx服务
systemctl restart nginx.service
# 查看nginx服务当前状态
systemctl status nginx.service
# 查看所有已启动的服务
systemctl list-units --type=service
查看所有正在运行的service(可用于当忘记哪个service打开某个程序的时候)
systemctl list-units --type=service
找到service文件的位置
systemctl cat lift_demo.service
返回的结果中第一行即是service文件地址
查看日志
sudo journalctl -u lift_demo.service
参考
https://blog.csdn.net/h13710501070/article/details/122327039
https://blog.csdn.net/xht555/article/details/110674215
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 刘哥还在学