Marvin-wtt
Marvin-wtt

Reputation: 499

Terminate ros2 nodes in bash

I currently have a bash script which launches my ros2 nodes.This works perfectly fine. I now tried to start the script as a background task and write the output into a file. When doing so, I am unable to terminate the nodes at a later point. Previously I terminated it by hitting Ctrl + C in the terminal and all nodes stopped. I tried to do same by saving the PID when starting the script and then killing it afterwards, however the nodes keep running.

Is there any possibility to stop all nodes started by the script? Stopping all ros2 nodes is not possible because I launch multiple in parallel.

Start

"./${SCRIPTFILE}" > $LOGFILE  & 
echo $! > $PIDFILE

Stop

kill -TERM $(cat $PIDFILE) 2> /dev/null

Upvotes: 0

Views: 9765

Answers (1)

user14757127
user14757127

Reputation:

When you use kill -TERM $(cat $PIDFILE) 2> /dev/null it sends a SIGTERM, which is more of a cautious way to see if a shutdown is possible not endangering the integrity of open DBs or files, but it can be blocked or handled otherwise. If you want to kill a process regardless of it's state then use:

kill -9 -TERM $(cat $PIDFILE) 2> /dev/null

Upvotes: 0

Related Questions