How to Launch a New Process That Is Not a Child of the Original Process

How to create a process that is not a child of it's creating process?

You can try that process A create process C, which create process B and then process C will be immediatly ended (terminated). In a process B there are exist only information about the direct parent process (process Id of C which is not more running) and not about the process A. So "if A's process tree is killed" the process B will probably stay running.

For example you start Process Explorer (see http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx) then start Total Commander. From the Total Commander you start cmd.exe. From cmd.exe you start notepad.exe. Then type "exit" in the cmd.exe. After terminating of cmd.exe you can see that notepad.exe will no more displayed under Total Commander (totalcmd.exe). After you choose in Process Explorer "Kill Process Tree" for the Total Commander (totalcmd.exe) you can see that notepad.exe stay running.

Start new process, without being a child of the spawning process

Here is the code that I'm now using. I thought that it may be useful to someone. It accepts one argument. The argument is a base64 encoded string that decodes to the path of the file that you would like to run.

 Module Module1

Sub Main()
Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs
If CommandLineArgs.Count = 1 Then
Try
Dim path As String = FromBase64(CommandLineArgs(0))
Diagnostics.Process.Start(path)
Catch
End Try
End
End If
End Sub

Function FromBase64(ByVal base64 As String) As String
Dim b As Byte() = Convert.FromBase64String(base64)
Return System.Text.Encoding.UTF8.GetString(b)
End Function

End Module

How to start an entirely new process in node.js (not child)?

You can spawn a child process in a detached state, ignore the outputs, and remove the child from the parents event loop with child.unref().

This code will start someScript.sh, and exit while keeping someScript.sh running.

var spawn = require('child_process').spawn;

var child = spawn(__dirname + '/someScript.sh', [], {
detached: true ,
stdio: [ 'ignore', 'ignore', 'ignore' ]
});

child.unref();

For more detailed information and alternatives (such as logging output / etc), take a look at the documentation for spawn. There are other examples there as well:

http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Creating a new child process when old one is terminated in C

A couple of issues jump out:

  1. if(ret != 0) {// didn't exit normally you've confused ret (which is the pid) for status (which is the exit code of the child)

  2. You can't call wait on a process twice, since calling wait allows the system to release the resources associated with the process. You have several options on how to rewrite this code:

        while(wait(NULL) != -1) { // while there are children to wait on
ret = waitpid(pids[i], &status, WUNTRACED);

One easy way is to use wait then lookup in the array which index it belongs to.

    while((pid = wait(&status)) {
if (pid == -1) { // no children to wait on
break;
}
for(int i = 0; i < nChildProc; ++i) {
if (pid == pids[i]) break;
}
if (i >= nChildProc) {
unexpected_pid_do_something_smart();
}
// Leave the rest of the loop the same

Note: I didn't compile or test the above code.

Launch a totally independent process from Python

I think I found the answer. By using Popen with close_fds = True I was able to start up a process that was independent and without handles to the parent.

For docs look here and search for close_fds.

Or, on Windows, if close_fds is true then no handles will be inherited
by the child process. Note that on Windows, you cannot set close_fds
to true and also redirect the standard handles by setting stdin,
stdout or stderr.

Note this solution only works on Windows. I have no idea about any *nix system.



Related Topics



Leave a reply



Submit