I'm trying to run a cron job for task scheduler in laravel 8 on cpanel
here is the command im entering in cpanel cron jobs page
usr/local/bin/php /home/webservername/public_html/laravelappfolder/artisan schedule:run >> /dev/null 2>&1
the cron job is set to run once every minute
and in the Kernel.php looks like this(the schedule function part):
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$d = DB::table('users')->where('userid', '=', 5)->first();
$newAge = $d->age + 1;
$d->update(['age'=> $newAge]);
})->everyMinute();
}
I'm doing a basic call to a function every minute, but nothing in the database is changing. I checked disable_functions thing but that line doesn't exist in my php.ini. I also tried doing Log::info('test') but no new logs to be found.
I also tried creating a command and calling it with its signature name and registering it in the $commands array, but same thing cron job not working
Related
I have a Laravel project that is already deployed on a live server using Hostinger's web service. I have a task scheduled to run every minute that will basically check if there are organizations that have already expired subscription dates, which will revert their subscription type back to "Free". I tried it first on my local machine and it works great.
However, when I tried to implement this task scheduling in Hostinger, it doesn't work.
I followed Laravel's official documentation for running a Scheduler on a live server. Since Hostinger doesn't allow special characters, I created a bash file containing the artisan run command with special characters following their article.
Here's the content of my created bash file:
/usr/bin/php /home/u482004401/domains/caviom.org/public_html/artisan && php artisan schedule:run > /dev/null 2>&1
app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
CharitableOrganization::whereDate('subscription_expires_at', '>=', now())
->update([
'subscription' => 'Free',
'subscribed_at' => null,
'subscription_expires_at' => null
]);
})->everyMinute();
}
When I try to view the output of my cron job in Hostinger, it just shows a generic message of a list of artisan commands.
I have test data on my database that should be updated with this Cron job, but it did not change at all. Has anyone successfully tried setting up cron jobs on Hostinger for a Laravel project?
Looks like I wrote the wrong syntax. I fixed it by replacing my bash file:
/usr/bin/php /home/u482004401/domains/caviom.org/public_html/artisan && php artisan schedule:run > /dev/null 2>&1
with
#!/bin/sh
/usr/bin/php /home/u482004401/domains/caviom.org/public_html/artisan schedule:run
Once I saw that the output is now what I expected, I then returned back the
/dev/null 2>&1
I must update my currency rate and I have this in my Plugin.php:
public function registerSchedule($schedule) {
$schedule->call(function () {
$url = "https://cbu.uz/ru/services/open_data/rates/json/";
$json = json_decode(file_get_contents($url), true);
file_put_contents("currency.json", json_encode($json[0]['G4']));
})->everyMinute();
}
I ran my cron job in cPanel
/usr/local/bin/ea-php72 /var/www/u1041398/public_html/agroparts.uz/artisan schedule:run >> /dev/null 2>&1
The path is correct I checked the PHP version it is correct also. My cron codes in plugin.php are correct also but it is not updating the currency rate. Is this command above correct to run cron job? I checked my code without cron and it worked successfully.
Cron job was not showing mistake. So, I created console command to update currency rate and run it and it showed
file_get_contents(): https:// wrapper is disabled in the server configurati
on by allow_url_fopen=0
So I used curl to get currency rate and worked
I am using Task Scheduling from Laravel, on local env. and till now I test it with php artisan word:weeklyUpdate , but I want to check if the Cron Job run automatically on a specific date, like in my code.
protected function schedule(Schedule $schedule)
{
$scheduler = new LkpSchedulerUpdateDate;
$scheduler = $scheduler->first()->toArray();
$schedule->command('word:weeklyUpdate')->weeklyOn($scheduler->date, $scheduler->time);
//ex: weeklyOn(3, 05:49:00)
}
Create a schedule that runs just some minuts after the current time (let's say 5 minuts from now), wait, then check the results.
Edit:
It's because when you run php artisan word:weeklyUpdate` you execute the command directly.
The scheduled task your wrote is equivalent to execute php artisan word:weeklyUpdate in console every week.
Also, in your locale did you activated the cronJob scheduler?
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
In my laravel application under routes/console.php have one command. I want to run this command every minute which is given below:
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->describe('Display an inspiring quote');
In digital ocean crontab i registered below command but not working.
* * * * * cd /var/www/html/laravel_projects/user_profile/ && php artisan schedule:run 1>> /dev/null 2>&1
N.B: In digitalocean cron status showing it's active and if i run this command under project directory manually & its' working.
You need to define a schedule to tell Laravel which commands you want to run.
Your task schedule is defined in the app/Console/Kernel.php file's schedule method.
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')->daily(); // or some other time
}
https://laravel.com/docs/5.8/scheduling#defining-schedules
Add task schedule inside App\Console\Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('reminders:send')->everyMinute();
}
I hope it helps.
sudo service cron restart
execute this command to restart cronjob service
With older versions of Laravel I only had to write a command in Forges controlpanel eg php /home/forge/default/artisan scheduled:run then set the interval and hit schedule button.
Now in Laravel 5.5 I can se that I can add jobs in the kernel.php file eg:
protected function schedule(Schedule $schedule)
{
$schedule->command('SomeJob:delete')
->daily();
}
Does this mean I no longer have to setup cron jobs in laravel Forge?
You still need one cron entry to start the scheduler:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1