note 99977 added to function.exec
| From: | digitalaudiorock at gmail dot com | Date: | Fri, 17 Sep 2010 14:34:12 +0000 |
| Subject: | note 99977 added to function.exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172684@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:
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.
----
Server IP: 69.147.83.197
Probable Submitter: 68.192.144.117
----
Manual Page -- http://www.php.net/manual/en/function.exec.php
Edit -- https://master.php.net/note/edit/99977
Del: integrated -- https://master.php.net/note/delete/99977/integrated
Del: useless -- https://master.php.net/note/delete/99977/useless
Del: bad code -- https://master.php.net/note/delete/99977/bad+code
Del: spam -- https://master.php.net/note/delete/99977/spam
Del: non-english -- https://master.php.net/note/delete/99977/non-english
Del: in docs -- https://master.php.net/note/delete/99977/in+docs
Del: other reasons-- https://master.php.net/note/delete/99977
Reject -- https://master.php.net/note/reject/99977
Search -- https://master.php.net/manage/user-notes.php