Laravel CRON job not working in aws - php

I have deployed laravel 5.4 app in AWS Ubuntu 16.04 apache2, i have created task scheduler for sending emails dailyAt('10:00').
When i run the artisan command php artisan email:reminder manually every thing works fine.
But when i run php artisan schedule:run i am getting No scheduled commands are ready to run.
I have also ran * * * * * php /var/www/html/app/artisan schedule:run >> /dev/null 2>&1 referring to documentation.
This is Kernal.php
class Kernel extends ConsoleKernel
{
protected $commands = [
\App\Console\Commands\EmailReminder::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('email:reminder --force')->dailyAt('10:00');
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}

Your schedule will only work if you call it at 10:00,
add this cronjob
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
Laravel will call this function everyminute, and once it is 10:00 it will call the function accordingly.
Check https://laravel.com/docs/5.5/scheduling#introduction , search for 'Starting The Scheduler'

In AWS ECS we can use this without adding cron in to the container
https://github.com/spatie/laravel-cronless-schedule
php artisan schedule:run-cronless

Related

Laravel 5.8 cron job for php artisan schedul:run

I added cron job this in my host:
* * * * * cd /home/mydomain/public_html/ && php artisan schedule:run >> /dev/null 2>&1
And kernel file:
protected function schedule(Schedule $schedule)
{
file_put_contents(base_path('public/temp/test.txt'),'test_string');
}
But it doesn't create the file and when I added this in my cron job, It creates folders every minute.
* * * * * cd /home/mydomain/public_html/ && mkdir test_folder >> /dev/null 2>&1
And when I run this command in terminal php artisan schdule:run it creates the test.txt file.
Because you didn't call it by $schedule,
check this out: https://laravel.com/docs/5.8/scheduling#defining-schedules
And your code need to be like this:
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
if (!file_exists(public_path('temp'))) {
mkdir(public_path('temp') , 0777);
}
file_put_contents(base_path('public/temp/tset.txt'),'test_string');
})->dailyAt('2:10');
}
If you use php artisan schedule:run it will automatically fire the events: "schedule:run source code"
And you don't have any events, it will display No scheduled commands are ready to run..
However, that file be created is because you overwrite the schedule function, it supports to be empty originally. schedule
wrap your function with $schedule
$schedule->call(function () {
file_put_contents(base_path('public/temp/tset.txt'),'test_string');
})->everyFiveMinutes();
Or Make command class and run through it
Read Defining Schedules

Laravel 5 schedule issues - php artisan schedule:run works but actual schedule does not

Laravel version 5.7, PHP version 7.2 running on Apache2
The command in my crontab is as follows:
* * * * * php /var/www/dev-site/artisan schedule:run >> /dev/null 2>&1
My kernel is as follows:
<?php
namespace App\Console;
use App\Console\Commands\ImportInvoiceBatch;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
ImportInvoiceBatch::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('import:invoice-batch')->everyMinute()->withoutOverlapping();
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Running my defined command as "php artisan import:invoice-batch" works correctly, and even running "php artisan schedule:run" recognizes everything and runs the task as expected, but the scheduler is not being executed ever minute as should be happening. Is there anything I am missing? Thanks in advance.
Try to change working directory to app root before running artisan:
* * * * * cd /var/www/dev-site/artisan && php artisan schedule:run >> /dev/null 2>&1
Also you could try to add output path for debuging:
* * * * * cd /var/www/dev-site/artisan && php artisan schedule:run >> /var/log/crontab/artisan.log 2>&1

My laravel's task scheduling not working properly

Trying to do task scheduling but not working properly. I set the time and wrote the commands but not running.
Here is my commands;
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\allestates;
protected $signature = 'bot:mitsui';
Also my Kernel is here:
protected $commands = [
//
Commands\mitsui::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('bot:mitsui')->everyFiveMinutes();
}
What am I missing here? Thanks for helping me out!
Are you running the schedule cron.? You need to add this into your cpanel crons or cronjob.
php {project_path}/artisan schedule:run > /dev/null 2>&1
If not add the above command to your cron jobs and run it everyminute.
Here the steps if you have root access,
crontab -e
//Add follow line to crontab
* * * * * php {project_path}/artisan schedule:run >> /dev/null 2>&1

