Laravel Task Scheduler Server not working on Windows Server Xampp - windows

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/

Related

Laravel database backup schedule

I am trying to set up a scheduler for weekly database backup in laravel. I have created a command and filled out some data like the command itself and description, and registered it in the console kernel as well. The issue is that the file never gets created and/or stored in storage.
This is the part of the code where is the command:
public function handle()
{
Log::info('Database backup completed.');
$filename = 'mysite' . Carbon::now()->format('Y-m-d') . ".gz";
$command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " | gzip > " . storage_path() . "/app/backup/" . $filename;
$returnVar = NULL;
$output = NULL;
exec($command, $output, $returnVar);
}
This is the kernel part:
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
OtherCron::class,
DatabaseBackupCron::class,
];
/**
* Define the application's command schedule.
*
* #param Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('othercron')->dailyAt('00:00');
$schedule->command('database-backup:cron')->everyFiveMinutes();
}
Note: I have used "->everyFiveMinutes()" just for testing purposes :)
You could use a ready-made lib for that:
github.com/spresnac/laravel-artisan-database-helper
and then just call the command in the scheduler as you like ;)
You can also set the full path to your mysqldump binary, if it's not in your path ;)
I tried your code, it is working for me, the only difference is in Kernel, I wrote like this.
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)
{
//some other commands
$schedule->command('database:backup')->weekly();
}
now the moment I run php artisan database:backup a file with the extension .zp created in the storage folder
php artisan make:command DatabaseBackUp
Use this command make databasebackup file.
app/Console/Commands/DatabaseBackUp.php
<?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') . ".gz";
$command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " | gzip > " . storage_path() . "/app/backup/" . $filename;
$returnVar = NULL;
$output = NULL;
exec($command, $output, $returnVar);
}
}
In this step, we need to create "backup" folder in your storage folder. you must have to create "backup" on following path:
storage/app/backup
Now, in this step, we need to schedule our created command. so let's update kernel file as like bellow:
app/Console/Kernel.php
<?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('database:backup')->daily();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
you can check following command to getting database backup with this command:
php artisan database:backup
t will create one backup file on your backup folder. you can check there.
Now, we are ready to setup cron on our server.
At last you can manage this command on scheduling task, you have to add a single entry to your server’s crontab file:
Run following command:
crontab -e

Laravel Schedule Only Exceute Command on first Run

I create a schedule to send email everyday, but for testing if it works i make it run every minute.
here is my kernel.php
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\SendExpiredReminder::class,
];
/**
* 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('reminder:send')
->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
it run every minute but reminder:send only called first time i run the schedule with php /var/www/html/artisan schedule:run >> /dev/null 2>&1
when i run php /var/www/html/artisan schedule:list it show that next due is next minute indicated the scheduler is running. What is going wrong?
your crontab should be to your laravel project folder, not www folder
* * * * * cd /var/www/html/artisan && php artisan schedule:run >> /dev/null 2>&1
note the project folder

How to let cron decide the time to run laravel schedule daily?

I don't want to set server cron to run every minute (* * * * *) so I set it to #daily
#daily usr/bin/php /home/dss/laravelAppDss/artisan schedule:run >> /home/dss/public_html/example.txt
This is my Kernel.php
<?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\SinkronDataDosen',
'\App\Console\Commands\SinkronDataMahasiswa',
'\App\Console\Commands\SinkronPotensiDO',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('SinkronDataDosen');
$schedule->command('SinkronDataMahasiswa');
$schedule->command('SinkronPotensiDO');
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
But it doesn't sync the data
This is the output example.txt
Running scheduled command: '/usr/bin/php7.0' 'artisan' SinkronDataDosen > '/dev/null' 2>&1
Running scheduled command: '/usr/bin/php7.0' 'artisan' SinkronDataMahasiswa > '/dev/null' 2>&1
Running scheduled command: '/usr/bin/php7.0' 'artisan' SinkronPotensiDO > '/dev/null' 2>&1
The commands worked if I run each command signature individually so there is nothing wrong with the commands
How do I solve it?

How to Run Laravel Scheduler on Google Cloud

I am trying to run Laravel Scheduler in Google Cloud.
In my command, I have this
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\User;
use Carbon\Carbon;
class ChangeRole extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'command:update';
/**
* The console command description.
*
* #var string
*/
protected $description = 'This Changes Stuffs';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
//
$users = User::where('trialExpires', '<=', Carbon::now())->get();
foreach ($users as $user) {
$user->type= 'subscriber';
$user->save();
}
}
}
I have done this in my Kernel
protected $commands = [
//
'App\Console\Commands\ChangeRole',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('command:update')
->daily();
}
In my cron.yaml file
cron:
- description: "daily job"
url: '* * * * * cd /project_id && php artisan command:update >> /dev/null 2>&1'
schedule: every 24 hours
retry_parameters:
min_backoff_seconds: 2.5
max_doublings: 5
timezone: Europe/Madrid
When I ran gcloud app deploy cron.yaml
I got this error
ERROR: (gcloud.app.deploy) An error occurred while parsing file: [C:\xampp1\newlibri\cron.yaml]
Unable to assign value '* * * * * cd /starlit-advice-260914 && php artisan command:update >> /dev/null 2>&1' to attribute 'url':
Value '* * * * * cd /starlit-advice-260914 && php artisan command:update >> /dev/null 2>&1' for url does not match expression '^(?:^/.*$)$'
in "C:\xampp1\newlibri\cron.yaml", line 3, column 8
I don't know what I did wrong.
When I run PHP artisan command:update. The scheduler work just fine. But I am unable to replicate on Google Cloud
I have been able to fix the error.
I have to set the URL to a get request in my application.
cron:
- description: "daily summary job"
url: /api/checksub
schedule: every 24 hours
retry_parameters:
min_backoff_seconds: 2.5
max_doublings: 5
timezone: Europe/Madrid
In my controller, I have this
public function checksub() {
$users = User::where('trialExpires', '<=', Carbon::now())->update(['role'=> 'subscriber']);
));
}
It doesn't need to setup Laravel Scheduler. Just call the get result of the URL
You might want to use the default implementation for these scheduler tasks. Basically what you do is setup a CRON task which runs the scheduler function every minute, and let the PHP code decide whether to run particular tasks.
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
This will run your app\Console\Kernel.php::schedule() function every minute, in which you can then define your tasks like:
// Example function which removes users older than a year.
$schedule->call(function () {
User::query()
->where('created_at', '<', Carbon::now()->subYear())
->delete();
})->hourly();
See https://laravel.com/docs/7.x/scheduling

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?

Resources