Get process executed by MONO on GNU/Linux

2020-04-26 01:42发布

问题:

I am using MONO to execute an application. Using ps command shows eihter processname MONO or CLI. How can I get the name of the application executed by MONO ?

Example : mono myApp.exe

I want to know, if myApp.exe is currently excecuted. Finally I want to do this check programmatically.

Cheers.

回答1:

You usually will run your program from a shell script and there you can use the -a flag to exec:

#!/bin/bash
exec -a VisibleName mono program.exe


回答2:

Here is a solution that uses .NET/MONO functions (no need to invoke native DLLs nor piping Shell Output):

  • List all processes.
  • If a process Name contains MONO or CLI then read the commandline of that process
  • The commandline shall contain all needed Information to identify your application

     public static int process_count(string application_name)
    {
        int rc = 0;
        string cmdline = "";
    
        Process[] processlist = Process.GetProcesses();
    
        foreach (Process p in processlist)
        {
    
            cmdline = "";
    
            //Console.WriteLine("PID : " + theprocess.Id + " " + theprocess.ProcessName);
    
            if (p.ProcessName.Contains("mono"))
            {
                Console.WriteLine("PID : " + p.Id + " " + p.ProcessName + " " + p.MainModule.FileName);
                cmdline = File.ReadAllText("/proc/" + p.Id.ToString() + "/cmdline");
                Console.WriteLine("CMDLINE : "+cmdline);
            }
    
            if (p.ProcessName.Contains("cli"))
            {
                Console.WriteLine("PID : " + p.Id + " " + p.ProcessName + " " + p.MainModule.FileName);
                cmdline = File.ReadAllText("/proc/" + p.Id.ToString() + "/cmdline");
                Console.WriteLine("CMDLINE : " + cmdline);
            }
    
    
            if (cmdline.Contains(application_name))
            {
                Console.WriteLine("Found existing process: {0} ID: {1}", p.ProcessName, p.Id);
                rc++;
            }
        }
        return (rc);
    }
    

How to do it manually:

  • invoke ps -e to get all processes of MONO or CLI
  • Look up the PID e.g. 2845
  • Display command line : cat /proc/2845/cmdline

Note for newbies: this Approach is not dedicated to Windows OS as it does not Support the concept of /proc filesystem.

Cheers



回答3:

Have a look at

getting-mono-process-info

The solution uses the same approach of reading /proc but has more options.

Cheers