aBadAssCowboy
aBadAssCowboy

Reputation: 2520

Running a PHP script containing MySQL Queries in background with out using cronjobs

I have a php script as "save.php" which contains sql queries, and need to run this script in the background i.e., not in the browser. For calling this script I am using the exec() command in file named "trigger.php".

now when save.php is called by exec() it runs normal when the mysql queries are not used., but when I put the mysql queries the queries alone doesn't work and rest of the script executes fine. As far as I am thinking the mysql_connect is not able to run.

I cannot use cronjob because my need is different, I use need to trigger the script and it should be running continuously, but not in intervals.

So, is there a way where I can create a MySqL Server connection in the file which needs to be running in the background?

Upvotes: 0

Views: 1670

Answers (3)

aBadAssCowboy
aBadAssCowboy

Reputation: 2520

I have managed to figure out the problem.

I'm running MAMP for PHP and MySQL. MySQL's socket is created in the MAMP's MySQL directory but when we call a PHP script using exec(), the script is being triggered from the shell/command prompt which checks for the MySQL socket in the usual directory, which is /var/mysql/mysql.sock. Which is why we get the following error.

Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

So, I created a directory under /var and added a symbolic link to the actual socket using

 sudo mkdir /var/mysql

 sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

Works very well now.

Thanks everyone for the suggestions!

Upvotes: 1

aib
aib

Reputation: 46921

PHP scripts are not meant to be run as daemons (so many problems, of which the time limit is just the beginning) so you'll need a launcher.

pcntl_fork() is a simpler alternative to exec() if you're on a system that supports it.

As for your problem with exec(), the problem most likely has to do with the fact that the PHP interpreter you're executing has a different environment than the one that's running on your web server.

Try to execute phpinfo() on both environments to see what the actual differences are.

Though I must confess I can't see what would cause an "unable to connect to server" error; post the full error message, please. Can it be that a local firewall is blocking php.exe? Some other security measure, perhaps? Or could you be getting incorrect information regaring the MySQL server's hostname?

Try to run your exec'ed script from the command line. That'll get you close to what the web server is doing.

Upvotes: 0

Authman Apatira
Authman Apatira

Reputation: 4054

I'm not exactly sure what you're asking (I lost your train of thought)... but if you are trying to get your scripts to run forever or at least as long as your host allows you(?) without using cronjob, then you can use the set_time_limit($seconds) function at the top of your script to set the max execution time allowed by your host. At the end of the script, have it sleep($seconds) and then call exec() to execute itself again. Be sure to factor in the execution time, or you will miss the trigger.

If your sql connections aren't working, then you're going to need to figure out what is going on there. If you post some of your code, we can help you debug. mysql_connect and mysql_pconnect for persistent connections are the commands you will be using to establish connectivity with your database.

Upvotes: 0

Related Questions