3


I want to run my long runing python script from my console app.
I use ("my_script.py"), when I shut down console also python script is terminate.
In task manager all(console app and script) is runing under .Net Core Host.
How to run python as completly separated process?

krkozlow
  • 55
  • 6
  • It could be something that could meet your needs..[click](https://stackoverflow.com/questions/27624850/launch-a-completely-independent-process) –  Oct 04 '18 at 13:46
  • How did you start the process? Post your code. [Others](https://stackoverflow.com/questions/43515360/net-core-process-start-leaving-defunct-child-process-behind) complain that the child process *remains alive* once the parent is killed. – Panagiotis Kanavos Oct 04 '18 at 15:07

1 Answers1

4

Normally, this would start your python script completely outside of your console application:

System.Diagnostics.Process.Start(@"C:\path\to\my_script.py");

In classic .NET it will invoke the process via a new shell, but in .NET Core, the process is created directly inside the existing executable. There is an option to change this, called UseShellExecute. This is directly from the Microsoft documentation:

true if the shell should be used when starting the process; false if the process should be created directly from the executable file. The default is true on .NET Framework apps and false on .NET Core apps.

This is how you can use it:

var myPythonScript = new Process();
myPythonScript.StartInfo.FileName = @"C:\path\to\my_script.py";
myPythonScript.StartInfo.UseShellExecute = true;
myPythonScript.Start();

When your C# console app terminates, your python script should still be running.

EDIT

Thanks to Panagiotis Kanavos, he made me realize the UseShellExecute has nothing to do with the parent/child relationship between the processes. So I setup a sandbox locally with .NET Core and played around with it a bit and this is working for me:

var myPythonScript = new Process();
myPythonScript.StartInfo.FileName = @"C:\path\to\python.exe"; // the actual python installation executable on the server
myPythonScript.StartInfo.Arguments = @"""C:\path\to\my_script.py""";
myPythonScript.StartInfo.CreateNoWindow = true;
myPythonScript.Start();

When the parent app terminates, my python script is still running in the background.

jtate
  • 2,612
  • 7
  • 25
  • 35
  • I feel like I am missing something. The question says "Process.Start" (assuming typo) doesn't work. This answer says it does. What am I missing? Is there some subtle interaction in the API or calling sequence that's important? – omajid Oct 04 '18 at 14:28
  • @omajid ah, I see now. I updated my answer. It's because .NET Core behaves differently than the classic .NET framework – jtate Oct 04 '18 at 14:56
  • @jtate It's `directly from the executable file`, which means from the file passed in th epath parameter. There's no attempt to find which application can handle the file extension. Definitely not `directly inside the existing executable`. You can't create a process inside a process. – Panagiotis Kanavos Oct 04 '18 at 15:00
  • `UseShellExecute` is a can of worms. `UseShellExecute` + script will not work on newer versions. See https://github.com/dotnet/corefx/issues/24704. In recent versions it does `xdg-open` instead of `bash -c` :( I would suggest calling out to `bash` manually, but I suspect we will run into OP's question (recursively!) – omajid Oct 04 '18 at 15:00
  • @jtate finding the application associated with an extension is the job of the shell. When you set `UseShellExecute` to true you say you want the shell to find out how to execute whatever is passed in the path – Panagiotis Kanavos Oct 04 '18 at 15:01
  • 1
    In any case, it's not the *Shell* that controls the parent/child relationship between the processes – Panagiotis Kanavos Oct 04 '18 at 15:07
  • @PanagiotisKanavos you're exactly right. updated my answer with code that actually works :) – jtate Oct 04 '18 at 15:53
  • I closed my Process.Start in .net console app, and now I invoke Process.Start(my_console.exe) and it's completely separate process. – krkozlow Oct 05 '18 at 08:45
  • @ianstewart I'm not exactly sure what you mean, but did you get it working? – jtate Oct 05 '18 at 12:44
  • Yes it's working now. I tried your solution and in my case it didn't work :( – krkozlow Oct 05 '18 at 15:48