Php artisan schedule:run only once execute command in localhost - laravel-5

I created a command artisan to make a text file in my path. This command executes correctly by running the command but not for task scheduling in my localhost (not server).
CreateFile class:
protected $signature = 'create:file';
public function handle()
{
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "parisa";
fwrite($myfile, $txt);
fclose($myfile);
}
Kernel.php:
protected $commands = [
Commands\CreateFile::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('create:file')->everyMinute();
}
When I run php artisan schedule:run my command executes only once at that moment. But I want to execute every minute. How to fix it?

Related

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 5.2 Cron job doesn't work

I want to create a cron job for test.
When i run the cron in log file appear the line but after not. I set cron job every minute.
I used php artisan schedule:run ..
Command dbTS
public function handle()
{
Log::info('Functioneaza !');
}
Console Kernel
protected function schedule(Schedule $schedule)
{
$schedule->command('dbTS:demo')
->everyMinute();
}
protected $commands = [
Commands\Inspire::class,
Commands\dbTS::class,
];

Laravel schedule works but command not working

With laravel schedule I have problem. schedule runs but command not working. here is example.I send mail from schedule on every minute and also want to run command on every minute to test it. command not working. I did't get email from command(with text "command executed").What I am doing wrong?
Please help.
Kernel.php
class Kernel extends ConsoleKernel {
protected $commands = [
'App\Console\Commands\myCustomCommand'
];
protected function schedule(Schedule $schedule) {
//this works
sendMailFunction('someone#gmail.com', 'test mail', 'schedule executed');
//this is not working
$schedule->command('command:myCustomCommand')->cron('* * * * *')
}
}
Command
class myCustomCommand extends Command {
protected $name = 'command:myCustomCommand';
protected $description = 'some description';
public function handle() {
sendMailFunction('someone#gmail.com', 'test mail', 'command executed');
}
}
in localhost :php artisan schedule:run
in host :* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
You have to see what it is
or
Introduce your controller in kernel.php

Artisan: "Command not found"

So I try to add a Command with
php artisan command:make MailSendCommand
The file MailSendCommand.php is created.
I edited it to this:
class MailSendCommand extends Command {
protected $name = 'command:send-mail';
protected $description = 'Send mails to the users.';
public function __construct()
{
parent::__construct();
}
public function fire()
{
$this->info('Befehl wird ausgeführt' );
}
...
In my file start/artisan.php I added
Artisan::add(new MailSendCommand);
but when I enter 'php artisan command:send-mail' it says:
Command "command:send-mail" is not defined.
It just worked on my Home PC (XAMPP) but not on my live server (PHP 5.5.15 (cgi-fcgi))
'php artisan clear:cache' and 'php artisan dump-autoload' did not help.
Go to app/Console/Kernel.php and add the class to the commands variable. It would look something like:
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\MyCustomCommand::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}
}
Here i added MyCustomCommand as a valid artisan command. Check it is among available commands executing:
php artisan list
It looks as though the problem is related to your setup. You're using PHP CGI instead of CLI to run your commands which will not work.
Ensure that the code is there on your live environment and run your commands on CLI, as these are command line scripts.

Resources