You can try this:
How to stop your robot when ROS is shutting down - Knowledgebase - The Construct ROS Community (robotigniteacademy.com)