系统RASPBIAN JESSIE 使用systemd替换init系统控制开机是系统的启动。它是通过单元文件来对文件进行管理。
我们启动ROS,使用的命令是
roslaunch dashgo_bringup minimal.launch
如何把它加到开机自启动中呢?具体操作如下:
1) 创建一个/usr/sbin/dashgo-start ,内容如下:
#!/bin/bash
log_file=/tmp/dashgo-upstart.log
DATE=`date`
source /opt/ros/indigo/setup.bash
source /home/pi/catkin_ws/devel/setup.bash
interface=wlan0
echo "$DATE: dashgo-start on interface $interface" >> $log_file
export ROS_IP=`ifconfig $interface | grep -o 'inet addr:[^ ]*' | cut -d: -f2`
echo "$DATE: dashgo-start setting ROS_IP=$ROS_IP" >> $log_file
if [ "$ROS_IP" = "" ]; then
echo "$DATE: dashgo-start can't run with empty ROS_IP." >> $log_file
exit 1
fi
roslaunch dashgo_bringup minimal.launch
增加可执行权限
sudo chmod +x /usr/sbin/dashgo-start
2) 创建一个/usr/sbin/dashgo-stop ,内容如下:
#!/bin/bash
log_file=/tmp/dashgo-upstart.log
DATE=`date`
echo "$DATE: dashgo-stop" >> $log_file
source /opt/ros/indigo/setup.bash
source /home/pi/catkin_ws/devel/setup.bash
for i in $( rosnode list ); do
rosnode kill $i;
done
killall roslaunch
增加可执行权限
sudo chmod +x /usr/sbin/dashgo-stop
3) 创建一个/usr/sbin/dashgo-restart ,内容如下:
#!/bin/bash
log_file=/tmp/dashgo-upstart.log
DATE=`date`
echo "$DATE: dashgo-stop" >> $log_file
/usr/sbin/dashgo-stop
sleep 2
/usr/sbin/dashgo-start
增加可执行权限
sudo chmod +x /usr/sbin/dashgo-restart
4) 创建一个/lib/systemd/system/dashgo.service ,内容如下:
[Unit]
Description=dashgo startup
[Service]
Restart=always
RestartSec=30
ExecStart=/usr/sbin/dashgo-start
ExecStop=/usr/sbin/dashgo-stop
ExecRestart=/usr/sbin/dashgo-restart
[Install]
WantedBy=multi-user.target
systemd使用文档参考:https://wiki.archlinux.org/index.php/systemd_%28%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%29
sudo systemctl start dashgo.service
sudo systemctl stop dashgo.service
systemctl is-enabled dashgo.service
sudo systemctl enable dashgo.service
sudo systemctl disable dashgo.service
sudo systemctl mask dashgo.service
sudo systemctl unmask dashgo.service
如果修改/lib/systemd/system/dashgo.service的内容,需要重新加载。
sudo systemctl daemon-reload
systemctl status
systemctl
或
systemctl list-units
systemctl list-unit-files
systemctl --failed
systemctl status dashgo.service
systemd-analyze plot > test.svg
这个例子中的失败的服务是 systemd-modules-load :
1) 通过 systemd 寻找启动失败的服务:
$ systemctl --state=failed
systemd-modules-load.service loaded failed failed Load Kernel Modules
2) 我们发现了启动失败的 systemd-modules-load 服务. 我们想知道更多信息:
$ systemctl status systemd-modules-load
systemd-modules-load.service - Load Kernel Modules
Loaded: loaded (/usr/lib/systemd/system/systemd-modules-load.service; static)
Active: failed (Result: exit-code) since So 2013-08-25 11:48:13 CEST; 32s ago
Docs: man:systemd-modules-load.service(8).
man:modules-load.d(5)
Process: 15630 ExecStart=/usr/lib/systemd/systemd-modules-load (code=exited, status=1/FAILURE)
如果没列出 Process ID, 通过 systemctl 重新启动失败的服务 ( 例如 systemctl restart systemd-modules-load )
3) 现在得到了 PID ,你就可以进一步探查错误的详细信息了.通过下列的命令收集日志,PID 参数是你刚刚得到的 Process ID (例如 15630):
$ journalctl -b _PID=15630
-- Logs begin at Sa 2013-05-25 10:31:12 CEST, end at So 2013-08-25 11:51:17 CEST. --
Aug 25 11:48:13 mypc systemd-modules-load[15630]: Failed to find module 'blacklist usblp' Aug 25 11:48:13 mypc systemd-modules-load[15630]: Failed to find module 'install usblp /bin/false'