ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
I start my roslaunch on start up using cron (http://wiki.ubuntuusers.de/Cron) and screen (http://wiki.ubuntuusers.de/screen).
I have a /home/user/autostart/autostart_screens.sh that get executed by cron @reboot (Meaning whenever the PC is booted). You can set these startup files via crontab
command. See crontab --help
. My current crontab -l
gives:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
@reboot bash /home/user/autostart/autostart_screens.sh
My autostart_screens.sh starts and detaches two screen sessions and logs any thing happening inbetween to a log file. It looks like:
# !/bin/bash
LOG_FILE=/home/user/autostart/logs/log_autostart_screens.txt
echo "" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
echo "#############################################" >> ${LOG_FILE}
echo "Running autostart_screens.sh" >> ${LOG_FILE}
echo $(date) >> ${LOG_FILE}
echo "#############################################" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
echo "Logs:" >> ${LOG_FILE}
set -e
set -v
{
screen -d -m bash /home/user/autostart/start_roscore.sh
screen -d -m bash /home/user/autostart/start_roslaunch.sh
} &>> ${LOG_FILE}
Note that it is not blocking as it just starts the screens.
The start_roscore.sh looks like:
# !/bin/bash
LOG_FILE=/home/user/autostart/logs/log_start_roscore.txt
echo "" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
echo "#############################################" >> ${LOG_FILE}
echo "Running start_roscore.sh" >> ${LOG_FILE}
echo $(date) >> ${LOG_FILE}
echo "#############################################" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
echo "Logs:" >> ${LOG_FILE}
set -e
{
source /opt/ros/hydro/setup.bash
source /home/user/workspace/ros_hydro/catkin/devel/setup.bash
export ROS_WORKSPACE=/home/user/workspace/ros_hydro/catkin
export ROS_MASTER_URI=http://192.168.1.10:11311/ ##e. g. Master
export ROS_IP=192.168.1.15 ##e. g. Own IP
sleep 5
} &>> ${LOG_FILE}
set -v
{
roscore
} &>> ${LOG_FILE}
The start_roslaunch.sh looks like:
# !/bin/bash
LOG_FILE=/home/user/autostart/logs/log_start_roslaunch.txt
echo "" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
echo "#############################################" >> ${LOG_FILE}
echo "Running start_roslaunch.sh" >> ${LOG_FILE}
echo $(date) >> ${LOG_FILE}
echo "#############################################" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}
echo "Logs:" >> ${LOG_FILE}
set -e
{
source /opt/ros/hydro/setup.bash
source /home/user/workspace/ros_hydro/catkin/devel/setup.bash
export ROS_WORKSPACE=/home/user/workspace/ros_hydro/catkin
export ROS_MASTER_URI=http://192.168.1.10:11311/
export ROS_IP=192.168.1.15
sleep 8
} &>> ${LOG_FILE}
set -v
{
roslaunch my_pkg my_launch.launch
} &>> ${LOG_FILE}
All logs go to /home/user/autostart/logs. After start youn can verify your ros being up using rosnode list
. You can monitor your screens using screen -ls
. Note that the timeout in start_roslaunch.sh is longer than in start_roscore.sh. In order for shutting down the nodes on my computer if I want to use it for something else I use this script:
# !/bin/bash
for session in $(screen -ls | grep -o '[0-9]\{5\}')
do
screen -S "${session}" -X quit;
done
It sends a Crlt+C to all running screen sessions.