/// <summary> /// 获取pid /// </summary> /// <param name="processName"></param> /// <returns></returns> public static int GetProcessPid(string processName) { string cmd = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { cmd = "ps -aux |grep " + processName + "|grep -v grep|awk \'{print $2}\'"; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { cmd = "ps -A |grep " + processName + "|grep -v grep|awk \'{print $1}\'"; } ProcessShell.Run(cmd, 1000, out string std, out string err); if (string.IsNullOrEmpty(std) && string.IsNullOrEmpty(err)) { return(-1); } int pid = -1; if (!string.IsNullOrEmpty(std)) { int.TryParse(std, out pid); } if (!string.IsNullOrEmpty(err)) { int.TryParse(err, out pid); } return(pid); }
/// <summary> /// 检查ffmpeg是否存在 /// </summary> /// <param name="ffpath"></param> /// <returns></returns> public static bool CheckFFmpegBin(string ffpath = "") { if (string.IsNullOrEmpty(ffpath)) { ffpath = "ffmpeg"; } ProcessShell.Run(ffpath, 1000, out string std, out string err); if (!string.IsNullOrEmpty(std)) { if (std.ToLower().Contains("ffmpeg version")) { return(true); } } if (!string.IsNullOrEmpty(err)) { if (err.ToLower().Contains("ffmpeg version")) { return(true); } } return(false); }
/// <summary> /// 获取系统负载率 /// </summary> /// <returns></returns> public static LoadAverage GetLoadAverage() { try { string std = ""; string err = ""; string cmd = "top - bn1 | awk '/average:/ '"; var ret = ProcessShell.Run(cmd, 1000, out std, out err); if (ret) { if (!string.IsNullOrEmpty(std)) { int pos_start = std.IndexOf("load average:", StringComparison.Ordinal); if (pos_start > 0) { std = std.Substring(pos_start + "load average:".Length).Trim(); } string[] loadaverageinfo = std.Split(','); if (loadaverageinfo.Length == 3) { var loadAverage = new LoadAverage(); loadAverage.LoadAverageMin1 = float.Parse((loadaverageinfo[0]).Trim()); loadAverage.LoadAverageMin5 = float.Parse((loadaverageinfo[1]).Trim()); loadAverage.LoadAverageMin15 = float.Parse((loadaverageinfo[2]).Trim()); return(loadAverage); } } if (!string.IsNullOrEmpty(err)) { int pos_start = err.IndexOf("load average:", StringComparison.Ordinal); if (pos_start > 0) { err = err.Substring(pos_start + "load average:".Length).Trim(); } string[] loadaverageinfo = err.Split(','); if (loadaverageinfo.Length == 3) { var loadAverage = new LoadAverage(); loadAverage.LoadAverageMin1 = float.Parse((loadaverageinfo[0]).Trim()); loadAverage.LoadAverageMin5 = float.Parse((loadaverageinfo[1]).Trim()); loadAverage.LoadAverageMin15 = float.Parse((loadaverageinfo[2]).Trim()); return(loadAverage); } } } } catch { return(null); } return(null); }
/// <summary> /// 获取cpu使用率和空闲率 /// </summary> /// <returns></returns> public static CPUInfo GetCPUused() { try { string std = ""; string err = ""; string cmd = "top -bn1 | awk '/%Cpu/ {print $2+$4,$8}'"; var ret = ProcessShell.Run(cmd, 1000, out std, out err); if (ret) { if (!string.IsNullOrEmpty(std)) { string[] cpuinfo = std.Split(' '); if (cpuinfo.Length == 2) { CPUInfo result = new CPUInfo(); result.CpuIdle = float.Parse(cpuinfo[1]); result.CpuUsed = float.Parse(cpuinfo[0]); return(result); } } if (!string.IsNullOrEmpty(err)) { string[] cpuinfo = err.Split(' '); if (cpuinfo.Length == 2) { CPUInfo result = new CPUInfo(); result.CpuIdle = float.Parse(cpuinfo[1]); result.CpuUsed = float.Parse(cpuinfo[0]); return(result); } } } } catch { return(null); } return(null); }
public static void KillProcess(int pid) { string cmd = "kill -9 " + pid.ToString(); ProcessShell.Run(cmd, 1000); }