incomplete representation of ctrl+c functionality of terminals in python code

84 views Asked by At

I have an application that I execute in terminal by through the following command:

rosbag record -O /home/user/some_address/file /topic

which makes a file.bag.active in the above address and basically record some information inside it.

After a while by pressing ctrl+c in the terminal the file.bag.active changes to file.bag and will be ready to be read by another application.

Yet, I want to make this sequence done by a python script for which the best way seems doing the following:

import subprocess, shlex

command = "rosbag record -O /home/user/some_address/file /topic"
command = shlex.split(command)
self.proc = subprocess.Popen(command)

where another part of the script that is meant to represent the ctrl+c function is:

self.proc.send_signal(subprocess.signal.SIGINT)

However, in this case the file.bag.active file doesn't change to file.bag, and I am not sure what else I can do or add to this code that will do so.

What I noticed in addition was that if I add a pdb.set_trace() and press ctrl+c inside it when it is invoked I get the result I look for which I can't explain why or how I can represent such functionality in an automatic code.

Does anyone know a workaround for it?

1

There are 1 answers

0
Alejandro On

Got an answer here, that is:

import subprocess, shlex, psutil
command = "rosbag record -O subset /camera/depth/image_raw /camera/rgb/image_raw /joy /mobile_base/sensors/imu_data_raw"
command = shlex.split(command)
rosbag_proc = subprocess.Popen(command)

and for the ctrl+c functionality:

for proc in psutil.process_iter():
    if "record" in proc.name() and set(command[2:]).issubset(proc.cmdline()):
        proc.send_signal(subprocess.signal.SIGINT)

rosbag_proc.send_signal(subprocess.signal.SIGINT)