note 99977 modified in function.exec by danbrown

From: Date: Fri, 17 Sep 2010 14:35:25 +0000
Subject: note 99977 modified in function.exec by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-172690@lists.php.net to get a copy of this message
Just a note related to exec() and programs that run in the background. I have a service that backgrounds itself using the Pear System_Daemon (whose start method forks the process and closes the parent). When I attempted to start that using the services /etc/init.d/service_name start from within another php script using exec(), I expected it to work without redirecting output, but it still hung unless I did. That solution is to close STDOUT and STDERR: <?php if (is_resource(STDOUT)) fclose(STDOUT); if (is_resource(STDERR)) fclose(STDERR); ?> If you background the process using pcntl_fork() AND close these, exec can start the process without redirected input. The same issue was causing the CentOS GUI to hang if I attempted to start my service from there until I added the above. This of course assumes you're already doing something else with all the output. I'm logging all my output using Pear Log and am using set_error_handler() to handle all catchable php errors using the the logger as well. --was-- Just a note related to exec() and programs that run in the background. I have a service that backgrounds itself using the Pear System_Daemon (whose start method forks the process and closes the parent). When I attempted to start that using the services /etc/init.d/service_name start from within another php script using exec(), I expected it to work without redirecting output, but it still hung unless I did. That solution is to close STDOUT and STDERR: if (is_resource(STDOUT)) fclose(STDOUT); if (is_resource(STDERR)) fclose(STDERR); If you background the process using pcntl_fork() AND close these, exec can start the process without redirected input. The same issue was causing the CentOS GUI to hang if I attempted to start my service from there until I added the above. This of course assumes you're already doing something else with all the output. I'm logging all my output using Pear Log and am using set_error_handler() to handle all catchable php errors using the the logger as well. http://php.net/manual/en/function.exec.php

« previous php.notes (#172690) next »