0

I have a simple shell_exec() script in my php script. On upload this line is run:

shell_exec("php  process.php > /dev/null &");

It definitely runs the file happily in the background as intended, but it seems to have trouble connecting to mysql in process.php

process.php includes:

$db_host="localhost";
$db_name="databasename";
$dbusername="root";
$dbpassword="pass";

mysql_connect($db_host,$dbusername,$dbpassword);
mysql_select_db($db_name);
//then a mysql_query()

It seems that mysql_connect() fails. If I copy and paste all of this into the first file in place of my shell_exec() it works fine.

Is there something about 'localhost' as the mysql host when running shell_exec()? I am testing in xampp on mac btw

user32571
  • 3
  • 3
  • Try with `127.0.0.1`... – Mihai Iorga Feb 22 '13 at 13:51
  • 1
    How do you know it fails? Does it give an error message (please add it to your question) – Ikke Feb 22 '13 at 13:51
  • 1
    Try piping the output of your script into a regular file instead of `/dev/null` and look for error messages. – helmbert Feb 22 '13 at 13:52
  • You should check the arguments for [php](http://php.net/manual/en/features.commandline.options.php), try to load the php.ini and maybe the mysql extention? – Eun Feb 22 '13 at 13:53
  • and also use die function in mysql_connect, mysql_select_db and mysql_query to know where it failed – Bojan Kovacevic Feb 22 '13 at 13:53
  • @Ikke - I know it fails there because I had a line to create a file and tested it by moving the line through the script on each try and the only time it stopped creating it was when it went after mysql_connect (which I also added 'or die' to – user32571 Feb 22 '13 at 13:54
  • The script that does not fail runs through a web browser, doesn't it? – Álvaro González Feb 22 '13 at 13:55
  • @user32571 Make it `or die(mysql_error());`. That way it will give you an error message; – Ikke Feb 22 '13 at 13:57
  • @Mihai - 127.0.0.1 worked instead of localhost. Any reason why this would be? – user32571 Feb 22 '13 at 14:02
  • what is the result of mysql_error? in case you use "localhost". maybe [this](http://stackoverflow.com/questions/4219970/warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-vi) is the issue. – Bojan Kovacevic Feb 22 '13 at 14:07

1 Answers1

0
  • Use full path to PHP binary /usr/local/bin/php instead of php
  • Make sure that process.php file is executable
  • You could use ip's instead of hostnames (i.e: 127.0.0.1 instead of localhost) to slightly speed up things.
  • Remove the /dev/null part so you can see errors and warnings.
Alexandru Guzinschi
  • 5,675
  • 1
  • 29
  • 40
  • The speed up of using ip's instead of names is minimal (especially things like localhost, because it doesn't have to hit the network). – Ikke Feb 22 '13 at 13:58
  • using 127.0.0.1 worked. Thanks! Why would that be? And what would I set it to when I upload to my live hosting server?!? – user32571 Feb 22 '13 at 13:59
  • @Ikke I agree with you, but connection to mysql on some LAMP stacks can not be made using `localhost`. – Alexandru Guzinschi Feb 22 '13 at 14:02