Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starting new Process as not child of the application #104210

Open
dpeter99 opened this issue Jun 30, 2024 · 5 comments
Open

Starting new Process as not child of the application #104210

dpeter99 opened this issue Jun 30, 2024 · 5 comments
Labels
area-System.Diagnostics.Process untriaged New issue has not been triaged by the area owner

Comments

@dpeter99
Copy link

Description

I'm trying to start a new Process in a multiplatform way (Win and Linux) and make the new process not a child process of the running C# App.

I found this code snippet online for running a process as non-child:

ProcessStartInfo psi = new ProcessStartInfo("YourExecutable.exe");
psi.UseShellExecute = true;
Process.Start(psi);

This still spawns the new process as a child process.

After a lot of searching, I ended up with this setup:

var linux = new ProcessStartInfo("/usr/bin/sh", new []
{
    "-c",
    "nohup /usr/bin/java"+
    " -Xmx1024M" +
    " -Xms1024M" +
    " -jar " +
     _serverJar.FullName +
    "" +
    " &"
})
{
     WorkingDirectory = _serverFolder.FullName,
     UseShellExecute = true,
};

However, this is not easily portable and requires the actual arguments to be already concatenated, which makes some mistakes possible.

Reproduction Steps

Try to use the first example to spawn a new process as non-child, on a Linux system.

Expected behavior

There should be an easy way to run a process as non-child that works on all platforms.

Actual behavior

The spawned process is a child of the app and is terminated when the c# app exits.

Regression?

No response

Known Workarounds

It is possible to call /bin/sh with the nohup and & to instruct it to make a new detached process, but this is not a portable setup.

Configuration

.NET 8
Linux (Fedora 40)
x64

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jun 30, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.

@KalleOlaviNiemitalo
Copy link

The spawned process is a child of the app and is terminated when the c# app exits.

What terminates it?

@dpeter99
Copy link
Author

dpeter99 commented Jul 2, 2024

What terminates it?

The operating system.

@KalleOlaviNiemitalo
Copy link

I'd expect the child process to be reparented to PID 1 (init) and continue running.

How do you start your C# app? If you start it from a terminal emulator and close the window, then that could cause a SIGHUP to be sent to the processes using that tty, including the child process of your app. Or do you start the app from cron?

@dpeter99
Copy link
Author

dpeter99 commented Jul 2, 2024

I tested when starting from Rider IDE, and stopped the app in 3 ways:

  • Rider stop button
  • Let the app exit without doing any await on the Process
  • Sending SIGKILL from htop
    And all of these resulted in the new Process closing
    In my use case, the started "child" process must survive any way the c# app might close/crash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Diagnostics.Process untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants