Laravel cron not working automatically - laravel

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

Related

Cron run schedule not running automatically on the server

I have this cron job code in Laravel-5.8 application:
App/Console/Commands:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Hr\HrDesignation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use Exception;
use Notification;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
class UpdateCreateDesignation extends Command
{
protected $signature = 'updatecreatedesignations';
protected $description = 'Update or Create Designation';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$client = new Client();
$res = $client->request('GET','https://api.cloudjkk.net/jkkapp/profile/all-designations', [
'query' => ['key' => 'dddddd']
])->getBody();
$clientdatas = json_decode($res->getContents(), true);
foreach($clientdatas as $clientdata)
{
if ($clientdata['job_title']) {
$designation = HrDesignation::updateOrCreate([
'designation_name' => $clientdata['job_title']
],
[
'description' => $clientdata['job_description'],
'company_id' => 1,
]);
}
}
}
}
For the schedule, I have this code in the kernel which is supposed to run by 1:00a.m. every day
kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
'App\Console\Commands\UpdateCreateDesignation',
];
protected function schedule(Schedule $schedule)
{
$schedule->command('updatecreatedesignations')
->dailyAt('01:00');
}
protected function commands()
{
require base_path('routes/console.php');
}
}
I observe that the cron job is not running on the server as scheduled by 01:00.
There is no error.
How do I resolve this?
Thanks
Laravel needs to run the php artisan schedule:run every minute. If the current time is 01:00, that task is executed.
So you need to:
Add a cronjob every minute * * * * *.
Go to the directory cd /path-to-your-project
Run the artisan command that will match the time php artisan schedule:run
You can test this if you add a job with ->everyMinute();
$schedule->command('increment-db-int')
->everyMinute();
You will need to make sure that you have enabled the scheduler in crontab
https://laravel.com/docs/7.x/scheduling#introduction
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

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?

php artisan command to make tests file inside a particular folder

Currently I have started learning about Unit Testing in Laravel 5.6.
By default my laravel project has a 'tests' directory inside which I have 2 more directories namely, 'Features' and 'Unit'. Each of these directories contain a 'ExampleTest.php'
./tests/Features/ExampleTest.php
./tests/Unit/ExampleTest.php
Whenever I create new test file using command
php artisan make:test BasicTest
It always creates the test file inside the 'Features' directory by default, where as I want the file to be created under the 'tests' directory.
Is there a command using which I can specify the path fro creation of the test file.
Something like this
php artisan make:test BasicTest --path="tests"
I have already tried the above path command but it is not a valid command.
Do I need to change some code in my phpunit.xml file?
php artisan make:test Web/StatementPolicies/StatementPolicyListTest
It will by default create a file namely StatementPolicyListTest under StatementPolicies(if not exist it will create a new folder of this name) folder under tests/Feature/Web
Use this command
php artisan make:test BasicTest --unit
Also you can use
php artisan make:test --help
to see available options
You must be create your custom artiasn command
<?php
namespace App\Console;
class TestMakeCommand extends \Illuminate\Foundation\Console\TestMakeCommand
{
/**
* The console command name.
*
* #var string
*/
protected $signature = 'make:test-custom {name : The name of the class} {--unit : Create a unit test} {--path= : Create a test in path}';
/**
* Get the default namespace for the class.
*
* #param string $rootNamespace
* #return string
*/
protected function getDefaultNamespace($rootNamespace)
{
$path = $this->option('path');
if (!is_null($path)) {
if ($path) {
return $rootNamespace. '\\' . $path;
}
return $rootNamespace;
}
if ($this->option('unit')) {
return $rootNamespace.'\Unit';
}
return $rootNamespace.'\Feature';
}
}
Register it in kernel
<?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 = [
TestMakeCommand::class
];
......
}
Then you can use
php artisan make:test-custom BasicTest --path=
or
php artisan make:test-custom BasicTest --path=Example

Laravel 5.1: Run Custom Artisan Command in Background

I'm working on a chat application using the Ratchet package. With the help of tutorials I've written a custom artisan command to start the Websocket server. I need to run this Artisan command in the background and it should be running all the time. How do I do it?
I tried using Artisan::queue and Artisan::call from Artisan Facade. But since my custom command runs indefinitely(for a long time) it isn't working.
Edit:
My hosting provider is not allowing me to run Artisan commands through ssh.
Here is the code for the Custom Artisan Command:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use App\Classes\Socket\ChatSocket;
use App\Classes\Socket\Base\BaseSocket;
class ChatServer extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'chat_server:serve';
/**
* 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 mixed
*/
public function handle()
{
$this->info("Start server");
$server = IoServer::factory(
new HttpServer(
new WsServer(
new ChatSocket()
)
),
8080
);
$server->run();
}
}
You simply should run it in console with command:
php artisan chat_server:serve
And probably you should make sure it will work all the time. One of the ways is using Supervisor to make sure this command will run (almost) all the time

Clear views cache on Lumen

Few weeks ago, I had the same problem in Laravel 5.1, which I could solve with this solution.
However, now I'm facing the same issue in Lumen, but I can't call php artisan view:clear to clear the cached files. There is any other way?
Thanks!
There's no command for the view cache in lumen, but you can easily create your own or use my mini package found at the end of the answer.
First, put this file inside your app/Console/Commands folder (make sure to change the namespace if your app has a different than App):
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class ClearViewCache extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $name = 'view:clear';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Clear all compiled view files.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$cachedViews = storage_path('/framework/views/');
$files = glob($cachedViews.'*');
foreach($files as $file) {
if(is_file($file)) {
#unlink($file);
}
}
}
}
Then open app/Console/Kernel.php and put the command inside the $commands array (again, mind the namespace):
protected $commands = [
'App\Console\Commands\ClearViewCache'
];
You can verify that everything worked by running
php artisan
inside the project's root.
You will now see the newly created command:
You can now run it like you did in laravel.
EDIT
I've created a small (MIT) package for this, you can require it with composer:
composer require baao/clear-view-cache
then add
$app->register('Baao\ClearViewCache\ClearViewCacheServiceProvider');
to bootsrap/app.php and run it with
php artisan view:clear

Resources