using log rotator in laravel - php

Consider the following:
This is a conf file, in: conf/log_rotation/laravel_rotate.conf
storage/logs/laravel.log {
missingok
notifempty
compress
size 1024k
daily
create 0640
}
Is triggered by this laravel task run daily:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Logrotater extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'log_rotater';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Rotates the logs on an hourly basis';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
exec("logrotate -f config/log_rotation/tweet_rotate.conf");
exec("logrotate -f config/log_rotation/laravel_rotate.conf");
}
}
When I do: php artisan log_rotater I get:
error: config/log_rotation/laravel_rotate.conf:1 unknown option 'storage' -- ignoring line
error: config/log_rotation/laravel_rotate.conf:8 unexpected }
Ideas as to why I am getting this error?

logrotate requires path provided to be absolute, otherwise it treats given string as configuration option. That's why you're getting unknown option storage error.
Replace
storage/logs/laravel.log
with the absolute path.

Related

Why pcntl_fork() not defined in Laravel Controller?

I have Controller:
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct(){}
public function index()
{
pcntl_fork();
}
}
Then I call index() by HTTP request, And I get:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to undefined function App\Http\Controllers\pcntl_fork()
Then I try this:
class CodeSheet extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'code_sheet';
/**
* 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()
{
pcntl_fork();
echo "1\n";
}
}
Then I call this command:
vagrant#homestead:~$ php artisan code_sheet
1
1
So my question is, Why I can call pcntl_fork() in command, but can't in HTTP request?
The PCNTL extension is disabled in web environments (it probably even only compiles for the CLI SAPI), this has nothing to do with Laravel.
The reason for that is simple - the web server itself (or PHP-FPM) is in control of process management, so using this extension would create a conflict with it.
It's also not available under Windows as it's a UNIX-specific thing.

Laravel: Interface 'Illuminate\Contracts\Queue\QueueableCollection' not found

I am creating a console (Artisan) command to run a custom-made package. The import of the package and all of its functions is working just fine, but I can't seem to query any eloquent models without having the following error pop up:
[Symfony\Component\Debug\Exception\FatalErrorException]
Interface 'Illuminate\Contracts\Queue\QueueableCollection' not found
Here is my code...
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Property;
use Sync;
class SyncTool extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'sync:all';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$credentials = [
...
];
$sync = new Sync( $credentials );
$properties = Property::all(); // this throws the error
}
}
Step 1 : Remove full vendor folder
Step 2: delete /bootstrap/cache/services.php, /bootstrap/cache/compiled.php
Step 3 : Run from your terminal composer install
try this

ARGUMENTS and OPTIONS with Laravel Illuminate Console Command

It's pretty clear how to create a simple command for the CLI in Laravel and this goes also for creating arguments, but I can't seem to find a simple documentation on how to create options for the CLI.
I want to make it so that the user can add options after the argument.
So for example:
php artisan cmd argument -a
php artisan cmd argument -a -c -x
How do I implement such a structure in the class below?
Updated code
There are indeed a few possible solutions. It was actually quit easy.
class cmd extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'cmd {argument}
{--s : description}
{--x : description}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$var = $this->argument('argument');
$options = $this->options();
new Main($var,$options);
}
}
There are a lot of posible solutions for this, but I prefer to add optional arguments and if they exist do determinate actions with the ? that means argument can exist or not, plus * this means can be more tan one, like this:
class cmd extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'cmd {argument} {-extra*?}';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$var = $this->argument('argument');
if($this->argument('-extra')) {
//do things if -extra argument exists, it will be an array with the extra arguments value...
}
new Main($var);
}
}
There a whole doc section dedicated to creating commands from scratch and it's well documented. Look through it.
https://laravel.com/docs/5.4/artisan
If you need to learn from examples then have a look here into all the laravel's built in console commands.
vendor/laravel/framework/src/Illuminate/Foundation/Console

Laravel console command not working

trying to setup an example console command on laravel 5.2 but it's not working
I ran php artisan make:console CoolCommand
Here's my file
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CoolCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'be:cool';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Allows you to be cool';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
echo "Yes you are very cool!";
}
}
When I hit php artisan the command is not listed under the given signature
What am I missing?
Thanks
If it's not listed when you type php artisan, you forgot to register the command as described here. Open app/Console/Kernel.php and enter your command.
protected $commands = [
Commands\CoolCommand::class
];

Set Up CronJob In CPanel Laravel

i set up a cronjob in cpanel, i'm using laravel 4.2. i also set to send the cronjob to my email and below yellow part is the error i received in email.
[InvalidArgumentException]
There are no commands defined in the "user" namespace.
//laravel command.php
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class CronDelFilesCommand extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'user:active';
/**
* 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 fire()
{
echo "aaa";
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
//command in cpanel
* * * * * /usr/bin/php /home/project/public_html/artisan user:active > /home/project/public_html/log.txt
what's wrong? what's the error mean?
UPDATED
thanks to #KristianHareland i use wget. :)
I think you forget to add this command to artisan app/start/artisan.php.
You shoud wright:
Artisan::add(new CronDelFilesCommand());
And don't forget about composer dump-autoload
More info you can find here.

Categories