Skip to content

Instantly share code, notes, and snippets.

@bhomaidan1990
Last active January 16, 2026 01:49
Show Gist options
  • Select an option

  • Save bhomaidan1990/f0cc0d8a8920275fa24fd2bc573e7a1c to your computer and use it in GitHub Desktop.

Select an option

Save bhomaidan1990/f0cc0d8a8920275fa24fd2bc573e7a1c to your computer and use it in GitHub Desktop.
kill_ros2_node() {
# Input: Node name
NODE_NAME=$1
# Check if node name is provided
if [ -z "$NODE_NAME" ]; then
echo "Error: No node name provided."
return 1
fi
# Find the PID of the node
PID=$(ps aux | grep "ros2 run .* $NODE_NAME" | grep -v grep | awk '{print $2}')
# Check if PID is found
if [ -z "$PID" ]; then
echo "Error: Node '$NODE_NAME' not found."
return 1
fi
echo "Debug Info: Found PID $PID for node '$NODE_NAME'."
# Attempt to kill the node
echo "Attempting to kill node '$NODE_NAME' (PID $PID)..."
kill $PID
# Check if kill command was successful
if [ $? -eq 0 ]; then
echo "Node '$NODE_NAME' (PID $PID) successfully killed."
else
echo "Error: Failed to kill node '$NODE_NAME' (PID $PID)."
return 1
fi
# Optionally, force kill if regular kill fails
# Uncomment the following line if you want to force kill on failure
kill -9 $PID
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment