Laravel 4.2 Indatus/dispatcher - Command no defined - php

I'm using Indatus/dispatcher for Laravel 4.2. It is basically a Cron job based Task Scheduler.
I am trying to run a Cron job every minute, and I am getting this error on the live server (but it works fine on my local machine). Here is what I have done:
<?php
use Indatus\Dispatcher\Scheduling\ScheduledCommand;
use Indatus\Dispatcher\Scheduling\Schedulable;
use Indatus\Dispatcher\Drivers\Cron\Scheduler;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class TestCommand extends ScheduledCommand {
/**
* The console command name.
*
* #var string
*/
protected $name = 'command:check';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Check if the meberships are verified.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* When a command should run
*
* #param Scheduler $scheduler
* #return \Indatus\Dispatcher\Scheduling\Schedulable
*/
public function schedule(Schedulable $scheduler)
{
return $scheduler->everyMinutes(1);
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
Log::info('I was here # ' . date('H:i:s'));
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
// array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
// array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
So basically, I'm just writing a line in my log file (i.e. Storage/laravel.log) every time the scheduler is run. When I do php artisan scheduled:run on my local machine, it writes a new line in my log file.
Now, I've uploaded the code on my server and have created a Cron job as:
The Cron job is running every minute as expected and the log file is also being updated every minute, but instead of the message I am writing in log file, its writing following error every minute:
[2016-09-24 09:20:01] production.ERROR: exception 'InvalidArgumentException' with message 'Command "command:check" is not defined.
Did you mean this?
command:make' in /home/mhjamil/public_html/l4-cron/test/vendor/symfony/console/Symfony/Component/Console/Application.php:564
Stack trace:
#0 /home/mhjamil/public_html/l4-cron/test/vendor/symfony/console/Symfony/Component/Console/Application.php(190): Symfony\Component\Console\Application->find('command:check')
#1 /home/mhjamil/public_html/l4-cron/test/vendor/symfony/console/Symfony/Component/Console/Application.php(124): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#2 /home/mhjamil/public_html/l4-cron/test/artisan(58): Symfony\Component\Console\Application->run()
#3 {main} [] []`
And by the way, I've registered the command in app/start/artisan.php as
Artisan::add(new TestCommand);
Also the command php artisan command:check runs successfully on my local machine, but when I upload it to my server it says Command "command:check" is not defined.
And one more thing, on my server I have added a route and did Artican::call("command:check");. Now whenever I reload this page, it logs an entry successfully in the log file. But its giving error when done through cron job. So I am guessing the problem is somewhat related to cron job or server settings.
I have tried changing cron command to:
/usr/bin/php /home/mhjamil/public_html/l4-cron/test/artisan command:check 1>> /dev/null 2>&1
previously I was using schaduled:run but still same issue :(

Related

Laravel Command Schedule Not Working Properly

I'm building a project with Laravel 7.28 on localhost. I need to update a PDF every hour. For the beginning I created a command:
<?php
namespace App\Console\Commands;
use App\Event;
use Illuminate\Console\Command;
class PDF extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'pdf:update';
/**
* The console command description.
*
* #var string
*/
protected $description = 'All Country PDFs updated';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$event = new Event();
$event->user_id = 1;
$event->save();
echo 'done';
}
}
It just insert a record into events table and works fine. Then I edited the Kernel.php under App\Console directory.
<?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 = [
Commands\PDF::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('pdf:update')->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__ . '/Commands');
require base_path('routes/console.php');
}
}
Finally, I run php artisan schedule:run. I was expecting that the command runs every minute but it just runs once. is this a problem on localhost or did I do something wrong?
The php artisan schedule:run just runs once by its definition:
The schedule:run Artisan command will evaluate all of your scheduled
tasks and determine if they need to run based on the server's current
time.
If you want to do it every minute you should use a scheduled process control system like supervisor or crontab or etc. (more info here)
In case if you are using laravel 8.x and running on a development/local server you can use the following command and it will work for you:
php artisan schedule:work
For Laravel 7 you can also use below command to run in your local
while true; do php artisan schedule:run; sleep 60; done
On windows localhost
First of all set your desired schedule job on the run method inside
App\Console\Kernel More information on the Laravel website page here
Secondly on the command line run the following
command
php artisan schedule:work

