0

I want to write a schedule in Laravel 9 which requests to an API and saves the results in the database.

Then I should add a cron job in my Cpanel to run it every minute.

My Laravel code:

<?php

namespace App\Console\Commands;

use App\Models\CurrenciesChart;
use Illuminate\Console\Command;

class CurrenciesInfo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'currency:info';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Getting currencies data';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // Some code
    }
}

Kernel.php

<?php

namespace App\Console;

use App\Console\Commands\CurrenciesInfo;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;

class Kernel extends ConsoleKernel
{
    protected $commands = [
        CurrenciesInfo::class,
    ];
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
         $schedule->command('currency:info')->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

When I run php artisan schedule:work locally, it works just fine but it doesn't seem to work on the host.

I've tried many queries such as below but nothing happened and there isn't any error in laravel.log.

I set the schedule to every minute (* * * * *) and tried the following queries.

/usr/local/bin/php /home/univardi/public_html/artisan schedule:run >> /dev/null 2>&1

cd /home/univardi/public_html && php artisan schedule:run >> /dev/null 2>&1

cd /home/univardi/public_html && /usr/local/bin/php artisan schedule:run >> /dev/null 2>&1

/usr/local/bin/ea-php81 /home/univardi/public_html/artisan schedule:run >> /dev/null 2>&1

But none of them worked.

  • As a side note you *might* gain some insight into command line Laravel [from here](https://stackoverflow.com/questions/43357472/how-to-manually-run-a-laravel-lumen-job-using-command-line). – Martin Feb 27 '23 at 23:46

1 Answers1

0

Change the query to touch /tmp/test.txt. After one minute checks the /tmp/test.txt file that exists or not. If the file exists you realize that the cron job works normally, Then change the original query to:

cd /home/univardi/public_html && php artisan schedule:run >> /tmp/job.log 2>&1

check /tmp/job.log file to find out the error of the query(it can be not found error), Then fix the problem and at the end replace /tmp/job.log with /dev/null

ehsan houshmand
  • 373
  • 3
  • 6