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

ROS commands from c++ applications

asked 2018-05-02 10:00:58 -0500

touhid gravatar image

updated 2018-05-02 13:26:13 -0500


I am fairly new to ROS. I am trying to run the ROS commands like roscore, rosrun and so on from external c++ applications. I am able to run roscore command but when I try to run multiple commands like, roscore then rosrun then other commands it doesn't work. So, is it possible to achieve this?

For example:

working code:

char* argsUwsim[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/roscore", NULL};
execvp(argsUwsim[0], argsUwsim);

Not working code:

char* argsUwsim[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/roscore", NULL};
char* argsUwsimSecond[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/rosrun uwsim uwsim", NULL};
execvp(argsUwsimSecond[0], argsUwsimSecond);

"not working code" only starts the roscore and then do nothing. I have tried to use c++ fork() method to create child process for running multiple commands but it still doesn't work or I am not running it right.

here is the code with fork():

char* argsMaster[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/roscore", NULL};
char* argsUwsim[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/rosrun uwsim uwsim", NULL};

pid_t pid = fork();

    if(pid == 0){
        execvp(argsUwsim[0], argsUwsim);

    } else if(pid > 0){
        execvp(argsMaster[0], argsMaster);


here, the roscore starts and stays there, nothing happens for the rosrun command. If I stop the roscore with ctrl + c ,whole program stops.

edit retag flag offensive close merge delete


it doesn't work

What does this mean? What's happening (output, errors, etc.)?

jayess gravatar image jayess  ( 2018-05-02 11:14:47 -0500 )edit

Can you please show all your code including the fork statement. It's probable that execution of roscore is blocking any further commands. You can test this by manually killing the roscore from a different terminal or the system monitor and seeing if the next command suddenly gets executed.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-05-02 13:08:16 -0500 )edit

I have edited the post

touhid gravatar image touhid  ( 2018-05-02 13:26:41 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-05-02 14:07:42 -0500

mohsen gravatar image

updated 2018-05-02 14:20:56 -0500

This works for me. (I used a node of my own instead of uwsim)
There are two caveats:
- When you kill the second node, the roscore receives a shutdown request (it doesn't actually shutdown). This is expected, since the second node is a child of the core.
- For some reason I can't shutdown the roscore with ctrl+C.

#include <unistd.h>
#include <ostream>

int main()
char* argsUwsim[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/roscore", NULL};
char* argsUwsimSecond[5] = {"bash", "-i", "-c", "/opt/ros/kinetic/bin/rosrun uwsim uwsim", NULL};

pid_t pid = fork();

if (pid < 0)
        std::cerr << "fork failed";
        return 1;

if (pid > 0) // The parent
        execvp(argsUwsim[0], argsUwsim);    

else // The child
        execvp(argsUwsimSecond[0], argsUwsimSecond);

edit flag offensive delete link more


Thanks for your help. But, I am actually trying to accomplish the running of multiple ROS commands. When, I run your code, it only started the roscore and stayed there and when I stopped roscore whole program stopped, nothing happened for the child's command.

touhid gravatar image touhid  ( 2018-05-02 19:01:25 -0500 )edit

Another thing happened, if child's command i.e. rosrun executes first, then it shows error about not finding the roscore running That is it, nothing happens after that. But if I give ctrl + c command several times, rosrun starts to run but stops after few seconds.

touhid gravatar image touhid  ( 2018-05-02 19:07:04 -0500 )edit

The child process runs in the background. You can verify that using rosnode list.

mohsen gravatar image mohsen  ( 2018-05-02 23:26:25 -0500 )edit

Would it not be far easier to do this with a bash script and/or launch file instead? In our lab we use this combination for starting up very complex ROS systems with a single command.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-05-03 06:33:59 -0500 )edit


mohsen gravatar image mohsen  ( 2018-05-03 06:44:40 -0500 )edit

Thanks PeteBlackerThe3rd and Mohsen. I was finally able to achieve the things with the help of bash script. I followed the link - Running roscore and Launching ROS nodes as Background Process.

touhid gravatar image touhid  ( 2018-05-08 05:30:19 -0500 )edit

Glad you got it working!

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-05-08 06:32:23 -0500 )edit

Question Tools



Asked: 2018-05-02 10:00:58 -0500

Seen: 1,100 times

Last updated: May 02 '18