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

Revision history [back]

click to hide/show revision 1
initial version

according to my experience , i will use a shell script to do this, an untested example needs your own completion maybe as follows:

#!/bin/bash

AMENT_PREFIX_PATH=/opt/ros/foxy         # your distro
source $AMENT_PREFIX_PATH/setup.bash
source /path/to/your/pkg/setup.bash

ros2 run pkg node1 node2> /dev/null 2>&1 &  # in case screen flushing and set the node run background


i=0
while(( $i<=3 ))  # try 3 times
do
    sleep 1
    echo check for the $i time
    let i++

    node1=`ros2 node list | grep -i node1 | grep -v grep`  # or other critics your choose, e.g. PID
    node2=`ros2 node list | grep -i node2 | grep -v grep`

    if [[ -n $node1 && -n $node2 ]]
    then
        ros2 run pkg node3 &
        echo nodes launched
        break
    fi
done    


if [[ -z $node1 ]]
then
    echo node1 failed
fi

if [[ -z $node2 ]]
then
    echo node2 failed
fi

btw, you may need killall -i -r node to stop the background nodes...

according to my experience , i will use a shell script to do this, an untested example needs your own completion maybe as follows:

#!/bin/bash

AMENT_PREFIX_PATH=/opt/ros/foxy         # your distro
source $AMENT_PREFIX_PATH/setup.bash
source /path/to/your/pkg/setup.bash

ros2 run pkg node1 node2> /dev/null 2>&1 &   # in case screen flushing and set the node run background
# and pls choose your prefered launch method


i=0
while(( $i<=3 ))  # try 3 times
do
    sleep 1
    echo check for the $i time
    let i++

    node1=`ros2 node list | grep -i node1 | grep -v grep`  
    node2=`ros2 node list | grep -i node2 | grep -v grep`
    # or other critics your choose, e.g. PID
    node2=`ros2 node list | grep -i node2 | grep -v grep`
 
    if [[ -n $node1 && -n $node2 ]]
    then
        ros2 run pkg node3 &
        echo nodes launched
        break
    fi
done    


if [[ -z $node1 ]]
then
    echo node1 failed
fi

if [[ -z $node2 ]]
then
    echo node2 failed
fi

btw, you may need killall -i -r node to stop the background nodes...