Remote Process Cleanup
Core Principle
When terminating local processes (SSH client, database client, etc.), must simultaneously terminate source processes on remote server triggered by that connection. Only terminating local connection will cause server resource exhaustion or even crash.
Trigger Scenarios
1. SSH Connection Interruption
- Before
killlocal SSH process, must first terminate remote executing commands through that connection or new connection - If SSH executed database query through
docker exec, need to firstKILLat database level
2. Database Query Termination
- Before canceling local database client, must first execute
KILL <process_id>on remote - For queries executed through SSH tunnel, need to login database through new connection to terminate residual queries
3. Background Task Cancellation
- When canceling remote background task initiated locally, must confirm remote process terminated
- Cannot rely solely on local
killto terminate remote process
Standard Cleanup Process
- Identify Remote Process: Through
SHOW PROCESSLISTorps auxconfirm remote process ID - Terminate Remote Process: Execute
KILLcommand through new connection - Verify Cleanup Result: Query again to confirm remote process disappeared
- Terminate Local Process: After confirming remote cleaned, then terminate local connection
MySQL-Specific Cleanup
-- 1. View current active queries
SHOW PROCESSLIST;
-- Or more precise:
SELECT id, time, state, LEFT(info, 100)
FROM information_schema.processlist
WHERE command != 'Sleep' AND time > 30;
-- 2. Terminate slow query
KILL <process_id>;
-- 3. Verify
SHOW PROCESSLIST;
Forbidden Behaviors
- ❌ Forbidden to only
killlocal process without cleaning remote - ❌ Forbidden to assume "remote will auto-terminate after disconnect"
- ❌ Forbidden to initiate new query without confirming remote cleanup complete
Preventive Measures
- Set timeout for large table queries:
SET SESSION max_execution_time = 60000; - Avoid executing full table scans or no-index JOINs on remote
- For potentially time-consuming queries, first use
EXPLAINto evaluate - Prioritize using
LIMIT+ sampling instead of full scan