Rexcelsius
Rexcelsius

Reputation: 63

Cron job for a php artisan command

When I usually run my commands I do it from /var/www/project/html/current/ and it looks like this php artisan import:myData. This works great.

But I can't get it to work while running it as a cron job, I have tried the following cron jobs.

*/3 * * * 1-5 cd /var/www/project/html/current/ php artisan import:myData >/dev/null 2>&1

*/3 * * * 1-5 /var/www/project/html/current/ php artisan import:myData >/dev/null 2>&1

*/3 * * * 1-5 /usr/local/bin/php /var/www/project/html/current/ php artisan import:myData >/dev/null 2>&1

Does anybody have a suggestion?

Thanks

Upvotes: 6

Views: 21793

Answers (3)

Maka
Maka

Reputation: 663

If you are running multiple PHP version, sometimes default one would not work. For example Laravel 10/11 are looking for 8.2 and 8.3 versions. So in order to run artisan command using desired version, use this:

* * * * * cd /home/myaccountname/mydomain.com && /opt/cpanel/ea-php82/root/usr/bin/php artisan schedule:run >/dev/null 2>&1

This works on cPanel accounts and your account setup could be different. This works for me with PHP 8.2 and Laravel 10.

Upvotes: 0

sobikashi
sobikashi

Reputation: 41

in the crontab add this job

*/3 * * * 1-5 cd /var/www/project/html/current && php artisan import:myData >/dev/null 2>&1

You forgot to put these characters "&&" between the path to your project folder and the artisan command

Example:

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

Upvotes: 4

ExohJosh
ExohJosh

Reputation: 1892

See here:

https://laravel.com/docs/5.6/scheduling

You add * * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1 into your cron tab, which runs artisan schedule:run and then you add your console commands into the schedule Kernel with their schedule parameters, laravel handles the rest :)

From the docs:

<?php

namespace App\Console;

use DB;
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 = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            DB::table('recent_users')->delete();
        })->daily();
    }
}

Edit:

You are also specifying an odd path to artisan, try:

*/3 * * * 1-5 cd /var/www/project/html/current/artisan import:myData >/dev/null 2>&1

Instead of

*/3 * * * 1-5 cd /var/www/project/html/current/ php artisan import:myData >/dev/null 2>&1

Upvotes: 11

Related Questions