Browse Source

执行命令加超时时间 和字符 转定义问题

master
root 4 days ago
parent
commit
dc1a48ed43
  1. 5
      CoreAgent.Infrastructure/Command/Executors/LinuxCommandExecutor.cs
  2. 6
      CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs

5
CoreAgent.Infrastructure/Command/Executors/LinuxCommandExecutor.cs

@ -91,14 +91,13 @@ public class LinuxCommandExecutor : BaseCommandExecutor
throw new ArgumentException("命令不能为空", nameof(command));
}
// 转义命令中的特殊字符
// 使用双引号包裹整个命令,并对特殊字符进行转义
var escapedCommand = command
.Replace("\"", "\\\"")
.Replace("'", "\\'")
.Replace("$", "\\$")
.Replace("`", "\\`")
.Replace("\\", "\\\\");
return $"-c '{escapedCommand}'";
return $"-c \"{escapedCommand}\"";
}
}

6
CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs

@ -187,10 +187,10 @@ public class CellularNetworkService : ICellularNetworkService
{
if (command.CanBeKilled)
{
await KillProcessAsync(command.Template);
await KillProcessAsync(command.Template, command.Timeout);
}
var result = await _commandExecutor.ExecuteCommandAsync(command.Template, _context.token);
var result = await _commandExecutor.ExecuteCommandAsync(command.Template, new CancellationTokenSource(),command.Timeout);
return result.IsSuccess;
}
catch (Exception ex)
@ -200,7 +200,7 @@ public class CellularNetworkService : ICellularNetworkService
}
}
private async Task KillProcessAsync(string template)
private async Task KillProcessAsync(string template,int Timeout)
{
string killCmd = $"ps -ef | grep {template} | grep -v grep | awk '{{print $2}}' | xargs kill -9";
await _commandExecutor.ExecuteCommandAsync(killCmd, new CancellationTokenSource());

Loading…
Cancel
Save