PHP system() mysqldump makes my page not responding - php

With PHP on my WIN 7 + localhost, i'm testing to dump a mysql database via:system(mysqldump <parameters>). But while i run the code, my page is gone hanged (not responding) by never ending with loading sign. The test database is quite tiny. Whats wrong with it?
Here are the steps i've done for this work:
system ('mysqldump -u rootname -p rootpw dbname > output.sql');
Add the full path of mysqldump.exe into Windows Environment Variables
When i run the code, there appear output.sql file with just 0 kb sized and the page is not responding.

Try increasing the time limit:
// If system call
set_time_limit(600);
// Otherwise
ini_set('max_execution_time', 600);
set_time_limit reference
max_execution_time reference
Also, if you want to see what your database is doing, do a show full processlist from the mysql command line.

Related

PHP: How to run a Batch-Script in the background [Windows]

I've got a simple question:
How can I run a batch-script in the background on a windows machine? I need to establish a database connection with MySQL over XAMPP. For this, I need to run "mysql_start.bat" and the problem is, that the following code stops executing on the exec-command of PHP (I think it waits until the script is finished).
exec("cmd /c C:\\xampp\\mysql_start.bat > tmp.txt 2>&1");
Hint: Redirecting the output isn't necessary, I just have tried it, but also didn't work.
I haven't found any other possibilties to start MySQL.
Hopefully someone can help me.
Yours Michael.
Covert your batch file into windows executable using "Bat_to_exe converter" and just give the path removing "cmd /c" as parameters to exec().
Also use exception handling methods or "die" to check if the function returns any error.

Issues executing vbscript through PHP on WAMP stack

I am having issues executing a VBScript through Apache (WAMP) on Windows Server 2012. I am attempting to convert a Docx to PDF, and the script runs perfectly from the command line, but fails when running through PHP. Rather than posting the vbscript, I will provide a link to it: http://bit.ly/1gngYAn
When executed through PHP as follows, WINWORD.exe starts, as does the VBScript, and it hangs there and nothing happens. No PDF is generated (and I never see the ~temporary.docx hidden file pop in the directory).
I have tried just about every iteration of exec, system, passthru and COM ( 'WScript.Shell' ), and all have the same outcome.
To avoid "escaping" issues, I also tried executing the script though a .bat file so no arguments needed to be passed, and the outcome was the same.
Here is my current php code (convert.vbs is the code from the link above):
$obj = new COM ( 'WScript.Shell' );
$obj->Run ( 'cmd /C wscript.exe //B C:\Users\Administrator\Desktop\convert.vbs c:\wamp\www\fileconv\temp_store\52fa8272bf84f.docx', 1, false );
//I have tried different "window styles" too, and it doesn't make a difference
I also tried modifying the apache service user to run as administrator (this is not a production server), and enabled "Allow service to interact with the desktop", and it had the same outcome.
I have also made sure the directories had "full control" by everyone (reading, writing, executing, etc).
It runs perfectly if I run from the command line or with my ".bat" file.
Since it hangs (the script and word, not apache), I have looked at the event viewer in the control panel, but there are no events that pertain.
My questions is firstly, why is this happening, and secondly, if the first cannot be answered, is there a way that I can get a more in depth look at what is happening when the process is executed, as to further troubleshoot it? As of now, I have no data to review or output to see to help me troubleshoot.
Please feel free to ask for any details. I have tried many, many iterations to try to get this to work, searched high and low, and can't seem to come up with any answers.
I appreciate your assistance,
Louis
It took me a couple of days, but here is the solution I found:
I used PsExec - http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
The following flags are required: -h -i -accepteula -u -p
(I tried without the -h, -accepteula and -i, but no dice. This is running on Windows Server 2012 under WAMP)
Here is an example:
exec('c:\psexec\PsExec -h -i -accepteula -u Administrator -p '.$password.' C:\Windows\System32\CScript.exe //Nologo //B c:\wamp\www\fileconv\convert.vbs '.$filename)
Now it executes properly and as intended.
I hope this helps someone in the same situation!
PS The WScript.Shell method of execution I used in my question works just as well as exec(), except exec() waits until the process exits.
You should use exec() function
this is the url http://php.net/manual/fr/function.exec.php

