I have created a php yiic command as
class StartCommand extends CConsoleCommand {
public function run($params){
Yii::log("Cron Job has started");
}
}
When I run the command php yiic Start, I get the result in the log.
And I have add this to the crontab
* * * * * /usr/bin/php /path/app/console/commands/StartCommand.php
But the job is not running.
I think I have to mention the php yiic Start to the crontab.
But where do I add it ??
* * * * * cd /path/app/console/commands && php yiic Start
Related
I am using Laravel 7 and AWS lightsail LAMP server for my application.
When I am running this
php artisan schedule:run
command manually it's working properly. But, when I am creating a cron job for this it's not working.
Here is my cron code as laravel documention:
* * * * * cd /opt/bitnami/apache/htdocs && php artisan schedule:run >> /dev/null 2>&1
Here is my app/Console/Karnel.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 = [
Commands\DeactiveCoupon::class
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('coupon:deactive')->everyMinute();
$schedule->command('queue:restart')->everyMinute();
$schedule->command('queue:work')->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
But, if I add this cron:
* * * * * cd /opt/bitnami/apache/htdocs && touch mytestfile.txt >> /dev/null 2>&1
It's creating a new file named mytestfile.txt
But, this Laravel command not working in cron!
* * * * * cd /opt/bitnami/apache/htdocs && php artisan schedule:run >> /dev/null 2>&1
How can I solve this?
Yes on live server you can register it through cron job
then you can register you command in cron job. you can ask you server provider for this command and then register like this
command look like this
/usr/local/bin/php /home/escuelar/your folder name/artisan schedule:run 1>> /dev/null 2>&1
After I run
which php
I got
/opt/bitnami/php/bin/php
and my cron code is:
* * * * * cd /opt/bitnami/apache/htdocs && /opt/bitnami/php/bin/php artisan schedule:run >> /dev/null 2>&1
It's working for me
Thanks #apokryfos bro
I have this simple command in symfony :
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class AuctionEndCommand extends Command
{
protected function configure()
{
error_log(print_r('test',true), 3, "/tmp/error.log");
$this->setName('desktop:auction_end')->setDescription('Execute when auction is end.')->setHelp("Identify winners for auctions");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// outputs multiple lines to the console (adding "\n" at the end of each line)
$output->writeln([
'User Creator',
'============',
'',
]);
// outputs a message followed by a "\n"
$output->writeln('Whoa!');
// outputs a message without adding a "\n" at the end of the line
$output->write('You are about to ');
$output->write('create a user.');
}
}
Now when I execute : /var/www/myProject/bin/console desktop:auction_end this command works fine. But when I try to execute as a cron in linux this script doesn't start :
In linux as sudo I did :
nano crontab -e, and the cron :
* * * * * /usr/bin/php /var/www/myProject/bin/console desktop:auction_end > /dev/null
What I'm doing wrong, can you help me please ? Thx in advance and sorry for my english
I can keep it very short. You will need to use PHP to execute the console.
* * * * * php -q /usr/bin/php /var/www/myProject/bin/console desktop:auction_end > /dev/null
Writing the output of a cron job that you are testing to a file will help you debug errors. All output is now lost in the void :)
* * * * * php -q /usr/bin/php /var/www/myProject/bin/console desktop:auction_end > /home/<user>/crons/auction_end.cron.txt
Edit
It might be that your php should be used as absolute path.
* * * * * /path/to/php /path/to/bin/console symfony:command
Or even by specifying the user to execute the command with:
* * * * * root /usr/bin/php /path/to/bin/console command:to:execute
Also make sure that the root user has permission to execute files in your symfony project.
What user are you executing the cron as?
Symfony requires write access to ( depending on version ) app/cache && app/log(s) || var/cache && var/log(s) if the user you're executing the command as doesn't have write access to the directories you're trying to write to then your operation will fail.
A good way of fixing this would be to check the error logs, either in var/log(s) app/log(s) or check the apache2 error logs in /var/log/apache2/error.log I would guess one of these logs will contain a hint to your problem.
If you run it as root, does that work:
* * * * * root php /var/www/myProject/bin/console desktop:auction_end > /dev/null
See if that works.
I have a task scheduled to run an artisan command, and whenever i run artisan command it is executing every time irrespective of time i have given to the cron job.
Here is the console i'm using
class CronJobConsole extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'sms:note';
/**
* 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()
{
//executing function;
}
$this->info('Remainder SMS send successfully !!');
}
}
and this is my console/kernel.php file
class Kernel extends ConsoleKernel
{
protected $commands = [
\App\Console\Commands\Inspire::class,
\App\Console\Commands\FileEntryData::class,
\App\Console\Commands\CronJobConsole::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('sms:note')->dailyAt('19:00')->sendOutputTo('cronjob-result.php');
}
}
when i run the php artisan sms:note it is executing every time. I want it to execute on particular time that i have gven.
Please help me out.
This problem, as you specify it, is just artisan's normal behaviour.
You have scheduled the command, so it will be executed on the specified time driven by Laravels internal scheduler (read below cronjob). But you can also manually execute the command, by entering it in CLI.
Note: executing your command manually has nothing to do with artisan's scheduler. The scheduler only executes this command whenever you see fit.
So the command you are looking for is:
php artisan schedule:run
This commands loops through the registered commands on the specified time, so it will only run your command when the time is ready.
To make sure Laravel executes your command when it's time, create a cronjob (on your server) to run the php artisan schedule:run command every minute, Laravel takes care of the rest.
From the docs (insert in crontab):
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
Goodluck!
I have defined a command and make it can run every minutes in Kernel with schedule. But when i use artisan command to run, it only run once time.So, i want to make it run auto every minutes in Xampp in window 10. But how can i add :
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1 to my server
Can i have run scheduler automatic?
In :
public function handle()
{
$banMember = MemberAccount::where('isBan',1)->get();
foreach($banMember as $ban)
{
$ban->isBan = 0;
$ban->save();
}
}
Here are some hints for you to do the task scheduling
https://www.drupal.org/docs/7/setting-up-cron-for-drupal/configuring-cron-jobs-with-windows
http://www.z-cron.com/
* * * * * means that it has to be running every minute.
I have statusUpdate.php file in the
xampp\htdocs\project\app\Console\Commands folder.
statusUpdate.php :
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
class statusUpdate extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'status:update';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Update Job status daily';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$affected = DB::table('jobs')->update(array('status' => 1));
}
}
It is created by the following Laravel official documentation.
Then I was added \App\Console\Commands\statusUpdate::class, class in Kernel.php on xampp\htdocs\project\app\Console folder.
Here is the karnel.php file code:
<?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\statusUpdate::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('status:update')
->everyFiveMinutes();
}
}
Then I was run
php artisan schedule:run command using CMD on windows 7.
Now it is working fine(in local server). My jobs table status field is updated properly by 1.
But when I was deployed this project on the shared hosting and added a CRON command for my server in cPanel:
Cron job command is like this : php /path/to/artisan schedule:run 1>> /dev/null 2>&1
Now in this case command not working & this is the problem. how can I solve it?
Well. I am giving you the answer as per what you have said.
Cron job command is like this : php /path/to/artisan schedule:run 1>> /dev/null 2>&1
The path should be locating the artisan file in the server. Like this:
Let's say your artisan file location is /var/www/artisan, then the simple answer could be do like this:
php /var/www/artisan schedule:run 1>> /dev/null 2>&1
Just check if that works. Thank You!
UPDATE:
This is how it should look like.
This worked fine for me
/usr/local/bin/php /path/to/artisan schedule:run >> /dev/null 2>&1
You should add the command from the cPanel server as
/usr/local/bin/php /home/xyz/public_html/artisan schedule:run 1>> /home/xyz/public_html/log_laravel 2>&1
This will keep all the logs in /home/xyz/public_html/log_laravel
Running scheduled command: '/opt/cpanel/ea-php71/root/usr/bin/php' 'artisan' SyncAPIOrders:orders > '/dev/null' 2>&1
In my case cron Job was not working, if you are about to schedule the command as once a day (i.e., 00:00) iff, same time is not reflected in a $schedule->command(); object
If the commands were incorrect, I used to get this warning in my Email as
PHP Warning: Module 'magickwand' already loaded in Unknown on line 0
Status: 404 Not Found
X-Powered-By: PHP/5.6.37
Content-type: text/html; charset=UTF-8
No input file specified.
In Kernel.php you should specify
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('SyncAPIOrders:orders')
->timezone('Asia/Kolkata')
->dailyAt('00:00');
}