mirror of
https://github.com/2dust/v2rayN.git
synced 2026-09-25 07:48:01 +03:00
https://github.com/2dust/v2rayN/issues/9863 Add a short 1-second delay after terminating core processes on Linux, macOS, and in CoreAdminManager so ports and process resources have time to settle before the next step runs.
79 lines
2.1 KiB
Bash
79 lines
2.1 KiB
Bash
#!/bin/bash
|
|
#
|
|
# Process Terminator Script for Linux
|
|
# This script forcibly terminates a process and all its child processes
|
|
#
|
|
|
|
# Check if PID argument is provided
|
|
if [ $# -ne 1 ]; then
|
|
echo "Usage: $0 <PID>"
|
|
exit 1
|
|
fi
|
|
|
|
PID=$1
|
|
|
|
# Validate that input is a valid PID (numeric)
|
|
if ! [[ "$PID" =~ ^[0-9]+$ ]]; then
|
|
echo "Error: The PID must be a numeric value"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if the process exists
|
|
if ! ps -p $PID > /dev/null; then
|
|
echo "Warning: No process found with PID $PID"
|
|
exit 0
|
|
fi
|
|
|
|
# Recursive function to find and kill all child processes
|
|
kill_children() {
|
|
local parent=$1
|
|
local children=$(ps -o pid --no-headers --ppid "$parent")
|
|
|
|
# Output information about processes being terminated
|
|
echo "Processing children of PID: $parent..."
|
|
|
|
# Process each child
|
|
for child in $children; do
|
|
# Recursively find and kill child's children first
|
|
kill_children "$child"
|
|
|
|
# Force kill the child process
|
|
echo "Terminating child process: $child"
|
|
kill -9 "$child" 2>/dev/null || true
|
|
done
|
|
}
|
|
|
|
echo "============================================"
|
|
echo "Starting termination of process $PID and all its children"
|
|
echo "============================================"
|
|
|
|
# Try graceful termination first
|
|
echo "Attempting graceful termination (SIGTERM) of PID: $PID"
|
|
kill -15 "$PID" 2>/dev/null || true
|
|
sleep 1
|
|
# If still running, fall back to kill_children
|
|
if ps -p $PID > /dev/null; then
|
|
echo "Process $PID did not exit after SIGTERM; proceeding with forced termination of its children and itself"
|
|
else
|
|
echo "Process $PID exited cleanly after SIGTERM"
|
|
exit 0
|
|
fi
|
|
|
|
# Find and kill all child processes
|
|
kill_children "$PID"
|
|
|
|
# Finally kill the main process
|
|
echo "Terminating main process: $PID"
|
|
kill -9 "$PID" 2>/dev/null || true
|
|
|
|
# Wait a little for process/port resources to be fully released
|
|
FINAL_WAIT_SECONDS=1
|
|
echo "Waiting ${FINAL_WAIT_SECONDS}s for resources to settle..."
|
|
sleep "$FINAL_WAIT_SECONDS"
|
|
|
|
echo "============================================"
|
|
echo "Process $PID and all its children have been terminated"
|
|
echo "============================================"
|
|
|
|
exit 0
|