瞬驰(Dash)D1开发手册--Raspberry Pi之开机启动

系统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
  • 所有可用的单元文件存放在 /usr/lib/systemd/system/ 和 /etc/systemd/system/ 目录(后者优先级更高)。查看所有已安装服务:
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'

你可能感兴趣的:(瞬驰(Dash)D1开发手册--Raspberry Pi之开机启动)