Execute PHP file from command line, kinda

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Execute PHP file from command line, kinda

Post by shiznatix »

I have several cron jobs that I use to get data from external XML files and the likes to keep out site updated. Sometimes these fail if the outside sources server is not on the up and up or whatever. So I setup a section that would allow an admin to rerun the script. Basically the cron job is just to run a job like this:

Code: Select all

php /path/to/file/file.php
which works great. Now I am trying to get this to happen from a call from something like exec(), passthru(), system(), or something. Right now, nothing works! I have this:

Code: Select all

$command = escapeshellcmd('php /path/to/files/file.php 2008 08 5 0');
system($command, $output);
$command is a perfectly valid command, works perfectly from the command line, but I get this response from a system() call:
Status: 404
X-Powered-By: PHP/5.2.3
Content-type: text/html

No input file specified.
So I'm like, blah. I thought, "maybe permissions" but alas, nope. So, can someone help me out here?
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

Re: Execute PHP file from command line, kinda

Post by idevlin »

In the first example you have "/path/to/file/file.php" and in the second one you have "/path/to/files/file.php"

So "file" and "files", the second one is wrong?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Execute PHP file from command line, kinda

Post by shiznatix »

idevlin wrote:In the first example you have "/path/to/file/file.php" and in the second one you have "/path/to/files/file.php"

So "file" and "files", the second one is wrong?
good eye but both paths are made up. I just was hiding my super-top-secret-awesome-9000-zaquier file structure.
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Execute PHP file from command line, kinda

Post by EverLearning »

I think that with you need to use -f switch if you're invoking your script like that

Code: Select all

$command = escapeshellcmd('php -f /path/to/files/file.php 2008 08 5 0');
Also if your cron file has a shebang line at the begining of the file, try just calling it directly

Code: Select all

$command = escapeshellcmd('/path/to/files/file.php 2008 08 5 0');
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Execute PHP file from command line, kinda

Post by shiznatix »

Alas, still having the same problem. Not sure why it always says "no input file specified", that is what I think the root of my problem is.
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Execute PHP file from command line, kinda

Post by EverLearning »

Try putting the full path to php executable /usr/bin/php or /usr/local/bin/php. Also are your really sure that the path to your php file is right? It may be right when run from cron, but since you're trying to run it from a web page, maybe something has changed?
Try listing the files from that directory with

Code: Select all

system('ls -al', $output)
or something similar.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Execute PHP file from command line, kinda

Post by shiznatix »

arg the problem was I had to use php5 for whatever reason. I always used php but when I added php5 everything worked. arg.
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

Re: Execute PHP file from command line, kinda

Post by idevlin »

How odd, are there two different versions of php installed then? php invokes version 4 and php5 invokes version 5?

The parameters must be different between versions then.
Post Reply