Get Process Executed by Mono on Gnu/Linux

Getting Mono runtime running Processes

After testing some code and using:

Proccess[] Proc =Process.GetProcessesByName("mono");

I finally reached an exception with his stacktrace:

Unhandled Exception: System.NotImplementedException: The requested feature is not implemented.
at System.Diagnostics.Process.get_SessionId () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Diagnostics.Process:get_SessionId ()
at ProcWatch.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NotImplementedException: The requested feature is not implemented.
at System.Diagnostics.Process.get_SessionId () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Diagnostics.Process:get_SessionId ()
at ProcWatch.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0

The request feature is not implemented. So it is not possible to recover mono children processes using the .NET framework. I am doing it by using a shell command:

Process.Start("/bin/bash"," -c 'ps -aux | grep Watcher'");

How to kill Linux properly C#

Using Process.Kill() is not a recommended way of stopping a process. It effectively sends the SIGKILL signal which unceremoniously terminates the process. By using the pkill command with just the process name or kill command with the PID (process identifier) you will politely ask the process in question to stop by sending it the SIGTERM signal. For more information on SIGTERM and SIGKILL please see https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html.

In short for your use case a good option is to do the following.

 Process.Start("/bin/bash", " -c 'sudo pkill -f process_name'");

It will nicely ask the process to stop. If the process doesn't end, you can then resort to using:
Process.Kill();

How can I list all processes got executed during a shell-script(or sth. like that) running

You can use strace in order to find all programs that were executed while your script was running. Something like this:

strace -o executed.txt -e execve -f sh your-sctrip.sh 

This is an example. First my bash script:

$ cat build.sh
#!/bin/sh
g++ main.cpp
ls

Then strace of it:

$ strace 2>&1 -e execve -f bash build.sh \
| sed -n -r "s/.* execve\(\"([^,\"]+)\".* = 0$/\1/p"
/usr/local/CC/gcc-4.3.3/bin/g++
/usr/local/CC/gcc-4.3.3/libexec/gcc/x86_64-unknown-linux-gnu/4.3.3/cc1plus
/usr/bin/as
/usr/local/CC/gcc-4.3.3/libexec/gcc/x86_64-unknown-linux-gnu/4.3.3/collect2
/usr/bin/ld
/bin/ls

Or if you need only filenames add xargs -n 1 basename:

$ strace 2>&1 -e execve -f bash build.sh \
| sed -n -r "s/.* execve\(\"([^,\"]+)\".* = 0$/\1/p" \
| xargs -n 1 basename
g++
cc1plus
as
collect2
ld
ls

Intermittent SIGSEV (segfault), SIGABORT and process hangs in C# code using Mono

Ok, it's a known bug in the kernel of Ubuntu.

There is a report bug on Xamarin: https://bugzilla.xamarin.com/show_bug.cgi?id=29827

So if you update your kernel on those machines the bug should go away (let's hope).

Cheers!



Related Topics



Leave a reply



Submit