php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32024 fclose(STDIN|STDOUT|STDERR) has no effect
Submitted: 2005-02-18 20:10 UTC Modified: 2005-02-18 22:31 UTC
From: davidcrawford at sympatico dot ca Assigned:
Status: Not a bug Package: CGI/CLI related
PHP Version: 5.0.3 OS: FreeBSD4.10
Private report: No CVE-ID: None
 [2005-02-18 20:10 UTC] davidcrawford at sympatico dot ca
Description:
------------
I am trying to run a simple PHP script in the background (as a daemon) using the PHP 5 CLI. I first set the execution time limit to 0 (no limit), and then I attempt to close STDIN, STDOUT, STDERR using fclose(), but the pipes are _NOT_ being closed.

This problem seems quite similar to bug #27865, which is labelled as "fixed", but the behaviour I am getting in this instance is not as documented.

Reproduce code:
---------------
set_time_limit(0);
	
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);

sleep(...sync timing with system clock...);

while (TRUE) {
   system(...generate call...);
   sleep(...sleep some more...);
} // END while

Expected result:
----------------
After invocation from the command line, the program should close all STD pipes, and run in the background. A new prompt should appear in the shell, ready to accept my next command while the PHP script is running in the background without open pipes to my current terminal.

Actual result:
--------------
A new command prompt does not appear, instead the terminal hangs in suspense waiting for the process to complete. The script is designed to run forever in a tight loop. Hitting Ctrl+C gets the command prompt back, but interrupts and kills the PHP process.

I can get the desired result by using I/O tricks from the command line:

./script.php &

But, this solution has limitations.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-18 20:17 UTC] wez@php.net
For a process to detach from the shell, you need to fork off a child and exit from the parent.

 [2005-02-18 22:31 UTC] davidcrawford at sympatico dot ca
I re-compiled my PHP CGI/CLI with --enable-pcntl after reading up on the PCNTL extension capabilities.

Thank you very much for pointing out the error in my method, I am glad that this was not a PHP issue.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 11:01:34 2024 UTC