I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello world example. The calling script looks like:
exec('/usr/bin/php /home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &');
When I run this, the output execoutput.txt contains a copy of the invoking script page, not hello world as I expected.
Why can't I get this PHP script to execute using exec? Note that when I change the command to something like ls -l, the output is a directory listing as expected. btw, in case it matters, I did chmod the called script to 755...
Update - I moved the exec call to the end of the calling script and at least now I don't see the calling script executed in the output. Thx to posters and I will try some of these ideas.
Help!
Thanks
Steve
I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:
exec("php-cli somescript.php");
This worked for me.
What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.
if you just want the script's output, try:
exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')
however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:
exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')
the above should only output the "Hello World" to the execoutput.
Edit:
Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:
alias pexec='php /home/quote2bi/tmp/helloworld.php'
then
exec('pexec > /tmp/execoutput.txt 2>&1 &')
it seems to be a problem with the way exec handles input as opposed to the shell itself.
-John
The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.
I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:
$script = $argv[1]
$params = implode(' ', array_slice($argv, 2));
$cmd = "{$script} {$params} > /dev/null &";
$output = array();
$return = 0;
exec("php {$cmd}", $output, $return);
exit((int)$return);
And then you call it using:
exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')
It´s the only way I managed to get this working.
To avoid the stated problems of PHP in this area, why not put this in inside a shell script? PHP can then execute the shell script which has all the redirections handled internally.
If you need to dynamically change things, then why not write the shell script and then execute it (and of course, clean up afterwards)?
if you are just simply running a php script one possible way to execute the entire code is to use the include() that will run the php file and output any results. You cannot direct the output to a text file but it should appear in the browser window if you're Hello World php script looks like
<?php echo "Hello World!"; ?>
then it will spit that out in the browser. So your second code would look like
<?php include("helloWorld.php"); echo " PHP ROCKS";?>
resulting in a page that would look like,
Hello world! PHP ROCKS
This runs as if you run the script from browser.
This came across while working on a project on linux platform.
exec('wget http://<url to the php script>)
Hope this helps!!
Related
Okay, I'm at a bit of a loss here.
I'm testing out running PHP scripts from within powershell and it just keeps opening NotePad ++ rather than executing the script. I cannot figure out why this won't work...
I'm using a pretty basic PHP script to test:
<?php
echo 'Hello, World!';
?>
And I'm calling it using the standard way I run .ps1 files:
PS C:\php> c:\phpfiles\test25.php
The execution policy is set to unrestricted... what am I doing wrong?
You should pass the path of the file as an argument to the PHP executable. If (lets say) PHP is installed in c:\php, then you must do:
PS c:\php\php.exe -f c:\phpfiles\test25.php
I am trying for last 3 hours to tell PHP to run a simple file. I am using wamp server for windows in local host (Windows 8)
I've tried with exec() working with:
echo exec('whoami');
I got response nt authority.
Also tested with:
if(function_exists('exec')) {
echo "exec is enabled";
}
So it probably works?
I am trying to run a file called tester.php
When I include it, its working, when I require it its working. I need to execute it in background. When I refresh file, code is working without any error, it writes to the database normally.
When i try to exec it its not working.
I tried :
exec("php http://localhost/diplomski/program/defender/tester.php");
exec("php-cli http://localhost/diplomski/program/defender/tester.php");
exec("http://localhost/diplomski/program/defender/tester.php");
Not working, also tried:
exec("php http://127.0.0.1/diplomski/program/defender/tester.php");
exec("php-cli http://127.0.0.1/diplomski/program/defender/tester.php");
exec("php-cli d:\wamp\www\diplomski\program\defender/tester.php")
Not working also tried:
exec("php tester.php");
exec("php-cli tester.php");
exec("tester.php");
Also tried:
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("D:\wamp\bin\php\php5.3.13\php-win.exe -f d:\wamp \www\diplomski\program\defender/tester.php", 0, false);
Tried this, its refreshing infinitely and not working:
exec("php d:\wamp\www\diplomski\program\defender/tester.php");
exec("php-cli d:\wamp\www\diplomski\program\defender/tester.php");
exec("d:\wamp\www\diplomski\program\defender/tester.php");
I'm starting to pull my hair out here. First time I'm trying to use exec() and I'm not very good with it or with the commands.
Give the full path to the PHP executable and the full path to the PHP script. You can save the output in $output to see what the script produced:
exec("d:/path/to/php.exe d:/wamp/www/diplomski/program/defender/tester.php", $output);
print_r($output);
1) What version of php? If it is older then 5.4.0 php can be in safe mode, when safe mode is enabled, you can only execute files within the safe_mode_exec_dir.
2)Note to this function in php.net
Note:
If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
3) So you can try this How to make php script run another php script you can try this
<?php
$somearg = escapeshellarg('blah');
exec("php file2.php $somearg > /dev/null &");
4) You can create a scheduled task How to run a PHP file in a scheduled task (Windows Task Scheduler)
In addition to the earlier answers, let me add that if you want to execute a PHP file from within PHP, you may want to consider the PHP function include instead:
include $path."tester.php"
I guess that would be (much?) more efficient than to spawn a new shell which executes a new instance of PHP which executes the file. But of course the choice of the "better option" may depend on the context.
I am calling a shellscript from php using shell_exec() command. Following is the simple version of the shell script:
args[0] = "tom"
echo "hello"
echo "${args[0]}"
When I run this script from terminal, it gives the following output in terminal:
hello
tom
Whereas when I call this from php using shell_exec() only "hello" is printed and not "tom" . ie; variable assignment is not working when the script is called from php. Why this happens and how can I resolve this.
Any help is appreciated.
Probably PHP executes the script with sh, not Bash; thus arrays (which are a Bash feature) are not supported by the shell.
Workarounds: don't use arrays, or explicitly inboke Bash on the script. (If PHP understands shebangs, having a correct shebang line as the first line of the script may well be sufficient.)
First, args[0] = "tom" should not have space. It should be args[0]="tom"
(I'm not sure is this the problem, but worth to try).
Try this at the end of your code, to see which shell is running your script.
echo "`ps -p $$`"
Try both on terminal and PHP scripts to see if it same shell or not.
I currently use nohup to run a long php script and redirect the live results to a file using this command
nohup php long_file.php >logs 2>&1 &
so i just go and visit logs file continuously to see the results
Now i want to do the exact same thing using another php file to execute the above command
i tried the above command with php exec and the redirect output doesn't seem to be working,
I know i can just retrive the output using php and store it using any file write function but the thing is .. the output is too long thats why i keep it running on server's background
a similar question :
Shell_exec php with nohup, but it had no answer
any solution ?
Please try with -q
nohup php -q long_file.php >logs 2>&1 &
http://ubuntuforums.org/showthread.php?t=977332
Did you try passthru instead of exec?
You are redirecting STDOUT to a file by using >
This will truncate the file each time the script is run. If two scripts simultaneously redirect their output to the same file, the one started last will truncate the output from the first script.
If you really want to properly append to a log file with multiple concurrent running scripts, consider using >> to avoid having the log file truncated.
Side effect however is that the log file never truncates and keeps expanding, so if the file gets really large you can consider including it in a logrotate scheme.
I just began learning PHP. I've installed php5 on Linux and wrote very simple code just to get going.
How can I run scripts? I tried using the -f option, but it works as a cat command and just spits out the code to standard output.
The interactive interpreter option works fine. Is a web browser the only way to execute a PHP script?
A simple:
php myScript.php
… should do the job.
If it is acting like cat, then you probably forgot to switch out of template mode and into script mode with <?php
Shorter way for command line:
php -r 'echo "Hello "; echo "Jay";'
OR
php -r 'echo dirname("parent/child/reply") . "\n";'
As already mentioned, you can execute your PHP with the following.
php myScript.php
If you wish to pass an argument(s), you can simply do so like this:
php myScript.php Apples
In your PHP file you can use this argument by accessing the $argv array like this:
<?php
echo 'I like ' . $argv[1];
?>
The above would print our "I like Apples".
Note the array index is 1 and not 0. 0 is used for script name. In this case $argv would be "myScript.php"
For more information, check out my blog post Running PHP from the Command Line - Basics.
Actually, PHP's main purpose is to generate web pages, but there are at least two other options:
command line (CLI) script execution,
interactive shell - which is actually the variant of the previous option,
The first one can be achieved in many ways (eg. by giving proper permissions to the file and calling script by providing its URI, eg. ./index.php), the second one can be invoked by php -a command (as stated in the documentation mentioned above).