# ROS2 OnShutdown 中 ExecuteProcess 不执行

> 原文链接：https://robotics.hellobike.com/pitfalls/2023-06-28-robotics-se-24908
> 频道：pitfalls | 更新日期：2023-06-28 | 标签：auto, stackexchange, roslaunch
> 核心摘要：ROS2 launch 关闭时 OnShutdown 只执行 LogInfo，可用回调函数内 os.system() 执行脚本作为绕过。

# ROS2 OnShutdown 中 ExecuteProcess 不执行

::: v-pre
> 本文由 AI 根据公开内容整理，可能有误，请以原文为准：[How to execute a script at shutdown of a launch process](https://robotics.stackexchange.com/questions/24908/how-to-execute-a-script-at-shutdown-of-a-launch-process)（Robotics Stack Exchange #24908）

**状态**：有绕过办法

## 问题

在 ROS2 launch 中按 `ctrl-C` 触发 `OnShutdown` 后，`LogInfo` 正常输出，但 `ExecuteProcess(cmd=['echo', ...])` 被忽略，无法执行用于清理网络配置的 bash 脚本。

## 环境

ROS 2

## 解决办法

1. 将 `OnShutdown(on_shutdown=...)` 的参数改为一个可调用函数，函数签名包含 `event, context`。
2. 在该函数内部用 `os.system('echo ...')` 或相同方式调用需要的 bash 脚本。
3. 函数可返回 `LogInfo` 记录关闭原因；但返回的 `ExecuteProcess` 仍不会执行。

原问题作者：TheotimeBlg；采纳答案作者：danzimmerman。原内容依 CC BY-SA 4.0 授权，本页摘要同样依 CC BY-SA 4.0 授权。
:::

## 原始参考来源 (Sources)

- [Robotics Stack Exchange #24908：How to execute a script at shutdown of a launch process](https://robotics.stackexchange.com/questions/24908/how-to-execute-a-script-at-shutdown-of-a-launch-process)