Skip to content

flamencist/Linux.ProcessManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linux.ProcessManager

Build Status NuGet Total NuGet downloads

Process manager for linux. It's can retrieve processes with linux specific info (uid of owner,gid of owner, executable path ...).

Help support the project:

Buy Me A Coffee

Sample usage

var processInfos = ProcessManager.GetProcessInfos();

var initProcess = actual.FirstOrDefault(_ => _.ProcessName == "systemd"); 
Console.WriteLine($"Name: {initProcess.ProcessName}, " +
                  $"ProcessId: {initProcess.ProcessId}, " +
                  $"Real user id: {initProcess.Ruid}, " +
                  $"Effective user id: {initProcess.Euid}, " +
                  $"User name: {initProcess.UserName}, " +
                  $"Start time: {initProcess.StartTime}, " +
                  $"Process path (exe): {initProcess.ExecutablePath}");
Name: systemd, ProcessId: 1, Real user id: 0, Effective user id: 0, User name: root, Start time: 01.03.2018, Process path (exe): /lib/systemd/systemd

Api

GetProcessInfos

ProcessInfo[] processInfos = ProcessManager.GetProcessInfos();

GetProcessInfos (for processes ids)

ProcessInfo[] processInfos = ProcessManager.GetProcessInfos(new []{1,2,3});

GetProcessInfos (with predicate)

ProcessInfo[] processInfos = ProcessManager.GetProcessInfos(_=>_.ProcessId == 1);

GetProcessInfos (with predicate and id)

ProcessInfo[] processInfos = ProcessManager.GetProcessInfos(new []{1}, _=>_.ProcessName == "init");

GetProcessInfoById

ProcessInfo[] processInfos = ProcessManager.GetProcessInfoById(1);

GetProcessIds

int[] processInfos = ProcessManager.GetProcessIds();

EnumerateProcessIds (lazy retrieve pid)

IEnumerable<int> processInfos = ProcessManager.EnumerateProcessIds();
var initProcessCmdLine = processInfos.Select(_=>ProcessManager.GetCmdLine(_)).First();

GetCmdLine (Retrieve command line of process (exe + parameters))

var initProcessCmdLines = ProcessManager.GetCmdLine(1);
Console.WriteLine(initProcessCmdLines[0]);
/sbin/init

GetEnvironmentVariables

var pid = 1000;
var envVariables = ProcessManager.Instance.GetEnvironmentVariables(processId);
Console.WriteLine(envVariables["USER"]);
some_user

GetEnvironmentVariables with predicate

var pid = 1000;
var envVariables = ProcessManager.Instance.GetEnvironmentVariables(processId, _=>_.Key == "USER");
Console.WriteLine(envVariables["USER"]);
some_user

GetEnvironmentVariable by name

var pid = 1000;
var value = ProcessManager.Instance.GetEnvironmentVariable(processId, "USER");
Console.WriteLine(value);
some_user

Kill (by pid)

var pid = 1000;
var signal = 9;//SIGKILL
ProcessManager.Instance.Kill(processId, signal);

ProcessManager.Instance.Kill(processId, ProcessSignal.SIGKILL);

TryKill

var pid = 1000000; // not existing process
var result = ProcessManager.Instance.TryKill(processId, ProcessSignal.SIGKILL);
Console.WriteLine(result);
False

Kill (by process name)

ProcessManager.Instance.Kill("some_process", "username", ProcessSignal.SIGKILL, (ex)=>Console.WriteLine(ex.Message));
ProcessManager.Instance.Kill("some_process", "username"); // signal SIGTERM
ProcessManager.Instance.Kill("some_process", 1001/*user id*/, ProcessSignal.SIGKILL, (ex)=>Console.WriteLine(ex.Message));
ProcessManager.Instance.Kill("some_process", 1001/*user id*/);// signal SIGTERM

Kill (by predicate)

ProcessManager.Instance.Kill((processInfo)=>processInfo.ProcessName == "some_process", ProcessSignal.SIGKILL, (ex)=>Console.WriteLine(ex.Message));
ProcessManager.Instance.Kill((processInfo)=>processInfo.ProcessName == "some_process");// signal SIGTERM

License

This software is distributed under the terms of the MIT License (MIT).

Authors

Alexander Chermyanin / LinkedIn

Contributions and bugs reports are welcome.