diff --git a/v2rayN/ServiceLib/Handler/CoreAdminHandler.cs b/v2rayN/ServiceLib/Handler/CoreAdminHandler.cs index ec448b7a..ec99b26b 100644 --- a/v2rayN/ServiceLib/Handler/CoreAdminHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreAdminHandler.cs @@ -96,7 +96,10 @@ public class CoreAdminHandler { var shellFileName = Utils.IsOSX() ? Global.KillAsSudoOSXShellFileName : Global.KillAsSudoLinuxShellFileName; var shFilePath = await FileManager.CreateLinuxShellFile("kill_as_sudo.sh", EmbedUtils.GetEmbedText(shellFileName), true); - + if (shFilePath.Contains(' ')) + { + shFilePath = shFilePath.AppendQuotes(); + } var arg = new List() { "-c", $"sudo -S {shFilePath} {_linuxSudoPid}" }; var result = await Cli.Wrap(Global.LinuxBash) .WithArguments(arg) diff --git a/v2rayN/ServiceLib/Sample/kill_as_sudo_osx_sh b/v2rayN/ServiceLib/Sample/kill_as_sudo_osx_sh index 043d3703..94011d6f 100644 --- a/v2rayN/ServiceLib/Sample/kill_as_sudo_osx_sh +++ b/v2rayN/ServiceLib/Sample/kill_as_sudo_osx_sh @@ -18,8 +18,8 @@ if ! [[ "$PID" =~ ^[0-9]+$ ]]; then exit 1 fi -# Check if the process exists -if ! ps -p $PID > /dev/null; then +# Check if the process exists - using kill -0 which is more reliable on macOS +if ! kill -0 $PID 2>/dev/null; then echo "Warning: No process found with PID $PID" exit 0 fi @@ -27,9 +27,9 @@ fi # Recursive function to find and kill all descendant processes kill_descendants() { local parent=$1 - # Use ps -eo pid,ppid for macOS compatibility - local children=$(ps -eo pid=,ppid= | awk -v ppid=$parent '$2==ppid {print $1}') - + # Use ps -axo for macOS to ensure all processes are included + local children=$(ps -axo pid=,ppid= | awk -v ppid=$parent '$2==ppid {print $1}') + echo "Processing children of PID: $parent..." for child in $children; do kill_descendants "$child"