Execute another process on process exit
I am attempting to use ROS2 launch to kick off a non-ROS-node executable (a long running process). This process is dirty and spawns other processes and needs a bit of clean up. I'm attempting to use the on_exit
argument of the ExecuteProcess
action to accomplish this and feed it another ExecuteProcess
action which performs the necessary clean up. This second process executes properly if the first one ends on its own, but if I interrupt with CTRL-C
it does not. Another action in the on_exit
list (a LogInfo()
) executes properly in both cases. It's strange to me that the log action executes but the process in the same list does not. Any thoughts would be greatly appreciated. Here's an example of what I'm doing. The long running process in this case is simply a shell sleep 5 and the second process is just an echo:
from launch import LaunchDescription
from launch.actions import ExecuteProcess, LogInfo
def generate_launch_description():
"""Launch the thing."""
return LaunchDescription([
ExecuteProcess(
cmd=['sleep', '5'],
log_cmd=True,
on_exit=[
ExecuteProcess(
cmd=['echo', 'it-worked!'],
log_cmd=True
),
LogInfo(msg='This message prints properly.')
]
)
])
And the output:
✔ 20:54 ~ # ros2 launch test_package test_launch.py
[INFO] [launch]: All log files can be found below /<redacted>
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [sleep-1]: process details: cmd=[sleep, 5], cwd='None', custom_env?=False
[INFO] [sleep-1]: process started with pid [16374]
[INFO] [sleep-1]: process has finished cleanly [pid 16374]
[INFO] [launch.user]: This message prints properly.
[INFO] [echo-2]: process details: cmd=[echo, it-worked!], cwd='None', custom_env?=False
[INFO] [echo-2]: process started with pid [16395]
[INFO] [echo-2]: process has finished cleanly [pid 16395]
✔ 20:54 ~ # ros2 launch test_package test_launch.py
[INFO] [launch]: All log files can be found below /<redacted>
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [sleep-1]: process details: cmd=[sleep, 5], cwd='None', custom_env?=False
[INFO] [sleep-1]: process started with pid [16424]
^C[WARNING] [launch]: user interrupted with ctrl-c (SIGINT)
[ERROR] [sleep-1]: process has died [pid 16424, exit code -2, cmd 'sleep 5'].
[INFO] [launch.user]: This message prints properly.