creating cron Job command every minute - php

I have a question in cron jobs where i create a job to run a page every one minute.
If this page didn't finish its job completely (not executed completely) in this minute, then
will the cron job command run this page again from the beginning?
or will it run this page again but will allow the first to complete?
or will it wait for the page to complete and do the command?
or will it do some thing else????

Every cron job is run independently in a separate sub process, so the cron job will run irrespective of whether some other job is running or not.
Hence, if your script is taking more than one minute to run, and you have scheduled it to run every minute, it will have unintended consequences because two process, or n+1 process for script execution time in range [n, n+1) minutes, will now be running in different execution stage.
This will be true for any overlaps in execution time of cron jobs.

Related

Execute Long running php scripts via cron jobs

I have a php script that updates a database. It takes round 5 minutes to complete. I want to set it to run automatically via cron job on cpanel but it don't execute via cron job.
Is there any time execution limit on cron jobs or is there any other effective way to automate this process on server?
set_time_limit() might help you
documentation
you can use supervisor to keep command line tasks running

Cron jobs delayed because of one super long one

So I have cron jobs running every minute. These cron jobs all finish within 30 seconds. However, my last cron job is random and only starts working when it finds the data in the system. Sometimes there is a lot of data and the cron job usually takes about 10 minutes to finish, which I am perfectly fine with. However, once this cron job initiates and the minute passes, the other cron jobs aren't started until the cron job is done. So I have about 15+ cron jobs waiting for that one cron job to finish and then they start up again.
I used withoutOverlapping and it doesn't seem to work? Is there a solution?
Here is a list of my jobs:
$schedule->command('sms:schedule')->everyMinute();
$schedule->command('sms:sendbulk')->everyMinute();
$schedule->command('sms:notifyevent')->everyMinute();
$schedule->command('voice:sendbulk')->everyMinute();
$schedule->command('email:sendbulk')->everyMinute();
$schedule->command('email:schedule')->everyMinute();
$schedule->command('voice:schedule')->everyMinute();
$schedule->command('sms:sendrecurringsms')->everyMinute();
$schedule->command('sms:sendrecurringvoice')->everyMinute();
$schedule->command('email:sendrecurringemail')->everyMinute();
$schedule->command('event:recurring')->everyMinute();
$schedule->command('senderid:validation')->dailyAt('12:01');
$schedule->command('invoice:recurring')->dailyAt('12:01');
$schedule->command('reports:daily')->dailyAt('06:00');
$schedule->command('checksms:status')->everyMinute();
$schedule->command('checkroom:status')->everyMinute();
$schedule->command('location:minute')->everyMinute();
$schedule->command('location:checkcircle')->everyMinute();
$schedule->command('recurring:setsmsname')->everyMinute();
$schedule->command('recurring:setvoicename')->everyMinute();
$schedule->command('site:schedulingevents')->everyMinute();
$schedule->command('sms:contactnameset')->everyMinute();
$schedule->command('daily:checklocation')->withoutOverlapping();
That last one is the one that has differentiating times. But I need the other ones to keep running every minute if that is the case.

handling concurrency in cron php

I am setting up a cron job that executes a script page.php every minute
I wanted to know about the concurrency.
The script it self take 15 minutes to complete, so how the cron will run?
Lets take a time frame of 15 minutes, how many crons will run in that time? 1 or 15?
will cron runs till its completion or it can be interrupted?
Thanks.
When you start a php script via cron it will run until it finishes or the execution time is exceeded.
The system will always start a new instance of the script whether the last script is still running or not.
If you know that the script is running a long time you should set up a bash script that checks if a script is already running and only start the php script if this is not the case.
Cron doesn't handle concurrency for you - your script must do so.
Turns out, this is quite easy, as there is a trivial PHP operation, that is atomic: unlink().
We tend to use it this way:
a global flag file enables the script
The main cron job does nothing but touch a timer flag file
Every minute, another cron job starts the worker script
This worker script tries unlink() on the global flag file
If it does not suceed (result evaluates to false), it quits (This makes sure, only one instance runs at a time)
This worker script tries unlink() on the timer flag file
If it does not suceed (result evaluates to false), it touch()es the global flag file and quits (This makes sure, one cron run starts at most one instance)
Now it runs
after finishing, it touch()es the global flag file to restart the cycle
This way, cron runs, that would result in concurrency, are left out in a loggable way.
it depends in your scheduled task/cron settings. You can configure to launch 15 times your PHP script, or launch it when the previous job is done.
Your scheduled task/cron can be interrupted by it self, by an external signal or also by the configuration you made in scheduled task/cron (stop job if it is not finished in N minutes...)
Hope that helps :)

Creating multiple cron jobs with PHP

I'm in need of a way to create dynamic, one-off cron jobs to execute tasks at different times. Ideally, I would like to achieve this using PHP, so that when a user completes a certain action, the cron job is created and scheduled for a time that is calculated based on the time that the user completes said action. At any one time, there could be multiple cron jobs scheduled at once for different times. These cron jobs also need to be deleted upon completion.
I have tried searching around for something appropriate, however haven't encountered anything that works as I need. If anybody could point me in the right direction, that would be greatly appreciated.
Cheers
A possible solution:
Setting a Cron job that calls to CronJobManager.php every second.
Just make a regular daemon that calls for the CronJobManager.php.
Create a cronjob table in your database
The cron job table should contain these basic fields: path (to php file), run_time(datetime), last run (datetime) and type (like suicidal, if as you explain you want some cron jobs to delete themselves)
Connect CronJobManager.php with the cronjob table
Every time CronJobManager.php runs (that is, every second), loads the cron jobs. Then, comparing "now"'s time with each cron job's run_time you'll get which cron jobs to run.
For example, if cron job "foo" run_time is set to 18/04/2014 22:02:01, CronJobManager will run it when reaching that moment.
Notice that if Cron jobs executing time needs a lot of time, they'll get delayed and eventually a second or two will get lost.
Now, for every cron job that needs an execution, you would execute the related php file of that cron job, indicated in the path.
This is a general idea, and of course you would have to extend it with for example cron job states (idle, running, stop, etc).
In order to delete cron jobs you would implement this feature in the cron job object.
That is: the Cron Job class, once it has executed what it had to do, it would check its type (as defined in database). If it is 'suicidal', then it would delete the database row.
UPDATE
I updated the answer but I want to note something. If what you need is several cron jobs to run at once, in a specific second with 0 delay, then you need a cron job per task out of php that runs a specific file.
To achieve this functionality, you need to have a daemon that is running all the time that checks for these dynamic jobs and launches them. The setup is a tad complicated to put together, but if you are ready for such an endeavor, you can use the project PHP Resque Scheduler.
https://github.com/chrisboulton/php-resque-scheduler
You start up a daemon that runs all the time and then you can add jobs to a dynamic queue to be executed at any specified time in the future. I think you will find this suitable to everything you are looking to do.

Run script only once for 5 minutes (Linux)

I have a php script that I'd like to run for a specific amount of time (e.g. 5 minutes), but only once. With cron jobs this will run indefinitely. Is there another way?
The way to handle this is:
When something triggers to need for the cron job to run, put a flag somewhere that the cron job will read.
Have cron job that runs all the time asking "do I need to run?". It checks hte flag. If it sees it, it deletes the flag and runs for specified time. If not, just wait until it next runs (to check for the flag)
In the actual cron job, set "max_execution_time" to 5 minutes. Then it shouod stop at (or just over) 5 minutes. To be more precise, include a timer in your script running in a loop, if you can.

Categories