I have two processes that run independently, A exits with some code other than 0, B waits for A to exit and prints the exit code. B does not start process A, it just receives its pid as an argument.
// process A
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
Environment.Exit(100);
// process B
int pid = int.Parse(args[0]);
using var proc = Process.GetProcessById(pid);
_ = proc.SafeHandle; // Otherwise it throws when we try to get exit code
proc.WaitForExit();
Console.WriteLine(proc.ExitCode); // always prints 0
Is there a way for B to actually get the exit code? If I run echo $?
in the terminal where I start process A, I do get 100, but I’m not sure how I can get the exit code from a process that hasn’t started the process I’m monitoring.