amphp / process

An async process dispatcher for Amp.
MIT License
229 stars 32 forks source link

amphp/process kills sub process on exit #33

Closed ostrolucky closed 1 year ago

ostrolucky commented 5 years ago

wrapper.php

<?php
echo 'foo';
sleep(1);
touch('a');

Following works as expected, outputs foo, exits immediately and after 1 second creates file 'a'.

<?php

$fd = [
    ["pipe", "r"], // stdin
    ["pipe", "w"], // stdout
];
$handle = @\proc_open('php wrapper.php', $fd, $pipes);

echo fread($pipes[1], 100);

Following does not work as expected. Outputs foo, exits immediately but file is not created.

<?php
require_once "vendor/autoload.php";
\Amp\Loop::run(function () {
    $process = new \Amp\Process\Process("php wrapper.php");
    yield $process->start();
    echo yield $process->getStdout()->read();
});
trowski commented 5 years ago

If the Process object is destroyed while the process is running, the process is killed. If you want to wait for the process to finish execution, you need to wait on the promise returned from Process::join().

ostrolucky commented 5 years ago

I don't want to wait, I want to exit immediately and let process finish in background

kelunik commented 5 years ago

Currently we don't directly support daemonizing child processes. Maybe you can get it work with nohup / setsid? I tried, but I failed.

ostrolucky commented 5 years ago

Yeah I don't know how to do that either, what you currently use at https://github.com/amphp/process/blob/355b1e561b01c16ab3d78fada1ad47ccc96df70e/lib/Internal/Posix/Runner.php#L82-L83 is black magic for me.

Also please remove wrong label.

trowski commented 5 years ago

A possible solution is adding a method to Process, perhaps daemonize(), that sets a flag to not automatically kill the child process when the Process object is destroyed. The current default behavior would not be altered, but that would enable what @ostrolucky is trying to accomplish.

ostrolucky commented 5 years ago

yeah that would work for me

kelunik commented 1 year ago

This won't be supported for now, because the process would have to be properly disowned to avoid zombie processes.