PHP CLI script not timing out

We have a node js script that runs a command to execute the following command:
/usr/local/bin/php -q /home/www/441.php {"id":"325241"}
This script does a lot of things, however it does not seem to respect the time limit. The first line of this file is:
set_time_limit(1800);
Yet if we check what processes are running on the server (ps -aux | grep php) we will see a lot of these commands that have been open since last week.
Any ideas on how we can clean this up?
I found the following comment on the PHP user guide for max_execution_time
Keep in mind that for CLI SAPI
max_execution_time is hardcoded to 0.
So it seems to be changed by ini_set
or set_time_limit but it isn't,
actually. The only references I've
found to this strange decision are
deep in bugtracker
(http://bugs.php.net/37306) and in
php.ini (comments for
'max_execution_time' directive).
So it would seem that there's a bug in the CLI module that means max_execution_time is effectively ignored.
The commenter mentioned a page in the bug tracker about this at http://bugs.php.net/37306 but the tracker seems to be down.
set_time_limit only has meaning to the php part of the program. If you had a query on a database that takes 5h to finish, those 5h are not counted by php, so they fall out of scope of the set_time_limit limitation. Having said that, it seems weird that a php process is still running after a week, if it is not calling another program that runs forever (which, in this case, the set_time_limit neither affects that calling).
Also, what does the -q flag? I can't find it on man php nor php --help nor in php's command line options.
If you start the script in nodejs, why not kill it there too, after 1800s?
var pid = startPHPProcess();
setTimeout(function() {
killPHPProcess(pid);
}, 1800);

How do I run a PHP script continuously on localhost?

I have a script which crawls info using API. What I want is to run this script continuously to grab data and store it on my local machine. I configured machine as localhost and installed phpmyadmin and MySQL.
You probably will want to run it from the command line. Put your code in some kind of loop to always keep it running, or schedule it to run as a cron job / scheduled task.
Wrap your main code, except for functions, classes, etc, in this:
while (true) {
and this:
sleep(2);
}
Then run it from the command line, using:
$ php myscript.php
If using Windows, you manually have to add php to your PATH. I don't know Windows (using a Mac all my life) but I guess you can do this in My Machine's Properties. :)
Read about set_time_limit, or modify max_execution_time value in your php.ini file .
Instead of a php loop, use a batch file loop. .
This gets around any set_time_limit , maximum memory etc problems. If php crashes, no problem, a new instance will be started immediately.
#echo off
:loop
php myScrapeScript.php
goto loop;

backup mysql database in php using mysqldump

I have this code, that uses mysqldump to backup mysql database. The problem is I'm getting this fatal error:
Fatal error: Maximum execution time of
60 seconds exceeded in
C:\wamp\www\pos\php\backupdb.php on
line 13
Line 13 is the final line.
<?php
$backupFile = 'c:\\onstor'. date("Y-m-d-H-i-s") . '.sql';
$command = "mysqldump --opt -u root -p onstor > $backupFile";
system($command);
?>
What do I do, I think the code is okay since I've tried it in command prompt and it worked.
Is it bad that I have put the path to mysql/bin to the environment variables.
The problem, as the error message states pretty plainly, is that your script is running for too long. Scripts executing through a web server are not meant to run longer than a few seconds. You can change that using set_time_limit, but what you should really do is let long running scripts run from the command line. Since the only thing you're doing is running a CLI command anyway, just ditch the PHP wrapper completely. Make it a shell script if necessary. Run this shell script regularly as a cron job/Windows Scheduler task (or whatever the Windows equivalent is called).
Your code looks okay. Dumping is taking lot of time. that's all. Read this Fatal error: Maximum execution time of 400 seconds exceeded. Do what is written there first and then look for any problem in your code.
The message say you reached out the 60 seconds excution time.
You can change it using the set_time_limit function, i.e.:
set_time_limit(120); // 2 minutes
but I don't know why you would try to use mysqldump in PHP, it seems dangerous to me.

Categories