Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-27-2004, 10:35 AM   #1
Registered: Sep 2002
Posts: 184

Rep: Reputation: 30
php: output the result of exec( $command )

I want to develop a console for php and I wanted to know how to retreive and output the result of a unix command.
At first I outputed the command to a file and read the file with php but some commands don't allow the "> filename" option.

is there another way to do that?

Old 07-27-2004, 11:05 AM   #2
Registered: Dec 2003
Location: the Netherlands
Distribution: SuSE 9.0
Posts: 54

Rep: Reputation: 15
Nice idea, a PHP console. I guess it will be like a telnet session in your browser.

For program execution functions, look at I don't know why your "> output" doesn't work. On UNIX systems, the >, < etc. are not parsed by the program, but by the shell, e.g. bash. Maybe you work with PHP on windows??? Maybe your command line is not parsed by a normal shell so that you can't use > ???

Anyway, I think that you need the proc_open and proc_close functions. You can access either input or output with a file descriptor, just like ordinary files.

How are you going to make this interactive? For example when the user hits a character on his keyboard, how are you going to give that key to the PHP script? Does the PHP script run in an infinite loop (which means that the page will never completely be loaded), or does it end (which may mean that the child process also ends)?
Old 07-27-2004, 11:12 AM   #3
Registered: Sep 2002
Posts: 184

Original Poster
Rep: Reputation: 30
I was only gonna make a form since it is just for a few commands (and I'm a php newb) if you have a better idea, I'll be glad to hear it

for the > option, try it with the useradd command, on my redhat9 the output of the command is not directed to the file

Old 07-27-2004, 11:13 AM   #4
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Rep: Reputation: 30
I suppose you could make it keep running until that command finishes, and then open the page again for another command. Stuff with ncurses might be hard...
Old 07-27-2004, 11:55 AM   #5
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
To get the output of a unix commmand inside PHP, the easiest is to use popen(). See

But this does not let you write to the input and read from the output from the same process. You probably really need this for a web-terminal. To overcome this, you need to use l(ike cjp already said) proc_open(). See

Especially for your idea (the "web-terminal") there's an interesting note in the latter page, referring to PHP5:
PHP 5RC2 introduces pty support for systems with Unix98 ptys. This allows your script to interact with applications that expect to be talking to a terminal. A pty works like a pipe, but is bi-directional, so there is no need to specify a read/write mode. The example below shows how to use a pty; note that you don't have to have all descriptors talking to a pty. Also note that only one pty is created, even though pty is specified 3 times. In a future version of PHP, it might be possible to do more than just read and write to the pty.
Having an interface to PTY's essentially means it will give you the same potential an xterm has.
Old 08-27-2004, 05:35 AM   #6
LQ Newbie
Registered: Aug 2004
Location: China
Distribution: Turbo & LFS
Posts: 5

Rep: Reputation: 0
why not do it like this:

$result = shell_exec($command);

echo $result ;

Old 08-27-2004, 07:41 PM   #7
Registered: Sep 2002
Posts: 184

Original Poster
Rep: Reputation: 30
just so that people know :

what shadow_wwp says works but it's better to append 2>&1 at the end of the command so that even when the command results in an error, it returns the error string.

$result = shell_exec( $command." 2>&1" );


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Output the result of a command to a file darkarcon2015 Linux - Newbie 1 09-24-2005 07:29 PM
BASH Command Result redhatnoob Programming 2 08-19-2004 07:25 PM
php exec devit Programming 4 04-08-2004 04:26 AM
PHP exec Spooky Programming 1 01-19-2004 09:34 AM
exec cgi from php? barazor Programming 5 01-23-2002 10:12 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:39 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration