robot_upstart 实现ROS程序开机自启动

robot_upstart install启动命令参数分析

命令示例

rosrun robot_upstart install --interface lo --master "http://127.0.0.1:11311" --user root --setup /home/test/ros_ws/setup.bash test_bringup/launch/test_start.launch --logdir /home/test/logs

没有指定job(service)名称就用“test_bringup”中的"test"

参数说明

usage: install [-h] [--job JOB] [--interface ethN] [--user NAME]
[--setup path/to/setup.bash] [--rosdistro DISTRO]
[--master http://MASTER:11311] [--logdir path/to/logs]
[--augment] [--provider [upstart|systemd]] [--symlink]
pkg/path [pkg/path ...]
--job 指定job(service)名称,如果没有制定,就使用package中的一部分 (一般为‘_’之前的单词)
--interface 指定网络接口
--user 指定用户启动launch脚本
--setup 指定使用特殊工程下的setup.bash文件。
--rosdistro 指定ros的发布版本
--master 指定ROS_MASTER_URI
--logdir 指定日志文件目录
--augment=False 不创建job,只是拷贝用户文件。默认job已经创建成功
pkg/path 安装job launch的package和path名称,保持路径以包名开头(不可使用绝对或workspace最top路径)
--provider 指定 provider 在无法自动检测正确的 provider 情况下
--symlink=False 创建软链接,而不是拷贝启动脚本。

成功执行后,说明

  • /etc/init/下生成 test.conf
# THIS IS A GENERATED FILE, NOT RECOMMENDED TO EDIT.

description "bringup test"

start on local-filesystems
stop on runlevel [016]

console log
respawn

exec test-start
pre-stop exec test-stop
  • /etc/ros下产生
/etc/ros$ tree
.
├── indigo
│   └── test.d
│       ├── test_start.launch
└── rosdep
    └── sources.list.d
        └── 20-default.list

4 directories, 3 files

其中 test.d下有

/etc/ros/indigo/test.d$ cat .installed_files 
/etc/ros/indigo/test.d/test_start.launch
/etc/ros/indigo/test.d/.installed_files
/usr/sbin/test-stop
/usr/sbin/test-start
/etc/init/test.conf
  • 查看日志
sudo tail -f  -n 10 /var/log/upstart/test.log 
启动时的log和节点运行的log都在这里面 (注意:这个test.log为服务名)

如果launch启动失败,会提示log记录的全路径,按照提示就是能找到launch启动失败的详细信息,可以帮助问题定位。

额外信息

cat /usr/sbin/test-start

#!/bin/bash
# THIS IS A GENERATED FILE, NOT RECOMMENDED TO EDIT.

function log() {
  logger -s -p user.$1 ${@:2}
}

log info "test: Using workspace setup file /home/test/ros_ws/install/setup.bash"
source /home/test/ros_ws/install/setup.bash
JOB_FOLDER=/etc/ros/indigo/test.d

log_path="/home/test/logs"
if [[ ! -d $log_path ]]; then
  CREATED_LOGDIR=true
  trap 'CREATED_LOGDIR=false' ERR
    log warn "test: The log directory you specified \"$log_path\" does not exist. Attempting to create."
    mkdir -p $log_path 2>/dev/null
    chown root:root $log_path 2>/dev/null
    chmod ug+wr $log_path 2>/dev/null
  trap - ERR
  # if log_path could not be created, default to tmp
  if [[ $CREATED_LOGDIR == false ]]; then
    log warn "test: The log directory you specified \"$log_path\" cannot be created. Defaulting to \"/tmp\"!"
    log_path="/tmp"
  fi
fi

export ROS_HOSTNAME=$(hostname)

export ROS_MASTER_URI=http://127.0.0.1:11311

log info "test: Launching ROS_HOSTNAME=$ROS_HOSTNAME, ROS_IP=$ROS_IP, ROS_MASTER_URI=$ROS_MASTER_URI, ROS_LOG_DIR=$log_path"

# If xacro files are present in job folder, generate and expand an amalgamated urdf.
XACRO_FILENAME=$log_path/test.xacro
XACRO_ROBOT_NAME=$(echo "test" | cut -d- -f1)
rosrun robot_upstart mkxacro $JOB_FOLDER $XACRO_ROBOT_NAME > $XACRO_FILENAME
if [[ "$?" == "0" ]]; then
  URDF_FILENAME=$log_path/test.urdf
  rosrun xacro xacro $XACRO_FILENAME -o $URDF_FILENAME
  if [[ "$?" == "0" ]]; then
    log info "test: Generated URDF: $URDF_FILENAME"
  else
    log warn "test: URDF macro expansion failure. Robot description will not function."
  fi
  export ROBOT_URDF_FILENAME=$URDF_FILENAME
fi

# Assemble amalgamated launchfile.
LAUNCH_FILENAME=$log_path/test.launch
rosrun robot_upstart mklaunch $JOB_FOLDER > $LAUNCH_FILENAME
if [[ "$?" != "0" ]]; then
  log err "test: Unable to generate amalgamated launchfile."
  exit 1
fi
log info "test: Generated launchfile: $LAUNCH_FILENAME"

# Warn and exit if setuidgid is missing from the system.
which setuidgid > /dev/null
if [ "$?" != "0" ]; then
  log err "test: Can't launch as unprivileged user without setuidgid. Please install daemontools package."
  exit 1
fi

# Punch it.
export ROS_HOME=$(echo ~root)/.ros
export ROS_LOG_DIR=$log_path
setuidgid root roslaunch $LAUNCH_FILENAME &
PID=$!

log info "test: Started roslaunch as background process, PID $PID, ROS_LOG_DIR=$ROS_LOG_DIR"
echo "$PID" > $log_path/test.pid

wait "$PID"

cat /usr/sbin/test-stop

#!/bin/bash
# THIS IS A GENERATED FILE, NOT RECOMMENDED TO EDIT.

PID=$(cat /home/test/logs/test.pid)
logger -p user.info "Attempting to stop test (PID $PID)"
kill $PID

Tips: 查看进程树命令 pstree -ng

你可能感兴趣的:(robot_upstart 实现ROS程序开机自启动)