run Laravel Task Scheduling on hPane (Hostinger - Cron Job) - laravel

as Running The Scheduler says I must run this Command:
* * * * * cd /home/u285707107/domains/dmn.com/ && php artisan schedule:run >> /dev/null 2>&1
but in hPanel Cron Job-s I can't use special characters, so I create cron.sh file as they say
#!/bin/sh
/home/u285707107/domains/dmn.com/ && php artisan schedule:run > /dev/null 2>&1
and in Hostinger Cron Job I run Command:
/bin/sh /home/u00000000/domains/dmn.com/public_html/dmn/app/Console/cron.sh
but it don't works : ( (
My Kernel.php file is:
<?php
namespace App\Console;
use App\Models\Proview;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule){
$schedule->call(function () {
Proview::query()->update(['today' => 0]);
})->everyMinute();
}
protected function commands(){
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
can someone Help me ?

Just replace the below scripts with yours.
cron.sh
#!/bin/sh
cd /home/u285707107/domains/dmn.com && php artisan schedule:run >> /dev/null 2>&1
Hostinger Cron Job
* * * * * /bin/sh /home/u285707107/domains/dmn.com/public_html/dmn/app/Console/cron.sh

Related

Laravel Task Scheduler Server not working on Windows Server Xampp

Laravel Task Scheduler not works properly in windows XAMPP server. I make Database Auto backup script source. backup database is works fine but, Laravel scheduler not works properly in windows server
app/Console/Commands/DatabaseBackUp.php file created using php artisan make:command DatabaseBackUp command
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Carbon\Carbon;
class DatabaseBackUp extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'database:backup';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$filename = "backup-" . Carbon::now()->format('Y-m-d') . ".sql";
$command = "". env('DUMP_COMMAND_PATH') ." --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " > " . storage_path() . "/app/backup/" . $filename;
$returnVar = NULL;
$output = NULL;
exec($command, $output, $returnVar);
dd("Database backup Successfully Done - $filename Time:- ".Carbon::now());
}
}
?>
app/Console/Kernel.php file
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
'App\Console\Commands\DatabaseBackUp'
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->command('database:backup')
->daily()
->appendOutputTo(storage_path('logs/db-backups.log'));
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Now when I hit php artisan database:backup command manually then it is create Database backup...
but as per Official Doc. of Laravel 8 when I hit cd C:\xampp\htdocs\laravel-project && php artisan schedule:run >> /dev/null 2>&1 command it shows error like The system cannot find the path specified.
but when I hit the php artisan schedule:run command then it create database backup only once. not repeat task as we mentioned.
in Laravel Docs there are mentioned scheduler start command cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 it is works for Linux crontab Feature, for Windows we need to use Task Scheduler
for Linux
crontab -e
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
OR
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
For Windows (Trick 1)
create .bat file in your applications directory
db-Backup.bat file
write following line in db-Backup.bat file & save it
cd C:\xampp\htdocs\microtechcoupon && C:\xampp\php\php artisan schedule:run
2.press Windows + R, write Taskschd.msc and press enter & Task Scheduler will opens,
3.
fill the inputs fields as per need. for more detail see here how to fill fields in Task scheduler
For Windows (Trick 2)
If you wants to prevent command line won’t popup every single minute when scheduler runs your task. then you can use 2nd trick
press Windows + R, write Taskschd.msc and press enter & open Task Scheduler
fill the fields. but In the tab 'Actions' click on 'New', in the field 'Action' select
'Start a program'
then in settings section fill C:\xampp\php\php.exe in Program/Script input field & C:\xampp\htdocs\microtechcoupon\artisan schedule:run in Add arguments (optional) field & then save it
credit of 2nd Trick in windows :- https://quantizd.com/how-to-use-laravel-task-scheduler-on-windows-10/

Automatic backup not working in Laravel with Spatie

I'm trying to run backup using cron, i'm using spatie-laravel-backup it is working when i run it manually but when i run it using cron using this path on server:
/home/user/public_html && php artisan backup:run >> /home/user/logs 2>&1
and i'm getting this
/bin/bash: /home/user/public_html: Is a directory"
kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:run')->daily()->at('10:00');
}
run scheduler from cron
https://laravel.com/docs/8.x/scheduling#running-the-scheduler
* * * * * cd /home/user/public_html && php artisan schedule:run >> /dev/null 2>&1
then
you can use
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:run')->daily()->at('10:00');
}

Cron job issue(Task Schedule) in Laravel

Same problem schedule cron job,
my kernal.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Log;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
// \App\Console\Commands\SecondTable::class,
];
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
//->hourly();
$schedule->call('App\Http\Controllers\Sale\SaleController#sync')->everyMinute();
}
protected function commands()
{
require base_path('routes/console.php');
}
}
manually running a command-> php artisan schedule:run is working good!
but, cronjob run server is not working correctly,
my cron job code,
* * * * * php /laravel project folder/artisan schedule:run >> /dev/null 2>&1
is not working.
Check your cronjob code, it could be something like this:
* * * * * cd path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
It means: every seconds (* * * * *), go to my project folder (cd path-to-your-project), then execute the command (php artisan schedule:run).
The meaning of the last part of the cronjob is explained here: What is /dev/null 2>&1?

Laravel cron not working automatically

I'm working on a project in Laravel through scotch box. I am trying to automate some things through cronjobs. The problem is that my cron does not run automatticly, but when I php artisan schedule:run it runs my task perfectly.
app/commands/sendmail.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Mail;
class sendmail extends Command {
protected $name = 'sendMail';
protected $description = 'A mail has been send ';
public function fire()
{
Mail::send([],[], function($message) {
//sendmail function that works...
});
}
}
app/console/Kernel.php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;
use App\Battle;
use Carbon\Carbon;
use App\Console\Commands\Inspire;
use App\Commands\mails;
class Kernel extends ConsoleKernel
{
protected $commands = [
\App\Console\Commands\Inspire::class,
\App\Console\Commands\sendmail::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')
->everyMinute();
$schedule->command('sendMail')
->everyMinute();
}
}
crontab -e
# m h dom mon dow command
* * * * * php var/www/artisan schedule:run
The problem ended up being the use of an relative path versus absolute path.
Using the relative path defined as var/www/artisan will set the path according to present working directory. That would mean App/Console/var/www/artisan where no artisan was located.
Instead using an absolute path such as /var/www/artisan will set the directory directly to /var/www/artisan, which would be the correct location of artisan.
* * * * * php -d register_argc_argv=On /var/www/artisan schedule:run

Laravel scheduler is not running automatically

I have made a scheduler. When I call it with php artisan userRanking it works.
This is the code in Kernel.php:
protected $commands = [
\App\Console\Commands\UserRanking::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('userRanking')
->everyMinute();
}
How do I dispatch it so that it runs automatically?
You have to set up a single cron job that calls the schedule runner every minute:
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
Read Laravel's Scheduler docs for more info.

Resources