Laravel 5.2 - cron job not working on ubuntu

I am setting cron job first time in Laravel 5.2. As per their docs, I have modified my app/Console/Kernel.php as -
protected $commands = [
\App\Console\Commands\Inspire::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->call(function() {
$myfile = fopen("testfile.txt", "w");
}
)->everyMinute();
}
And start scheduler -
* * * * * php /var/www/html/{project_dir}/artisan schedule:run 1>> /dev/null 2>&1
I want to run this cron every minute. But it is totally not working.
If I tried with php artisan schedule:run cron runs and "testfile.txt" generated in root folder. But why it is not calling automatically every minute? I don't know what's going wrong here.
Any help is appreciated.
Thanks.

Laravel 5 schedule not working

My laravel version is 5.0.28, I build on cloud9, and I added this command to my cron:
#!/bin/bash
PATH=/usr/bin
* * * * * php /home/ubuntu/workspace/app/artisan scheduled:run 1>> /dev/null 2>&1
I added this code on my Kernel.php. I referenced this site: https://laravel-news.com/2014/11/laravel-5-scheduler/
<?php namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Http\Controllers\ApiController;
class Kernel extends ConsoleKernel {
protected $commands = [
'App\Console\Commands\Inspire',
];
protected function schedule(Schedule $schedule)
{
$schedule->call('ApiController#test_job')->hourly();
}
}
I waited and it still didn't work, so I tried to use the command php artisan schedule:run, and I got: No scheduled commands are ready to run.
I searched and found this answer: Laravel 5 "Class does not exist" when using the scheduler
So I modified my code. Also, this code had no specified time, so I modified my cron to specify a time, but it still doesn't work. I have no more ideas. please help. Thanks.
code
$schedule->call(join('#', [ApiController::class, 'test_job']));
cron
0 0,3,6,9,12,15,18,21 * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1
30 1,4,7,10,13,16,19,22 * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1
first to Test if cron is running in your server or localhost type:
> sudo service cron status
if not installed:
> sudo apt-get install cron
to enable laravel's scheduler:
> crontab -e
and you can select an editor if not vim opens directly. Be sure to enter there this line at the bottom:
* * * * * php /path_from_root_to_laravel_proj_folder/artisan schedule:run 1>> /dev/null 2>&1
to Test if you have setup inside laravel the scheduler right, run this from your projects folder:
>php artisan schedule:run
this should execute the tasks and tell you what is doing.
Laravel scheduler works with commands, not with controller methods:
create command:
php artisan make:command PurchasePodcast
edit command:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PurchasePodcast extends Command
{
protected $name = 'purchase:podcast';
public function fire()
{
// do stuff here
}
}
add command to Console\Kernel.php:
protected $commands = [
'App\Console\Commands\PurchasePodcast',
];
use command in scheduler:
$schedule->command('purchase:podcast')->hourly();
In my case, the scheduler actually run but encountered an error because of lower php version, beside artisan path (which is your project folder), I had to set the php path as below:
* * * * * /path_to_php_folder/bin/php /path_from_root_to_laravel_proj_folder/artisan schedule:run 1>> /dev/null 2>&1
There are several aspects to find the cause:
First, check whether the 'timezone' in config/app.php is set properly. Laravel will reset the timezone even though you already configured it in php.ini.
Secondly, check that crontab is working as expected. When you get the message "No schedule to be ready", it means your crontab is running and can detect the php and artisan command.
In order to complete #limonte's answer the create Console Command is the following:
php artisan make:console CampaignsCollect --command=campaigns:collect
Reference here: link

Categories