php exec() or system() or even shell_exec()...


New Member
I've spent enough time on this now that it's become personal.

I can't seem to figure out how to run a _PHP_ script from PHP. I'm on shared hosting.

I can run arbitrary commands from a php file. ps? sure. whoami? ls? which? Yes to all, and those are almost invasive.

But... I can't run a php file that echoes "Hi"... _from_ a php file. I can run any of this from a file hit by the server.

The php mailing list has a cron job (which is what I eventually want) that has php -q. I've tried all permutations I could think of based on that, as well as much time googling. I know I'm doing something wrong, but I can't figure out _what_.

I want to do two things:
1. Call a php file from a php file, with shell_exec or equiv
2. Call a php file from cron.

What is the proper syntax? Neither php -q /full/path/to/myfile or just adding !#/usr/bin/php -q to the top and /full/path/to/myfile work. I don't seem to have any useful error messages. Anyone conquer a problem like this?


Hello greyproc,

I have multiple php files that I run from cron. Some using php-cgi <file> and others using simply php <file>. Can you get the file to run simply from the command line? If you can get it to run from the command line then you can get it to run from the crontab.

This could also have something to do with the shared environment although I have run php files from cron on another host's shared environment.


New Member
Thank you for the response.

I'm on shared access, so I don't have access to the command line. I'm creating a file, test.php, that then tries to execute another .php file, and fails. Executing a perfectly good working one (that you can hit in the browser and get a "It worked!" type message) gives me line after line of this:
X-Powered-By: PHP/5.2.10
Which seems to be php trying to run the same file over (ie, test.php). It's very weird. The mailing list has a cronjob that starts as php -q, and obviously works fine. I can't see in the source for it that it's doing anything special.

I also tried explicitly trying php-cgi, and that didn't seem to work, either, though a good suggestion.


New Member
Quick reply to self since I can't edit my last message:

My php works just lovely in cron without me doing any sort of changes. I'm adverse to not being able to test on a system before tossing in cron, but I'll deal. My form logic was that I'd call the same php file, and if an action was desired, exec/system/shell_exec the php script to handle it. Instead, it looks like I have to just make the form actions all different php files and redirect back to the main, which is acceptable, if suboptimal.

This still has me scratching my head, because I can happily exec/system/shell_exec anything _else_, and obviously a php file via cli from cron, so... weird!

But, I'm sorted. Thanks again for the response.


New Member
If you're on shared hosting then those methods have almost certainly been disallowed in the PHP configuration.