2
$\begingroup$

I need to write a bash script that runs the command ros2 bag record /topic_name for a specified amount of time. Is it possible?

I tried with the command timeout 10 ros2 bag record /topic_name but it doesn't work. I don't know why but basically nothing happens, I cannot see the standard output messages when a record is started. But If I run the command timeout 10 ros2 bag record /topic_name directly from the terminal it works. How is it possible?

$\endgroup$

2 Answers 2

2
$\begingroup$

An alternative solution that I found is by storing the PID:

ros2 bag record /topic_name BAG_PID=$! sleep time_lenght kill $BAG_PID 

I also opened an issue on GitHub here.

$\endgroup$
6
  • $\begingroup$ I'm glad you found a solution. I'm general though this sounds like it would be better on a different stack exchange or stack overflow itself. As this is just a bash scripting question and not a robotics question. $\endgroup$ Commented Nov 15, 2024 at 16:55
  • $\begingroup$ Thanks for the feedback. The question was meant to be robotics-related since the command that I needed to use is a ROS 2 command. It's true that the solution that I've found is basically a bash workaround but what I've asked still lies in the filed of robotics in my opinion $\endgroup$ Commented Nov 18, 2024 at 10:15
  • $\begingroup$ You're asking about the difference between running a command inside and outside a bash script. What the command does (robotics related or not) is not part of this question. Any other command could be substituted in this question and answer. The underlying question is about how you're starting a subprocess in bash. $\endgroup$ Commented Nov 18, 2024 at 20:26
  • $\begingroup$ My question is how to record a rosbag for a fixed amount of time. I understand that my description can be misinterpreted but I guess what I asked can be helpful for other people working with ROS. That's why I would keep the question in this stack exchange $\endgroup$ Commented Nov 19, 2024 at 8:39
  • 1
    $\begingroup$ In your question you state that it works fine using timeout 10 ... . You have answered your own question is it possible. Then what's left to the reader is how to achieve it in a bash script. My goal in pointing this out is to get this in front of the people who can help you answer your best. You're more likely to get a better answer on the appropriate forum. But I'm trying to help you refine your question and get it to the best audience. $\endgroup$ Commented Nov 19, 2024 at 18:01
1
$\begingroup$

I do not know why, but if you run the bash script with use source, like:

source your_timout_record_bag.bash 

then everything will work.

P.s.: The memorizing and killing PID approach didn't work for me.

$\endgroup$
1
  • $\begingroup$ which approach did you use in the end? Interesting question about source command here that maybe help you in understanding the problem $\endgroup$ Commented Dec 19, 2024 at 19:37

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.