4

I have a (possibly dumb) question. I have a script made in php, constructed for cli usage. Works fine when I run it from the command line, no problem there. The problem is that the site I'm working on has ssh restrictions on the hosting server and I cannot ssh there to run it. Hence my question: how can I run the script from another php that is web-accessible? Already tried with exec(), system(), etc. The main problem is that I need he $_SERVER['SHELL'] variable set, and when the call is comming from a web browser of course php doesn't set it.

Any ideeas will be greatly apreciated, thanx.

3
  • exec() should be the way to go. Why does it not work? What happens? Commented Feb 11, 2011 at 13:04
  • What Pekka said. And please post the code you used for exec(), system(), etcetera. Commented Feb 11, 2011 at 13:07
  • it's not a problem that it doesn't run, the problem is that it runs with the apache-cgi interpreter, NOT the php-cli one. inside the script i have a condition if(isset($_SERVER['SHELL'])) sort of thing. in the web-accesible script it just an exec("/usr/bin/php ./console.php"); stuff Commented Feb 11, 2011 at 14:54

2 Answers 2

1

There are many possibilities why exec() and related function calls are not working for you.

  • Your webhost does not have PHP-CLI installed. Just a webserver module
  • You need to use the full path to the php binary for lack of a decent shell environment. E.g. /usr/bin/php <script> instead of php <script>.
  • Your webhost has installed PHP-CLI on a non-standard path (e.g. /usr/local/bin/php, or /opt/php5/php)
  • The webserver user does not have rights to access the php binary
  • Et cetera..
Sign up to request clarification or add additional context in comments.

4 Comments

Pretty much. If your host says they don't support the CLI or PHP-CLI then even if you manage to get it to work, it's just a security exploit that they can close down, rendering your code non-functional again. Why not simply rewrite your original script so it runs from both the CLI as well as the webserver?
It's a script that reads from different csv files, most of them having over 1 milion lines. Using the cli it's much more effective, regardingd mainly the memory and some of the cpu execution times and resources needed. The script gets the data from csv files and writes the informations in a mysql database. The main problem is how exactly to trigger the script from the web browser somehow, it's not a problem that if my hosting supports php-cli, because i can't find a way to execute it even on my development servers, and i'm pretty sure i have cli there, i "aptitude install"-it myself :)
@lost: In that case you should be able to get it going on your development server. What is your production server like? Shared host? Dedicated host? Your own server with root access?
a stupid share-host, unfortunately. made some adjustments to the script and works now, altough the execution time and resources consumption are somwhere between cli and cgi :))) pretty much, i just put a condition at the begining of the stript saying that $_SERVER['SHELL']='/bin/bash'; - sucks, i know, but... whatever works :) 10x a lot guys
0

maybe update the php script to be both an include and a cli script.

use

__FILE__ 

to check if it's a file, then read the params. otherwise do nothing.

and as an include just call the function you want directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.