Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl exec('/usr/bin/php -v') hangs on CentOS 6.6 unless STDIN is closed first

My question is, is this expected (and why?) and/or is this behavior seen on other systems too?

Environment:

# cat /etc/*-release
CentOS release 6.6 (Final)
...
# perl -v 

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
...
# php -v
PHP 5.4.41 (cli) (built: May 14 2015 23:15:28) 
... 

mini.pl:

#!/usr/bin/perl
exec('/usr/bin/php -v');

Run it and see the hung PHP process:

# perl mini.pl &
[2] 16958
[1]   Killed                  perl mini.pl
# ps ax | grep 16958
16958 pts/2    T      0:00 /usr/bin/php -v
16960 pts/2    S+     0:00 grep 16958

[2]+  Stopped                 perl mini.pl

But if I close STDIN:

mini.pl:

#!/usr/bin/perl
close(STDIN);
exec('/usr/bin/php -v');

It runs fine:

# perl mini.pl &
[1] 16976
# PHP 5.4.41 (cli) (built: May 14 2015 23:15:28) 
...

[1]+  Done                    perl mini.pl
# ps ax | grep 16976
16978 pts/2    S+     0:00 grep 16976

Other notes:

  • I cannot reproduce this hanging behavior on Mac OS X (perl 5.18.2, php 5.5.24)

Thanks,

like image 241
Kristopher Windsor Avatar asked Aug 06 '15 08:08

Kristopher Windsor


People also ask

What is stdin stdout in Linux?

stdin − It stands for standard input, and is used for taking text as an input. stdout − It stands for standard output, and is used to text output of any command you type in the terminal, and then that output is stored in the stdout stream.

How do I run a Linux command in the background?

Use bg to Send Running Commands to the Background You can easily send such commands to the background by hitting the Ctrl + Z keys and then using the bg command. Hitting Ctrl + Z stops the running process, and bg takes it to the background. You can view a list of all background tasks by typing jobs in the terminal.

What is Hup nohup?

nohup is a POSIX command which means "no hang up". Its purpose is to execute a command such that it ignores the HUP (hangup) signal and therefore does not stop when the user logs out.


2 Answers

I just tested this on Ubuntu 14.04 and RHEL 6.7, and could not reproduce the problem.

I suspect there is a bug that is causing it to both print the version and attempt to process STDIN as PHP code. While it's hanging, try pressing Control-D, or typing a few lines of HTML/PHP and then pressing Control-D to see if your input is processed.

like image 55
Andru Luvisi Avatar answered Nov 15 '22 14:11

Andru Luvisi


I have a CentOS with the same issue, resolved by adding an ampersand(&) at the end of the system call while you can keep yum.

I actually use a python script to call the php, same issue.

The real issue - unknown, if anyone have some idea, please let me know as well.

Solution:

exec("/usr/bin/php -v &");
`/usr/bin/php -q ./mini.php &`;

my os:

CentOS release 6.4 (Final)
CentOS release 6.4 (Final)

[gliang@www perl_tools]$ perl -v

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
[gliang@www perl_tools]$ php -v
PHP 5.3.3 (cli) (built: Jul  9 2015 17:39:00)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.1.4, Copyright (c) 2002-2012, by Derick Rethans
[gliang@www perl_tools]$

Issue duplicated:

[gliang@www perl_tools]$ perl mini.pl &
[1] 29744
[gliang@www perl_tools]$ ps uax|grep php
gliang   29744  0.1  0.3 341016  8728 pts/0    T    20:27   0:00    /usr/bin/php -v
gliang   29756  0.0  0.0 103248   812 pts/0    S+   20:27   0:00 grep php

[1]+  Stopped                 perl mini.pl
[gliang@www perl_tools]$
like image 23
Gang Avatar answered Nov 15 '22 13:11

Gang