ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
11

Can I run a bash script using roslaunch?

asked 2013-01-04 04:39:13 -0600

autonomy gravatar image

updated 2014-11-19 21:52:03 -0600

130s gravatar image

I read http://answers.ros.org/question/12327... and http://answers.ros.org/question/33772... questions and I am confused. I see people mention the possibility of running bash scripts via .launch files but I can't find any documentation describing how to do it. Is it even possible?

(I'm trying to delay the launch of a gpsd_client node because during startup it scans and usurps other serial ports.)

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted
18

answered 2013-01-04 04:49:14 -0600

Yes, This is possible. Place your shell script inside a ROS package. Use chmod to make the script executable. You can then run it from a roslaunch file similar to any node (any executable for that matter):

<node name="foo" pkg="YOUR_ROS_PACKAGE" type="YOUR_SCRIPT_NAME" />
edit flag offensive delete link more

Comments

2

Can anyone add to this how this works in the context of catkin? Where should the script go within the node directory, do there need to be CMake commands to install it to a predictable place?

lucasw gravatar image lucasw  ( 2013-12-05 10:36:39 -0600 )edit

lucasw, have you figured out the answer to your question? if so, could you please post it?

Wesley gravatar image Wesley  ( 2015-07-17 15:26:25 -0600 )edit
1

I think best practice is to put it in scripts/ alongside python files, and install it the same as a python file would be installed.

lucasw gravatar image lucasw  ( 2015-07-20 10:38:42 -0600 )edit

I now it's an old thread. lucasw, if you can read this, can you explain me in details what have you done? Thank you!

Marcofon gravatar image Marcofon  ( 2016-11-09 05:29:47 -0600 )edit
5

I think you just need install(PROGRAMS scripts/foo.sh DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

lucasw gravatar image lucasw  ( 2016-11-09 17:17:48 -0600 )edit

Thank you for your reply! I tried something similar but it doesn't work. Did you tried and reached your goal? My script it's not a .sh, it has no extension. Thank you again!

EDIT: I tried copying the content of the script in a new script with .sh extension, i'm getting:

[test.sh-3] process has died [pid 3316, exit code 1
Marcofon gravatar image Marcofon  ( 2016-11-10 05:05:06 -0600 )edit

You'll probably need to ask a new question and put your test.sh script and the launch file launching it into it (and link to it from here), though it looks like your script did in fact run when launched, though maybe it didn't do what you want it to?

lucasw gravatar image lucasw  ( 2016-11-10 09:56:17 -0600 )edit

It didn't run at all, it has to do some elaborations to a .txt file and then print results on the terminal. Maybe i have found a workaround, i used the command:

system("terminal command for launching the script");

It is a c++ command that gives to the terminal the command that you want.

Marcofon gravatar image Marcofon  ( 2016-11-11 03:18:19 -0600 )edit
17

answered 2014-04-24 02:24:10 -0600

koenlek gravatar image

Better late than never! I came across a similar issue and have implemented a convenient solution. Hopefully many more people can use it, as I find it to be very helpful!

How to use it?

Save the script shown below to a file named "timed_roslaunch.sh". Put it in a scripts/ subfolder of your catkin package. Make it executable using chmod +x. And use it either through command line or a launch file. The scripts help (./timed_roslaunch.sh -h) is shown below:

This script can delay the launch of a roslaunch file
Place it in the 'scripts' folder of your catkin package
and make sure that the file is executable (chmod +x timed_roslaunch.sh)

Run it from command line:

Use: ./timed_roslaunch.sh [number of seconds to delay] [rospkg] [roslaunch file] [arguments (optional)]
Or: rosrun [yourpackage] time_roslaunch.sh [number of seconds to delay] [rospkg] [roslaunch file] [arguments (optional)]
Example: ./timed_roslaunch.sh 2 turtlebot_navigation amcl_demo.launch initial_pose_x:=17.0 initial_pose_y:=17.0

Or run it from another roslaunch file:

<launch>
<arg name="initial_pose_y" default="17.0" />
  <node pkg="semantic_turtle_test" type="timed_roslaunch.sh" 
    args="2 turtlebot_navigation amcl_demo.launch initial_pose_x:=17.0 initial_pose_y:=$(arg initial_pose_y)" 
    name="timed_roslaunch" output="screen">
  </node>
</launch>

The script or use this link.

#!/bin/bash          
#
# Script to delay the launch of a roslaunch file
# 
# Koen Lekkerkerker
# Thu 24 Apr 2014 
#
# Use: ./timed_roslaunch.sh [number of seconds to delay] [rospkg] [roslaunch file]
#

function showHelp(){
    echo 
    echo "This script can delay the launch of a roslaunch file"
    echo "Place it in the 'scripts' folder of your catkin package"
    echo "and make sure that the file is executable (chmod +x timed_roslaunch.sh)"
    echo 
    echo "Run it from command line:"
    echo 
    echo "Use: ./timed_roslaunch.sh [number of seconds to delay] [rospkg] [roslaunch file] [arguments (optional)]"
    echo "Or: rosrun [yourpackage] time_roslaunch.sh [number of seconds to delay] [rospkg] [roslaunch file] [arguments (optional)]"
    echo "Example: ./timed_roslaunch.sh 2 turtlebot_navigation amcl_demo.launch initial_pose_x:=17.0 initial_pose_y:=17.0"
    echo 
    echo "Or run it from another roslaunch file:"
    echo 
    echo '<launch>'
    echo '  <arg name="initial_pose_y" default="17.0" />'
    echo '  <node pkg="semantic_turtle_test" type="timed_roslaunch.sh"'
    echo '    args="2 turtlebot_navigation amcl_demo.launch initial_pose_x:=17.0 initial_pose_y:=$(arg initial_pose_y)"'
    echo '    name="timed_roslaunch" output="screen">'
    echo '  </node>'
    echo '</launch>'
}

if [ "$1" = "-h" ]; then
    showHelp
else 
    echo "start wait for $1 seconds"
    sleep $1
    echo "end wait for $1 seconds"
    shift
        echo "now running 'roslaunch $@'"
    roslaunch $@
fi

Now why did I need it?

My problem was that when I have launch script to launch a fairly large project. Gazebo simulates a world with a simulated turtlebot and gmapping is used for mapping it. It seems that gazebo returns laser measurements before the whole world is spawn. The robot is in a corridor, but as the walls are spawned later than the laser, the first laser measurements give a full free circle. As gmapping already processed these measurements, gmapping incorrectly mapped this circle as free space.

See screenshot:

image description

edit flag offensive delete link more

Comments

That script looks useful. However, wouldn't subsequent laser readings be consumed by gmapping, causing it to update the map with the obstacles/walls?

ceverett gravatar image ceverett  ( 2014-04-24 04:15:18 -0600 )edit

yes and no: the walls are indeed mapped after the world has spawned. But, the fact that space behind these walls is considered free, will affect gmappings performance slightly when it compares measuremnts & expectations (theoretically). In practice, gmapping is robust enough to overcome this.

koenlek gravatar image koenlek  ( 2014-04-24 04:43:51 -0600 )edit

The core of the problem in my opinion lies with Gazebo, that starts up the robots sensors before the world has spawn. Which does not make sense...

koenlek gravatar image koenlek  ( 2014-04-24 04:44:02 -0600 )edit

If I do not misunderstand, it seems that you could try setting the Gazebo simulator to be paused when it starts in the launch file, and you could start the simulation with the start button in the GUI of the simulator after you are sure that everything is correctly spawned.

Mike Gao gravatar image Mike Gao  ( 2014-05-12 06:25:08 -0600 )edit
1

couldn't you also just separate into two launch files: 1st one launches world, 2nd one launches robot+sensor

nbanyk gravatar image nbanyk  ( 2014-09-17 13:41:25 -0600 )edit
1

answered 2018-10-24 17:55:46 -0600

aschaefer gravatar image

I also stumbled across this problem and implemented a generic solution. You might want to have a look at the aliencontrol ROS package.

This package contains a node that starts any external application. Almost more importantly, the node also takes care of a clean shutdown, no matter whether the external application or the containing ROS node is terminated.

It is run like so (if you wanted to start your_script.sh):

rosrun aliencontrol aliencontrol /path/to/your_script.sh

If you want to embed it in your launch script, add:

<node pkg="aliencontrol" type="aliencontrol" name="aliencontrol">
    <param name="cmd" value="/path/to/your_script.sh"/>
</node>
edit flag offensive delete link more

Comments

useful and up-to-date answer. +1

dhsantos gravatar image dhsantos  ( 2023-03-05 04:08:25 -0600 )edit
0

answered 2013-01-06 11:20:11 -0600

dbworth gravatar image

See this source code http://www.ros.org/wiki/webots_run for an example of running an executable from a ROS package.

edit flag offensive delete link more

Comments

+1; For those who may have missed the nuance here -- The advantage of doing it this way is that you are actually creating a ROS Node for your executable, and in this way you can tell the process is executing, and possibly control it.

rlinsalata gravatar image rlinsalata  ( 2015-09-18 11:48:31 -0600 )edit

I did not understand what is that. Webots replaces roslaunch ? The executable is a webots ? Where does this link talk about executable ?

Piero-31 gravatar image Piero-31  ( 2016-07-18 18:41:54 -0600 )edit

Question Tools

6 followers

Stats

Asked: 2013-01-04 04:39:13 -0600

Seen: 46,368 times

Last updated: Oct 24 '18