How to run a detached background process?

173 views
Skip to first unread message

Julien Dutriaux

unread,
Dec 12, 2016, 11:13:13 AM12/12/16
to Haxe
Hi,

I'm looking to run a background process that stays alive without his parent process. I managed to do spawn a process using a piece of code like that :

import sys.io.Process;

//...
 
var proc = new Process("my_daemon", ["arg1", "arg2"]);
//...


The problem is that the process spawned by this code is also killed when my main process is killed. That's why I need to detach this process from my main process.
But I can't see how to do this easily in the documentation.

Note : I'm targeting CPP ;).

Thanks for your help,
Julien.


Ilir Liburn

unread,
Dec 12, 2016, 6:15:57 PM12/12/16
to Haxe
on Unix use

var proc = new Process("my_daemon &", ["arg1", "arg2"]);

on Windows use

var proc = new Process("start /B my_daemon", ["arg1", "arg2"]);

where /B stands for:

without a new window. The
                application has ^C handling ignored. Unless the application
                enables ^C processing, ^Break is the only way to interrupt
                the application.

Julien Dutriaux

unread,
Dec 13, 2016, 5:56:54 AM12/13/16
to Haxe
Hi,

Thanks for your answer. I will try it today =).

This gave me the idea of using "nohup" on Unix because adding "&" only sends a SIGHUP signal to the process when its parent is killed.

underscorediscovery

unread,
Dec 14, 2016, 2:44:57 AM12/14/16
to Haxe
The cpp code itself has a detached concept iirc , see here around process_run: https://github.com/HaxeFoundation/hxcpp/blob/master/src/hx/libs/std/Process.cpp

You could call into those directly if not exposed in the std api I think.

underscorediscovery

unread,
Dec 14, 2016, 2:46:56 AM12/14/16
to Haxe
p.s. I was posting from my phone and from memory so I may be off on where I saw the flag!
Reply all
Reply to author
Forward
0 new messages