Solved

PHP exec() not executing php file

Posted on 2008-10-30
11
2,257 Views
Last Modified: 2013-12-13
I am trying to call a PHP file to run as a background process from another PHP file using the exec() command and it does not appear to be working.  

I have a file called test1.php that just calls a second file test2.php to run in the background.  

Very simple...

test1.php:

exec ("/usr/bin/php -f /path/to/test2.php > /dev/null &");

test2.php just sends out an email to me using the mail function.  (just a basic function to debug and figure out what's wrong).

If I run test2.php from the command line, it works fine.  

php -f /path/to/test2.php - this works fine and I receive the email if I run this in putty.  

If I put test2.php in the public_html directory and go to it directly, that also works fine.  But when I try to call it from another PHP file using exec(), nothing happens.

Also, if I call a basic linux command or another program such as ffmpeg using exec() that all works fine.  It just appears to be calling a php file.  Some google searching has suggested it could be an issue with the php or apache user being set to NOONE and not having permissions.  But I'm not sure about that, or what the best solution is if that's the case.

This is on a dedicated linux server.  Centos 4.7  PHP 5.2.6  safe_mode = Off

Let me know what other info would be helpful.  

Thanks!

0
Comment
Question by:stangill13
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 16

Expert Comment

by:sh0e
ID: 22838669
Are you sure that's the right path to php?
What are its permissions?
0
 
LVL 6

Expert Comment

by:vojans
ID: 22838747
Why don't you include the mail module or do it somehow else? You don't need to execute it, do you?
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 22839183
> But when I try to call it from another PHP file using exec(), nothing happens.

Try to analyze the output (if there's any). PHP is compiled with --enable-force-cgi-redirect sometimes which means that calls from shell won't work.
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 22839261
Put both files in same directory and try this...
exec ("/usr/bin/php -f test2.php > /dev/null &");

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 22839272
Better yet just include the file (it will run)
include 'test2.php'; // run test2.php

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:stangill13
ID: 22841670
Hey guys, the scripts here are just some basic test scripts to get this working.

The ultimate goal is that the first file will be a file uploader (video files).  And once the file is uploaded it will then exec() the second file, which will encode the video to .flv format using ffmpeg.  The mail function was just a test to try to figure out whether or not it's working.  So that's the reason I'm not just including it.  Eventually the second file I'm trying to call will be doing video encoding and I will want that to run in the background.  

sh0e: how can I double check if that is the correct path and it's permissions?

NerdsOfTech: putting the two php files in the same directory has the same result.

caterham_www: what do you mean by analyze the output?

Thanks!

0
 

Author Comment

by:stangill13
ID: 22842240
caterham_www:

I don't believe that is the case --enable-force-cgi-redirect.  From the php info file -

'./configure' '--enable-bcmath' '--enable-calendar' '--enable-exif' '--enable-ftp' '--enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-mbstring' '--enable-pdo=shared' '--enable-soap' '--enable-sockets' '--enable-sqlite-utf8' '--enable-wddx' '--enable-zip' '--prefix=/usr' '--with-bz2' '--with-curl=/opt/curlssl/' '--with-curlwrappers' '--with-freetype-dir=/usr' '--with-gd' '--with-gettext' '--with-imap=/opt/php_with_imap_client/' '--with-imap-ssl=/usr' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-libexpat-dir=/usr' '--with-libxml-dir=/opt/xml2' '--with-libxml-dir=/opt/xml2/' '--with-mcrypt=/opt/libmcrypt/' '--with-mhash=/opt/mhash/' '--with-mime-magic' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-mysqli=/usr/bin/mysql_config' '--with-openssl=/usr' '--with-openssl-dir=/usr' '--with-pdo-mysql=shared' '--with-pdo-sqlite=shared' '--with-png-dir=/usr' '--with-pspell' '--with-sqlite=shared' '--with-tidy=/opt/tidy/' '--with-ttf' '--with-xmlrpc' '--with-xpm-dir=/usr/X11R6' '--with-xsl=/opt/xslt/' '--with-zlib' '--with-zlib-dir=/usr'


I also added the optional output and return_var arguments to the exec call.  The result was output was simply an empty Array() and the return_var was 0.
0
 

Author Comment

by:stangill13
ID: 22842258
Also, _ENV["PATH"] is /bin:/usr/bin

I assume that's the path to PHP?  And if so, do I have it correct here:

exec ("/usr/bin/php -f /path/to/test2.php > /dev/null &");
0
 
LVL 16

Accepted Solution

by:
sh0e earned 500 total points
ID: 22847099
Can you try a couple things for me?
At the shell, type: which php

Also, can you try exec("ls -l /path/to/test2.php", $output) and/or exec("cat /path/to/test2.php", $output) in your code and give me the value of $output?
0
 

Author Comment

by:stangill13
ID: 22847172
Yes that's it, THANK YOU!

which php -> /usr/local/bin/php

Path to php was wrong.  (figures something basic)  I changed that and everything works as it should.  
0
 

Author Comment

by:stangill13
ID: 22847214
Although,

which php -a

does list both paths

/usr/local/bin/php
/usr/bin/php


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
htacces issue 2 40
Re-imbursement Claim System 3 25
Link SQL table to Webpage 9 38
Paging Using PHP 7 33
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now