Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am about embarking on this massive project. Using php for the server side , mysql for the database. It's really important that some part of this application keeps running on the background , algorithms , the database being updated , etc. Even if no one is on the system. How do I achieve this ??
From personal research, I found out that I can create a from job. But I've not really used that before , and is there a way a from job can be made to run on an endless loop ?
Make a php file which you want to run in background
say automate.php
Make a cron job which runs at every 30 mins(JUST EXAMPLE) using crontab -e and edit like below
0/30 * * * * /path/php automate.php
Make sure php is defined in your PATH
#olu The easiest way to get PHP code to run over an over again is to put it inside of a loop that never ends and run the command in headless mode. You could have a php loop that looks like:
public function loop(){
//Do you stuff here.
sleep(30); // Wait for 30 seconds.
loop();
}
loop();
When you run a file that calls this loop it will never end. This will achieve what you want but isn't the best way to perform this task.
On unix/linux there is a job/task scheduler named cron. http://www.unixgeeks.org/security/newbie/unix/cron-1.html
It can be a bit tricky to learn at first but there are lots of examples on how to schedule a cron job. Ideally this would be a better solution then running a headless loop.
I hope this helps.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
This has been a problem for me for a while, but I honestly have NO clue how to write this script. Essentially, what I want to do is have a cron job that will be called daily and then update all variables under the "Age" field in my database. I also have a date of birth while helps change this variable at the moment, (Subtracting todays age with the DOB). Ive looked up a lot for cron jobs and I understand that you have one file which has all the code in it to do one job.
What I THINK I should do is create a repeat region that just updates every record as it is pulled from the server, but other people have been telling me differently. I'm new in programming and this Cron Job thing has REALLY been confusing me on what I'm doing. If I could have a good explanation, or even someone help me write this it would be greatly appreciated.
Cron is just timesheet for periodical jobs. So you should write your script with code wich will update needed data. For example this script will be updater.php. Then you should add to crontab string like this (this script will be repeating every day in 12 p.m.)
0 0 * * * /usr/bin/php path/to/updater.php &> /dev/null
If you need help for writing updater.php you should add some info about your schema
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a start_processing("set 1") function which takes 6 hrs to complete set1,
I want to process set 2, set 3 .... how can i process all of them in same time ?? since when i put
<?php
start_processing("set1");
start_processing("set2");
start_processing("set3");
?>
It takes 18 hrs.
I want to complete process in 6hrs for all processing.
Finally i got a solution
I have take curl_multi - it is far better. Save the handshakes - they are not needed every time!
Use curl_multi_init to run the processes in parallel. This can have a tremendous effect.
Unless you are using PHP as Apache-module, you can use pcntl_fork to create several processes of which each processes one function call.
if(pcntl_fork())
start_processing("set1");
else if(pcntl_fork())
start_processing("set2");
else
start_processing("set3");
If you have a varying number of working sets, just put them in an array and loop through it. Just bear in mind that too many processes could overload your system!
Another, more lightweight, option is the use of php pthreads which AFAIK work with Apache, but require installing the corresponding php-extension first.
A third possibility is, as mentioned by sandeep_kosta and Niranjan N Raju, to create one Cronjob for each working set.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am wondering if there is a way to add a new user automatically in a mysql table every minute or so ?
I have a USER table and a USER ACTIVE table, when ever a user is active the details goes under the USER ACTIVE table. I would like to automatically be able to add and delete x number of users from the USER ACTIVE table every minute.
How do I achieve this? I am also a beginner with PHP (which is what I am using) MySQL.
You can't do what you want in pure PHP. PHP is designed to render a page on request and then finish, it won't keep running with a timer. You can either set up a cron job as Dagon suggests, or look at adding some java or similar that keeps running a timer once the page is loaded - which I don't think you want, because that would still require the page to be open somewhere.
I have something similar set up using only PHP, the cron job simply launches the PHP interpreter to run the page every so often: "php /home/user/timer.php". Exactly how you set this up is going to depend on what level of access you have to the system and what commands are available, but most web hosts will allow the setup of a basic cron job.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to run a php function on my server in a certain time without make the script sleep.
I need this in order to update my database in specific time.
For example a chat room that lasts for 5 minutes.
The chat room have a flag in the database for open status.
I want to change the flag in the database to close after 5 minutes.
public function callOnDelay($time, $data)
{
//SOME CODE EXECUTED AFTER SOME TIME IS OVER//
}
There are different ways of doing this depending on your OS.
On Linux you can look for Cron Jobs (http://www.thesitewizard.com/general/set-cron-job.shtml).
On Windows you can look for Task Scheduler (http://technet.microsoft.com/en-us/library/cc766428.aspx).
Also another, not so reliable, method of doing this is adding a conditional/if in your main script (index.php ?) or a "before function" in your controller (if you are using some kind of framework that supports it) and checking for the last status and doing something with it.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I need a script to work forever; that's it, it will generate information on the aground without stopping, downloading stuff and storing info on the database, as well as performing calculations.
It seems possible in PHP like here:
<?php
ignore_user_abort(); // run script in background
set_time_limit(0); // run script forever
$interval=60*15; // do every 15 minutes...
do{
// add the script that has to be ran every 15 minutes here
// ...
sleep($interval); // wait 15 minutes
} while(true);
?>
but it's accepted generally that PHP was not designed with this in mind.
1) Is there any drawback using the PHP way (How to stop this script?) or is there a better language to do this, like C++?
2) What other companies, like google who indexes the web, do?
Long-running scripts are generally a bad idea, as they're scripts for a reason. I'm not sure of a way to stop the PHP script once it runs, but there probably isn't a way to do so (at least practically). I'd recommend making writing a program that'll run on a server computer, using languages more designed to do that kind of work compared to scripts.
A simple C# or Java program can run forever, as long as you don't close it. You can manipulate databases by using the corresponding language's database support.
What you're doing is typically accomplished in PHP using cron jobs.
http://www.serverwatch.com/server-tutorials/a-primer-for-scheduling-cron-jobs-in-linux.html
(or google "cron job" + your OS of choice)
A cron job can be scheduled to execute an arbitrary script every 15 minutes pretty easily.
Designing PHP scripts to run forever is considered bad practices because PHP wasn't ever designed with that in mind.