Scheduler in Laravel Forge not working correctly - php

As per the Forge documentation regarding task scheduling with Laravel scheduler, I have set up a schedule on forge with the command
php8.1 /home/forge/default/artisan schedule:run
User is forge and frequency is set to every minute
My keneral.php has a schedule that is supposed to go off only right before midnight at whichever timezone it is for the user. However, when the scheduler runs on forge it seems like the command is getting executed every minute instead of checking what the schedule function has to say about it.
protected function schedule(Schedule $schedule)
{
$users = User::all();
foreach ($users as $user) {
$timezone = $user->timezone;
$schedule->command('nullDay:daily')->timezone($timezone)->between('23:50', '23:58');
}
}
This schedule function works great in local when running schedule:work so I tried use that instead of schedule:run in the forge command input but it doesn't seem to work.
This is the output when I have the php8.1 /home/forge/default/artisan schedule:run with the kernal code above:
[2022-01-06T20:07:02+00:00] Running scheduled command: '/usr/bin/php8.1' 'artisan' nullDay:daily > '/dev/null' 2>&1

Try it might work
cd /home/forge/default && php8.1 artisan schedule:run
Might also follow
https://forge.laravel.com/docs/1.0/resources/scheduler.html
https://mattstauffer.com/blog/laravel-forge-scheduling-a-cron-job/
https://laravel-tricks.com/tricks/laravel-scheduler-cpanel-cron-job

Related

How to auto run schedualing in Laravel

I'm new in Laravel and I need to swlwtw otps expired in my otps database, I create an expiration file and define all things, and the command runs perfectly when I use
php artisan schedule:run
this is the output:
2022-09-17 16:37:15 Running ['artisan' Otp:expire] in background .......................................... 7ms DONE
⇂ ('/usr/bin/php8.1' 'artisan' Otp:expire > '/dev/null' 2>&1 ; '/usr/bin/php8.1' 'artisan' schedule:finish "framework/schedule-7ff27dde37314470633aef84f65f27b83fd05b4e" "$?") > '/dev/null' 2>&1 &
but when I run the server with php artisan serve and I add an OTP the OTP didn't be deleted after the time expected, it deletes only when I use schedule run.
any help, please?
The official laravel documentation suggests running the php artisan schedule:run command every minute by using a cron (Cronjob etc.). You can do it by using the following cron entry:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
The best way is to use Supervisor, which runs your command constantly, and when the process finishes the new one will start immediately.
If you use Cronjob, you should know that It runs every process for a minute minimum! Please check the Laravel documentation

do i have to do something to have the schedule run automatically every minute?

I created this function but it only works once when I execute this command:
php artisan schedule: run
Should I have to do something to continue automatic? is it then triggered automatically when you enter the site only once and run it alone?
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$dias1 = \Carbon\Carbon::today()->subDays(5);
$dias2 = \Carbon\Carbon::today()->subDays(15);
$encomendasnaopagas = encomendas::where('estado', 1)->where('updated_at', '<', $dias1)
->join('distritos', 'encomendas.distrito', '=', 'distritos.id')
->update(['estado' => 5]);
$encomendasenviadas = encomendas::where('estado', 3)->where('updated_at', '<', $dias2)
->join('distritos', 'encomendas.distrito', '=', 'distritos.id')
->update(['estado' => 4]);
})->everyMinute();
}
This may be something you have missed in the documentation on keeping the scheduler running
https://laravel.com/docs/8.x/scheduling#defining-schedules
When using the scheduler, you only need to add the following Cron entry to your server. If you do not know how to add Cron entries to your server, consider using a service such as Laravel Forge which can manage the Cron entries for you:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. When the schedule:run command is executed, Laravel will evaluate your scheduled tasks and runs the tasks that are due.
You will need to setup an entry in your crontab or similar cron management system.

laravel 5.8 scheduler not working automatically have to trigger manual

i am running command php artisan schedule:run and it will work . but i want to update thing everyMinute(); automatically without trigger command every time .if i have to trigger it mannualy then what is the meaning of scheduler
my command file
public function handle()
{
$update = Roi::find(12);
$update->level = 48;
$update->save();
}
2.kernal.php
protected function schedule(Schedule $schedule)
{
$schedule->command('level:update')->everyTwoMinutes();
}
i am checking updates in updated_at timestamp in database
That might solve your issue, it's the way you start the scheduler:
php artisan schedule:run >> /dev/null 2>&1
That way you will run every minute and check whatever is due to execute it.
https://gist.github.com/Splode/94bfa9071625e38f7fd76ae210520d94 windows not supported for command line task schedule
I think running schedule not working on windows locally
the only way worked for me is to create task in windows and related with .bat file
note: bat file content is
#ECHO OFF
php path-of-laravel-project\artisan schedule:run
PAUSE

Task helpers not running Laravel, but cron is running every minute

I have a task I'm trying to run every day. In my Kernel.php I have the following command:
$schedule->command('emailProgram')->daily()->timezone('US/Central');
I'm my crontab I have:
* * * * * php /var/www/html/appname/artisan schedule:run >> /dev/null 2>&1
So, When I run php artisan schedule:run, or run it directly with php artisan emailProgram it runs as expected. But, its not running on its own using daily()/dailyAt() or otherwise. Lastly, if I remove daily() from the command in the kernel.php file:
$schedule->command('emailProgram')->timezone('US/Central'); it's running every minute, so its like there is some disconnect with the Laravel task helpers. This is my first time setting up Cron, and task management with Laravel so maybe I'm overlooking something simple. Any help would be really appreciated, thanks.

Starting the Laravel cron job on a Mac

I have a command scheduled in the Laravel 5.4 scheduler and would like to start the Laravel cron on Mac OS X El Capitan.
app/Console/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\GetToken'
];
protected function schedule(Schedule $schedule) {
$schedule->command('gettoken')->everyMinute();
}
protected function commands() {
require base_path('routes/console.php');
}
}
My GetToken.php makes an API call and then a DB change. I believe that this is working properly, as I can run the task directly from the cli using:
php /path/to/project/artisan schedule:run 1>> /dev/null 2>&1
To edit my cron file I use:
env EDITOR=nano crontab -e
I then add:
* * * * * php /path/to/project/artisan schedule:run >> /dev/null 2>&1
I save with ctrl+o and exit with ctrl+x.
Re-editing the file shows that the changes have saved.
Running crontab -l shows the text that I entered into the crontab file.
My cron never runs. I can only get it to run once by running manually using the command I mentioned above.
Not directly answering your question, but proposing another solution:
If you want to set up cron jobs for your development environment, it's best to use Homestead, for its Linux standards compliance.
For small projects that i develop directly inside macOS, i run the following command inside the project root (in a separate terminal tab) to have my jobs run every minute:
while true; do php artisan schedule:run; sleep 60; done
This helps to make sure, the cron jobs are only run while i'm developing. When i'm done, i Ctrl+C that command and can be sure nothing unexpected happens while i'm not watching.
Plus it gives me the freedom to adjust the interval, by simple choosing another number of seconds for the sleep command. This can save time when developing.
Update Laravel 8.x
Laravel now offers the above as a single artisan command:
php artisan schedule:work

Categories