Hudzilla.org - the homepage of Paul Hudson
Contents > Miscellaneous topics > Process control Wish List | Report Bug | About Me ]

16.1.8     Running programs in the current process space: pcntl_exec()

This is NOT the latest copy of this book; click here for the latest version.

int pcntl_exec ( string path [, array args [, array envs]])

We already went over the various common ways to execute programs from PHP, so you're forgiven if you thought, "what? Another to run programs?!"

However, the pcntl_exec() function is quite unlike the functions we looked at previously. Try running this script, for example:

<?php
    
print "Before\n";
    
pcntl_exec("/usr/bin/uptime");
    print
"After\n";
?>

What do you think that should output? If you thought it would print "Before" followed by your computer's uptime information, then "After", you are quite wrong!

You see, unlike functions such as exec() or passthru(), pcntl_exec() replaces the existing process with the new program specified in its parameter. This program is launched and run normally, it has the same PID as PHP had before it called pcntl_exec(), but it replaces the PHP process entirely. As a result, the second print line is never reached, because the PHP interpreter gets killed and replaced by the uptime program in the same process.

If you find this confusing, do not despair - pcntl_exec() is very rarely used, and, to be honest, when you do need to use it things will become clear!

Author's Note: If you are a Unix programmer, it may be helpful for you to know that pcntl_exec() is just a thin wrapper around the execve() function.

The last two parameters to this function allow you to control how the launched process operates. The "args" parameter is simply an array of strings to pass into the program as arguments, whereas the "envs" parameter lets you specify environment variables and their values as an array. Use array keys for environment variables and array values for the environment values.





<< 16.1.7 Other ways to evaluate pcntl_waitpid()\'s return value: pcntl_wifexited(), pcntl_wifsignaled(), pcntl_wifstopped(), pcntl_wtermsig(), and pcntl_wstopsig()   16.1.9 Piping between processes: proc_open() and proc_close() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus four?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow