Kill long-running MySQL queries exceeding an execution threshold

-- Generate KILL commands for active non-sleeping queries running longer than 30s:
SELECT CONCAT('KILL ', id, ';') AS kill_command
FROM information_schema.processlist
WHERE command != 'Sleep' 
  AND time > 30 
  AND user NOT IN ('system user', 'event_scheduler');