Cron job of my custom command in Laravel 4.2 - laravel

I know there is Laravel 5.* now but for many reasons I'm using Laravel 4.2
I have a custom command, and in the fire() method, I import a file and seed the information in my database, so my class is some like this
class SeedDataFile extends Command {
protected $name = 'import:file';
public function fire(){
$command = 'mongoimport --db things --collection users --type csv --file file.csv --headerline';
$result = exec($command, $output, $return);
}
}
I want the file that is seeding data, i.e. every 12 hours (considering the file every 12 hours is changing with new data), but as an user I don't want to type the command in my terminal:
php artisan import:file
..every 12 hours (I just want to upload the new file to my project).
So the cron job is where I do all the work, but I don't want to do this:
crontab -e
and edit the file
I want to setup the schedule of my command in one class o somewhere in the code, and automatically the custom command is running everyday until a determined date.
Is this possible? Or I have to configure the crontab file?

You could take a look at this package, description says that it does exactly what you want
https://github.com/Indatus/dispatcher

As is Laravel 4.2 does not offer this type of functionality. In fact you'll need to set up 1 cron job for this functionality no matter the version you're using.
Newer versions of Laravel do indeed provide this functionality, but they still require you to set up 1 cron job (scheduler on Windows) at highest frequency available for Laravel's command php artisan schedule:run.
If you want to have scheduling of your commands in your code (rather than cron file) you could replicate what newer versions of Laravel do.
1. you would create a "master" command, let's call it TaskSchedulerCommand and let's say it's signature is php artisan schedule.
2. you would create a cronjob at highest frequency on the TaskSchedulerCommand like this: * * * * * php /path/to/artisan schedule`
3. you would write all the scheduling logic of other commands within the TaskSchedulerCommmand

Related

Laravel not executing scheduled command

I'm using laravel telescope. I used the following code to schedule the prune command in kernel.php (it has 48 hours but I already tried with less hours):
$schedule->command('telescope:prune --hours=48')->daily();
I tested it in my local environment and it prunes the data correctly. But once in my production server it doesn't seem to be working.
Telescope is runing there and capturing data. I thought it could be a telescope command issue, but I ran
php artisan telescope:prune
and it works, it says "25404 entries pruned", I also verified this in the DB. Also, my cron task is runing every minute, and I have some scheduled calls like this:
$schedule->call('*some irrelevant stuff*')->cron('0 */3 * * *');
$schedule->call('*some irrelevant stuff*')->dailyAt('00:00');
And they are working as expected. I also tried calling the scheduller manually inside a function:
Artisan::call('schedule:run');
and the schedule calls work, but "$schedule->command" does not.
So, I'm guessing that the problem is that somehow "$schedule->command" is not working. Do you have any idea or suggestion to fix this?
You should set Laravel Task Scheduler to your's servers crontab file, adding the following entry:
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1

How to run command in Laravel kernel after another successful command

I'm creating a custom command which will truncate a table every thirty minutes in the console kernel (for development purposes). I want to run another right after the previous command.
P.S: I have an if statement which prevents running these commands on the production server.
$schedule->command('db:seed')->after(function () use ($schedule) : void {
$schedule->command('my-command:remove-users-from-tables')
->everyThirtyMinutes()
->environments(['demo', 'local']);
});
I expect to run the seeder right after "my-command" runs successfully every thirty minutes. However, in this way, only db:seed runs.
If you want to run B after A you need to schedule A and AFTER that run B:
$schedule->command('my-command:remove-users-from-tables')
->everyThirtyMinutes()
->after(function() {
$this->artisan->call('db:seed');
});
I have checked the source code for Illuminate\Console\Scheduling\Schedule class.
I think when we say:
$schedule->command(...);
The artisan command will be scheduled, not run straightaway.
So when you write like this:
$schedule->command('first-command')->after(function () use ($schedule) {
$schedule->command('second-command');
});
The second command will be registered, not run right after the first command.
So the best approach that I can think of is run the second command inside the first command according to this link
You might try something like this:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RemoveUsersFromTable extends Command
{
public function handle()
{
// Do something to remove users from table.
$this->call('db:seed');
}
}
An alternative that may be relevant in some cases (but I can imagine would also be a bad idea in others) is to run shell_exec: https://www.php.net/manual/en/function.shell-exec.php
If you want to chain two artisan commands together as you would on the cli using && you can simply shell_exec('php artisan command:first && php artisan command:second').
shell_exec returns the console output so if your commands print anything (ie, via $this->info) then you can print that to console like so: $this->info(shell_exec('php artisan command:first && php artisan command:second'))
I'm probably going to get a lot of hate for this answer...

Laravel scheduled job did not resume after maintance mode

I did a maintanace on my laravel app by php artisan down. Then again after 15 minutes I did php artisan up.
But the scheduled jobs have not still resumed. They are not being executed as it was before.
Schedueled jobs are not being executed even when I manually give the command. ex: php artisan my:command
I think by making logs of your cronjobs will help you.
First Method
remove this part from you cronjob
>> /dev/null
Now add log file path in you cronjob
* * * * * php /var/www/html/project/artisan schedule:run >> /var/www/html/project/mylog.log 2>&1
create a log file and this file 777 (writable) permission.
so you can check what actually happening
Second Method
Or you can use method appendOutputTo() in your kernal.php
$schedule->command('inspire')->everyMinute()->appendOutputTo($filePath);
$filePath is a path of a log file you can give a public path or storage path

Laravel commands like tinker

I am wondering if there's any way to run an ad-hoc command in laravel? like in tinker, but without tinker?
I want to be able to run an ad-hoc command but without using tinker ?
Example:
"App\Post::orderBy('timestamp', 'desc')->first()"
I want to run this every 5 seconds?
Yes you can write orn comands in artisan. It is very useful.
Classes are kept in app/console
More: https://laravel.com/docs/5.5/artisan

Laravel4 task to cron

I'm trying to create a cron using a task I made. The problem is I have no clue how to build the cron. My task name is meeting:close, and it's in my commands folder.
Can anyone help me to build the url to call this task every hour? I guess it will start with 0 * * * *
Thanks everyone!
To create a cronjob, you have to edit a file. It is pretty easy, run crontab -e to edit the cron file for the current user.
Now just add a line to the file that opens:
0 * * * * php /full/path/to/your/application/artisan meeting:close
Maybe you also have to specify the absolute path to your php executable
Now save the file and you're all set.
By the way, if you didn't know, you can run your commands with php artisan command:name from the terminal (that's also what the cronjob is going to do...)

Resources