For example:
$task->schedule(3500,$func);
function func(){ //do something}
How to implement schedule to execute callback function after 3500 seconds?
It's better if you make Cron Jobs:
Method to create Crob Job for every minute:
login in to server using ssh ..
type crontab -e
*/1 * * * * php SCRIPT_NAME --> this will run every minute
Related
It seems that only everyMinute() and cron('* * * * *') are working for me. Any other methods like everyFiveMinutes, everyTenMinutes, daily, dailyAt etc, aren't working at all and always return "No scheduled commands are ready to run". My cron entry is always * * * * * so the other methods should work as well right? And yes; I've actually tried waiting for the other methods including daily, excluding yearly :P
Cron entry: * * * * * /opt/alt/php72/usr/bin/php /home/retracted/domains/retracted/artisan schedule:run >> /dev/null 2>&1
Schedule entry:
$schedule->call(function () {
$stat = new Stat();
$stat->users = User::count();
$stat->reviews = Review::count();
$stat->scholen = School::count();
$stat->save();
})->daily();
So my questions: Why don't the other methods work? How do I make the other methods work, especially daily()?
So first you have to run your cronjob every minute that is correct. With that line you run your Laravel scheduler.
So i don't know the scheduler code but it's possible that the code runs only on that minute and not backwards.
So if you need a 5 minute cronjob you have to run your scheduler every minute and then define your duration in your scheduler task.
$schedule->call(function () {
$stat = new Stat();
$stat->users = User::count();
$stat->reviews = Review::count();
$stat->scholen = School::count();
$stat->save();
})->everyFiveMinutes();
So with the function ->everyFiveMinutes(); you can run the scheduler every five minutes.
For laravel custom cron jobs to work you have to do the following:
First setup an every minute cron by executing the command "crontab
-e" and adding the following line * * * * * php /var/www/html/crontutorial/artisan schedule:run >> /dev/null 2>&1
Configure the appropriate timezone on app/config.php eg 'timezone'
=> 'Europe/Berlin',
Create a custom command that you want to execute at a specific time.
If you don't know how to create custom commands please have a look
at laravel cronjob scheduling tutorial
Schedule custom crons in app/Console/Kernel.php by adding the
following lines of code
protected function schedule(Schedule $schedule)
{
$schedule->command('my:customcommand')
->cron('01 13 * * *');
}
The cron will run every day 1.13pm using the timezone configuration in app/config.php.
Did you try specifying with the timezone? Check the below working snippet from my project:
protected function schedule(Schedule $schedule)
{
try{
$schedule->call(function (){
(new MTSnapshot)->createSnapshot();
})->timezone('Asia/Kolkata')->dailyAt('23:57');
$schedule->call(function (){
(new MTTransaction)->schedulerStatus();
})->hourly();
$schedule->call(function (){
(new MTTransaction)->syncPendingTransactions();
(new MTCommission)->processPendingCommissions();
})->twiceDaily(1, 16);
} catch(\Throwable $t){
Log::emergency($t);
}
}
I want to create a queue (AMAZON SQS) that only runs jobs every X sec. So if suddenly 50 jobs are submitted, the end up in the queue. The queue listener then pulls a job, does something and waits X sec. After that, the next job is pulled. Another X sec pause. Etc etc
For the queue listener, the sleep option option only determines how long the worker will "sleep" if there are no new jobs available. So it will only sleep if there is nothing in the queue.
Or should I just put in a pause(x) in my PHP code?
[edit] I just tested the sleep method with a FIFO and standard AWS SQS queue and this messes up the whole queue. Suddenly jobs are (sucesssfully) resubmitted 3 times after which the go into failed state. Moreover, the delay that is given in my code (3-4 min) was ignored, instead a one minute was taken
<?php
namespace App\Jobs;
use App\City;
class RetrieveStations extends Job
{
protected $cities;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct ($cities)
{
$this->cities = $cities;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
// code here
doSomething()
sleep(X);
}
}
I have the exact same problem to solve. I'm using Laravel 5.8 and I don't see how I can get the queue worker to wait a fixed period between jobs.
I'm now thinking of using a scheduled task to handle this. I can schedule a task to run, say, every 5 minutes and run the following artisan command:
$schedule->command('queue:work --queue=emails --once')->everyFiveMinutes();
This will take one job from the queue and run it. Unfortunately, there's not much more granular control over how often a job is processed.
Exactly, you need to set asleep your php code, there is no other way.
Php sleep
Currently my cron job is scheduled to every 5 minutes.
*/5 * * * * curl http://localhost/aa_portal/refresh_id.php
currently this schedule is running every 5 minutes
Example : 12.00AM, 12.05AM, 12.10AM, ....
I need to run this Job every 5 minutes by 2 min gap
Example : 12.02AM, 12.07AM, 12.12AM, ....
Can I change this cron job command to full fill this requirement
Following the KISS principle you could just list the minutes
2,7,12,17,22,27,32,37,42,47,52,57
Example:
2,7,12,17,22,27,32,37,42,47,52,57 * * * * curl http://localhost/aa_portal/refresh_id.php
The following worked for me on Debian 11 using cron.
2-59/5 * * * * myCommand
0 2-59/5 * * * ? this worked for me, but it's java
I am using CodeIgniter for my website. I have to use cron job to run one of controller function. I am using route in website. And also I am not using index.php in URL.
e.g. http://example.com/welcome/show, here welcome is my controller and show is function name of that controller.
I have used like this,
0 * * * * php /home/username/public_html/welcome/show
It is giving 'No such directory'
How can I set cron jon in cPanel for above URL.
Use:
php index.php welcome show
as command in your crontab. E.g.:
0 * * * * php /home/username/index.php welcome show
Source (ver. 2.2.0)
http://www.codeigniter.com/userguide2/general/cli.html
Source (ver. 3.*)
http://www.codeigniter.com/user_guide/general/cli.html
Source (ver. 4.*)
http://codeigniter.com/user_guide/cli/cli.html
I have used below cron
php /full-path-to-cron-file/cron.php /test/index
source: http://www.asim.pk/2009/05/14/creating-and-installing-crontabs-using-codeigniter/
This works for me.
Thanks to all
You can try with this one:
wget api.example.com/index.php/controller/function
You can also try:
0 * * * * /usr/bin/curl --silent --compressed http://example.com/welcome/show
Or localhost
0 * * * * /usr/bin/curl --silent --compressed http://localhost/welcome/show
I hope that is helpful.
/usr/local/bin/php /home/username/public_html/index.php controllername methodname
This worked for me.
Here is the cron I use
/usr/bin/php /home/pia/www/jobs/index.php cron newsletter
Explanation:
a) $_SERVER['DOCUMENT_ROOT'] = /home/pia/www
b) codeigniter website root = /home/pia/www/jobs
c) 'cron' = controller name
d) 'newsletter' = method name
I have done it as
00 09-18 * * 1-5 /usr/bin/php /var/www/html/app/index.php crontest
crontest is the name of the controller which also uses a model to pull data from the database and send mail periodically (between 9 AM to 6 PM on Monday to Friday every week)
I just viewed this page which explains very detail with example. Hope this will be useful to others as well.
I am using codeigniter 3.0.3 and my server is hostgator. For me, the below format is working fine
*/15 * * * * /opt/php55/bin/php /home/username/public_html/myapp/index.php reminders index
above command runs every 15 minutes, reminders in command is controller name and index is method name.
watch -n60 curl [your application path]/check_banalce/user_balance
in my case im using codeigniter and the above command executes the user_balance function which is found in check_balance controller every 60 sec.
On a Linux EC2 intance, this worked:
*/5 * * * * /usr/bin/php /var/www/html/cifolder/index.php [module] [function]
If you are using the Hostgator(or any other Linux server) then try this one.
/opt/cpanel/ea-php72/root/usr/bin/php /YOUR_HOME_DIRECTORY/YOUR_USERNAME/public_html/marketing/index.php welcome emailcampaign 1
for example for me its
/opt/cpanel/ea-php72/root/usr/bin/php /home3/adnan/public_html/index.php welcome emailcampaign 101
where
welcome is the controller name
emailcampaign is the function name of welcome controller
101 = First argument of url.
Set up cron jobs through cPanel using this procedure:
Log on to your cPanel Interface.
Go to ''Advanced' section.
Click on "Cron Jobs".
Select the specific time from the lists provided.
You should enter the command to run in the "Command" field.
* * * * * php index.php controllername functionname
1st * - minute,
2nd * - hour,
3rd * - day of month,
4th * - month,
5th * - day of week.
For more info visit : https://crontab.guru/
I am using hostgator's cPanel.
I have created a user controller and run_cron_data function inside the user controller.
Command: wget www.example.com/index.php/user/run_cron_data
See the below screenshot
If you are using cPanel then Use the following command:
/usr/bin/curl -k http://example.com/welcome/show
This works perfectly for me.
I'm creating a web app where users can specify a time and date to run 2 scheduled tasks (one at the start date and one at the end date). As these are only run once each I didn't know if a cron job would be appropriate.
The other option I thought of would be to save all of the task times to a DB and run a cron job every hour to check if $usertime == NOW(), etc. But I was worried about jobs overlapping, etc.
Thoughts?
Additional: Many users can create many tasks that run 2 scripts each.
cron is great for scripts run on a regular basis, but if you want a one-off (or two-off) script to run at a particular time you would use the unix 'at' command, and you can do it directly from php using code like this:
/****
* Schedule a command using the AT command
*
* To do this you need to ensure that the www-data user is allowed to
* use the 'at' command - check this in /etc/at.deny
*
*
* EXAMPLE USAGE ::
*
* scriptat( '/usr/bin/command-to-execute', 'time-to-run');
* The time-to-run shoud be in this format: strftime("%Y%m%d%H%M", $unixtime)
*
**/
function scriptat( $cmd = null, $time = null ) {
// Both parameters are required
if (!$cmd) {
error_log("******* ScriptAt: cmd not specified");
return false;
}
if (!$time) {
error_log("******* ScriptAt: time not specified");
return false;
}
// We need to locate php (executable)
if (!file_exists("/usr/bin/php")) {
error_log("~ ScriptAt: Could not locate /usr/bin/php");
return false;
}
$fullcmd = "/usr/bin/php -f $cmd";
$r = popen("/usr/bin/at $time", "w");
if (!$r) {
error_log("~ ScriptAt: unable to open pipe for AT command");
return false;
}
fwrite($r, $fullcmd);
pclose($r);
error_log("~ ScriptAt: cmd=${cmd} time=${time}");
return true;
}
I'd do it like that, save settings in a database and check when needed if the task should start.
You could run a checking/initiating cronjob every minute. Just make sure the checking code is not not too heavy (exits quickly). A database query for a couple of rows shouldn't be a problem to execute every minute.
If the "task" is really heavy, you should consider a daemon instead of a cronjob calling php. Here is a good & easy-to-read introduction: Create daemons in PHP
Edit: I took for granted that even if the tasks are only ran "once each", you have multiple users which are 1:1 to the "once each", thereby jobs for each user. If not, at (as the comments says) looks worthy of an experiment.
Whatever mechanism you chose (cron/at/daemon) I would only put the start task into the queue. Along with that start task is to place the end task. That part can either place it into the future or it the time has elapsed start it immediately. That way they will never overlap.
I would also favour the PHP/DB and cron option. Seems simpler and gives more flexibility - could chose multiple threads etc if performance dicttates.