I have a Windows 2012 Server that runs IIS and SQL Server 2012.
I am trying to run a PHP script from the command prompt. The script takes around 1 hour to run. If I run it straight from the command line like this - c:\PHP>php.exe "D:\Web\phpScript.php" it runs fine. Again it takes around 1 hour to run but it completes fine.
The thing is I need to run it from another PHP page. So this code - exec('start c:\php\php.exe "D:\Web\phpScript.php"'); in PHP runs the script. When I run it from PHP like that it runs good for around 30 minutes or so but for some reason Windows ends up killing the process after around 30 minutes.
I have been watching the task manager on Windows and cannot see any difference in the way the process runs compared to when I run it straight from the command prompt or when I use PHP to run the command. They both show up as a background process and look exactly the same in the task manager but for some reason Windows is killing the one that runs from PHP and not the one ran straight from the command prompt.
I have even tried running the PHP one in Realtime thinking maybe if it had higher priority it would not get killed but that did not help.
I am really stuck with this.
Any help would be great.
Thanks!
If it has to do with your PHP configuration, you can force allowing a certain execution time with this at the beginning of the script
set_time_limit(60*60*2); // allows 2 hours execution time
Then to execute the external file just use include('D:\Web\phpScript.php'); in the script.
http://php.net/manual/en/function.set-time-limit.php
Otherwise if its a server problem, beats me. You could, of course...run it in your web browser instead of in the command prompt, if PHP is installed on the machine.
Related
I am running centos 7 webserver for one of my clients inside there is a python script that is start from a php script. (this process can happen a few thousand times a minute).
For whatever reason the python scripts are left opened in the server after they finish running each one of them for something like 30 seconds.
This puts a huge load on the server and it's causing me many issue, i tried adding in exit() and sys.exit to the end of the code and in the appropriate function areas but it does not seem to work.
I know that the process are kept a live because i can see them using the top command and they also already made the server shutdown 2 times.
my question is how do i go about shutting down this python scripts what am i missing?
I'm writing a PHP script (OK, it's a daemon worker) that I'd like to run in the background, and it follows the following pseudo-code:
do {
// stuff
sleep(60*30); // 30 minutes
} while(1);
Now, how can I set this baby up to run automatically in the event that the server gets restarted. I don't need to worry about state, since everything is stored in the MySQL DB - and it should just be able to pick up right where it left off.
I'm using an Ubuntu 12.04 x64 server, on AWS (if that matters).
Thanks!
As a solution to your specific problem use bash script to execute PHP cli Pusedo code will be
\usr\sbin\php -q \home\user\myphpscript.php
this will execute the PHP script without any time constrain, however since you use PHP CLI you might not get some environment variables, which are web specific, but for such background process you hardly need them anyways.
Is there a way to make php work forever without cron.
What I want it for is to unban users after a few hours by running a mysql query, thanks
If you don't have access to cron jobs on your server (I guess you are running on a shared hosting?), the best alternative is to run an "external cron". Have a look at www.setcronjob.com. I have been using this for a couple of months now and it is pretty stable.
You can set it up such that it calls a script on your website every whenever you want. (Example: http://www.yoursite.com/script.xxx)
In the script, you can run a MySQL query to check which users have been banned for a couple of hours and then unban them.
You can start your script from the command line and let it run in the background. You will have to design this script in such a way that it never exits and just loops forever using the sleep() function to avoid unnecessary processor load. Since php scripts invoked from the command line have no max execution time the script will run until you manually kill it off with the kill command.
Once you've written the script you can start it with:
nohup php myscript.php &
nohup makes the script still run once you log out of the console session that you started it from, otherwise it would kill off then. The & symbol at the end starts the script as a new process in the background so that you can continue using the console.
I just can't figure this out.
I have a script that gets data from Facebook API and this script runs all the time. (using set_time_limit(0); )
However, sometimes the Facebook API gives errors and stops the script. Therefor, I would like to have a cron task every 5 minutes or so that checks to see if the script is still running and if not, starts it again.
I tried several things but it looks like I cannot run a exec() command from a cron job because of different user rights or something? How would you guys do this?
I use CentOS and PHP 5.3+
Set up the cron under a different user (say, root), which will get around any rights issues. However, PeeHaa makes a good point: if this is a cron script, there's no reason to use exec, as exec's job is to send commands out to the OS... these commands can be run directly from the crontab rather than having cron execute a php file.
You may want to look into creating a Daemon which is better suited to running a script continuously. You can create one using PHP with this PEAR package System_Daemon
If this process runs very frequently, run it in an endless loop and just sleep it. No need for crontabs.
while(true) {
//magical code stuff
sleep(60);
}
I have a php file which pulls some data from external API's, and I want to schedule it to do so every few hours (or every few days). Some googleing led me to "scheduled tasks", but it seems I need to be running my own server to do it?
So far, all the PHP and MySQL I've done have been very simple form-filling, so I'm a little lost. Do I need to turn a computer into a server to do this, or should I look into hosts that allow you to run scripts? I'm not exactly sure what I'm looking for.
Side-question: how would I be able to prevent someone else from running the PHP script (therefor making tons of API calls)?
How are you running the script now? Windows or Linux? Linux is a no-brainer with cron: on a PHP-enabled server simply drop the PHP script somewhere, edit the crontab and away you go!
Ex. for every 2 hours
0 */2 * * * /usr/local/bin/php /path/to/script.php
Edit Re: Mac
launchd is apparently the preferred method to run scheduled tasks but I understand that OS X has cron capabilities as well being a UNIX derivative.
If you have a reasonably busy web server, you can simply check every time how long it has been since the last time you ran the script. If more than two hours, run it.
Just make sure to update the time and run the script atomically so you don't launch several copies of the script. You can do this with a file that contains the last time the script was run that you lock while you check and update it.
cronjobs are made for it... You can check the Cron Jobs in cpanel..
I am assuming your website is launched in Linu environment
http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
http://man.cx/cron
You can find much more exlaination about the Background Process
http://www.fijiwebdesign.com/blog/create-a-background-process-on-the-server-with-php.html