|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-10-23 15:01 UTC] toppi at kacke dot de
Description:
------------
After forking a process and setting the child as sessionleader (becomes deamon) you cant disconnect from the current terminal-session (exit) solong the child is in process.
After typing exit, the terminal prints the output from the child again. Youre always connected but no more control
Current Config:
[simon@vegeta simon]$ php -v
PHP 5.0.2 (cgi) (built: Oct 23 2004 14:07:24)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.2, Copyright (c) 1998-2004 Zend Technologies
Reproduce code:
---------------
#!/usr/local/bin/php -q
<?PHP
declare (ticks = 1);
set_time_limit (0);
ob_implicit_flush ();
error_reporting(E_ALL);
if( ($pid = pcntl_fork()) == -1 ) {
echo "Error: Cant fork myself ! Dying.... \n";
}
elseif($pid) {
//PARENT PROCESS
echo "Parent-Pid: ".getmypid()." quits here\n";
exit(0);
} else {
//CHILD PROCESS
// detatch from the controlling terminal
if (!posix_setsid()) {
die("could not detach from terminal");
}
//Install Signalhandler
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGCHLD, SIG_IGN);
$i=0;
$childpid = getmypid();
//Do something for testing
while ($i<20){
sleep(1);
echo "Child-Pid $childpid ist working -> type exit to quit your shell-session ->it doesnt close the connex \n";
$i++;
}
echo "Just now we get diconnectet from the shell\n";
exit(0);
}
function sig_handler($signo) {
switch($signo) {
case SIGTERM:
exit;
case SIGCHLD:
while( pcntl_waitpid(-1,$status,WNOHANG)>0 ) { }
break;
default:
break;
}
}
Expected result:
----------------
closing shell (telnet/ssh) and the child doing his background work
Actual result:
--------------
when starting ./testd.php (above) it looks
7360 simon 15 0 7308 2060 6856 S 0.0 0.2 0:00.00 sshd
7361 simon 25 0 4396 1340 3948 S 0.0 0.1 0:00.00 bash
7976 simon 16 0 10012 3452 8872 S 0.0 0.3 0:00.00 testd.php
when "EXIT" the shell
7360 simon 15 0 7308 2060 6856 S 0.0 0.2 0:00.00 sshd <--- left
solong the child repeats 20 times
7360 simon 15 0 7308 2060 6856 S 0.0 0.2 0:00.00 sshd
when the child is done
everthing got closed and the terminal is disconnected
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 08:00:01 2025 UTC |
I tired the code by using cli now. Same problem even when i close STDIN. Also i tired it, by creating a shutdown_function() register_shutdown_function( create_function('','fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;') ); A test on another system, to make sure its not a os-config thingo, gives the same result. Run the code as root (to make sure, its no perm. thing) doesnt help tho. The child always post his outputstream ahead tru sshd (but no bash alive). When creating a socket around the child and you just close the terminal-window to get disconnected from the master, the code chrashes on connect.