Execute Server Commands on Laravel 7 Scheduler

I have 2 commands in my Laravel 7 Schelduler
$schedule->command('inspire')->everyMinute()->emailOutputTo(env('MAIL_TO'));
$schedule->exec('whoami')->everyMinute();
✅ The first one works perfectly, I get the email
❌ This second one doesn't work at all
$schedule->exec('whoami')->everyMinute();
I followed: https://laravel.com/docs/7.x/scheduling
Any hints for me ?
My guess is that whoami runs fine but nothing is done with the output.
Can you try to add emailOutputTo(env('MAIL_TO')); to the second command to see if you get an email with the output ?
Please check the documentation about outputting the result from exec: https://laravel.com/docs/7.x/scheduling#task-output
as #Clément Bisaillon suggests, you have forgotten to add method for shell command output.
but your comment has been Raised a new Question.
Why it works with whoiam and date, but not working with history ?
This Works:
$schedule->exec('cat /home/abilogos/.bash_history ')->everyMinute()->appendOutputTo('/home/abilogos/Desktop/testHist.txt');
you can find history file in with echo $HISTFILE
BUT WHY?
it gets even more interesting when you just which history to find history path and it tells you there
which: no history in Your Path
like source command.
because they are not Programs stored in $PATH locations. they are bash`s command
http://manpages.ubuntu.com/manpages/bionic/man7/bash-builtins.7.html
and laravel uses php #proc_open (Symfony\Component\Process\Process) which just execute Programs not Bash commands :
https://www.php.net/manual/en/function.proc-open.php
Temporarily
I created this
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class exec extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'exec {cmd}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Run any command(s)';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$cmd = $this->argument('cmd');
$this->info(shell_exec($cmd));
}
}
and use it like this
$schedule->command('exec date')->everyMinute()->emailOutputTo(env('MAIL_TO'));
$schedule->command('exec history')->everyMinute()->emailOutputTo(env('MAIL_TO'));
It works !!

how to change laravel jobs time

I'm developing the app for the customer and he wants to start some jobs in special time
I must run it in jobs, that's right?
for example, he wants to publish a post have 2 status published or waiting
and in send page, he can set time for publish post
how I can develop this in jobs?
ScanJob::dispatch($property->Name, $property->Owner, $Scan->id)->delay(Carbon::now()->addHour(Carbon::now()->diffInHours($Time)));
it's my first try
get diff time in hours and add it from delay
There are basically two ways via which you can solve your problem:
Create a Laravel Artisan command(you can use other methods also that Laravel provides, but I found Artisan to be fun and more flexible, helps avoid the rework) and schedule it accordingly.
Create a Queued Job and dispatch it for some later time, but it has some limitation like, the Amazon SQS queue service has a maximum delay time of 15 minutes.
Now, what is to be done:
In my opinion, you should use Solution 1 as it is more flexible and gives you more control.
Queues are used for 2 things. First, ideally, the task you want to perform should be done in the next 30-45 minutes. Second, the task is time intensive and you don't want to block the thread because of that.
Now the FUN part.
Note: You need not worry, Laravel will perform the majority of the steps for you. I am mentioning each and every step for the sake of not skipping the knowledge.
Step 1: Run the following command to create an Artisan Console Command(Remember to be in your project's root path.):
php artisan make:command PublishSomething
The command will now be available for further development at app/Console/Commands.
Step 2: You will see a handle method inside the Class like following, this is where all of your logic will exist.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PublishSomething extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'something:publish';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Publishes something amazing!';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
//
}
}
Step 3: Let's add some logic inside our handle method
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$this->info('Publishing something cool!');
// you can add your own custom logic here.
}
Step 4: After you have added your logic, now we need to test it, you can do so like:
php artisan something:publish
Step 5: Our function is running all fine. Now we will schedule the command. Inside app/Console you will find a file Console.php, this class is responsible for all task scheduling registration, in our case.
<?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 = [
//
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Notice the schedule function here, this is where we will add the schedule logic.
Step 6: Now we will schedule our command to run every 5 minutes. You can change the time period very easily, Laravel provides some pre-made frequency options, and you have your own custom schedule also.
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('something:publish')->everyFiveMinutes(); // our schedule
}
Step 7: Now, Laravel's task scheduler itself is dependent on Cron. So to start the schedule, we will add the following file to our crontab.
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
That's it! We are done. You have created your own custom command and scheduled it for every 5 minutes.
You can learn more about Laravel Artisan Command & Laravel Task Scheduling.
Hope it helps!

How to run Cron Job on Shared Hosting? (Lumen)

I am trying to implement a cron job in my lumen project. I have BookingMaster Table when a user is creating a booking I am setting the default status to B means booked in the table. in the day of booking I am trying to update the status to I to database means In-progress. When I am doing this locally the cron is running perfectly and the status is also updating.
But when I moved this code to my shared hosting it is not working any more. The cron is not updating the status in database.
Location Of the BookingUpdate.php is - app/Console/Commands/BookingUpdate.php
BookingUpdate.php
<?php
namespace App\Console\Commands;
use Helpers;
use Illuminate\Console\Command;
use App\BookingMaster;
class BookingUpdate extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'BookingUpdate:booking-update';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Cron for Booking Update';
public static $process_busy = false;
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle(){
if (self::$process_busy == false) {
self::$process_busy = true;
$where['status'] = 'B';
$update = BookingMaster::updateRecord(6,$where);
self::$process_busy = false;
echo 'Done';
return true;
} else {
if ($debug_mode) {
error_log("Process busy!", 0);
}
return false;
}
}
}
karnel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
'\App\Console\Commands\BookingUpdate',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
//
}
}
Cron Job Command :
/usr/local/bin/php -q /home/rahulsco/public_html/api.pawsticks/artisan schedule:run 1>> /dev/null 2>&1
There might be a several issues with your code not running in Cron.
Maybe the correct path to PHP is not on the /usr/local/bin/php try running Cron with just php -q
Some systems require the script to start with #!/usr/bin/env php or some similar combination.
There is a space in your cron command on this part artisan schedule:run so it might work once you put your command in quotation marks and escape spaces
php -q "/home/rahulsco/public_html/api.pawsticks/artisan\ schedule:run" 1>> /dev/null 2>&1
Finally, if anything else fails I would try logging something to a file and checking after cron runs, maybe there is some other error in your directory configuration causing your script to fail before writing to database and the cron is running fine...
In app/Console/kernel.php, the schedule function should be like this:
protected function schedule(Schedule $schedule) {
$schedule->command('BookingUpdate:booking-update')->daily();
}
Then your BookingUpdate process will run daily at midnight. There are various other options to schedule your task as mentioned here: https://laravel.com/docs/7.x/scheduling#schedule-frequency-options
Also, you can simply execute the cron manually at any time using: php artisan BookingUpdate:booking-update
PS. Replace BookingUpdate:booking-update with $signature variable value defined in your command class you need to execute.
Tested with shared server - linux with laravel/lumen API.
It's working seamlessly.
lynx -dump "here your url for api or any external url you want call"
Note: Wrap your URL inside double quote

Tailing a log within Laravel never executes Symfony Process callback

I'm trying to tail a log file using a console command to detect specific errors. However, the call back in the run(...) portion of my script is never called in the Symfony Process:
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
class MonitorLogs extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'monitor:logs {log}';
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$command = "tail -n 1 -f " . escapeshellarg($this->argument('log'));
(new Process($command))
->setTty(true)
->setTimeout(null)
->run(function ($type, $line) {
$this->info('test');
});
}
}
I tried tracing with Xdebug any my break point at $this->info() is never reached. I can add lines to the log file I am testing with and they show up in my console while the script is running, but that line to output the word test is never hit.
What is wrong here?
Please remove setTty(true) and the output should display accordingly

Categories