Cron job for a php artisan command
Asked Answered
E

3

6

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

Earlap answered 17/4, 2018 at 16:43 Comment(0)
H
11

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
Herries answered 17/4, 2018 at 16:46 Comment(4)
Hi, worth mentioning is that I am running my commands in Crontab. Shouldnt that be possible? Thanks for your reply.Earlap
Thank you for your help so far. Is artisan supposed to be a directory? In /current/ I have an Artisan file, but not a directory. I might want to try to specify the full path for Crontab since /current/ is actually /releases/100 for example.Earlap
artisan is a php file without an extension, it sits in the root directory of Laravel. Does the /current/ directory ever exist on the server?Herries
Edit: Full path seemed to have worked and without cd I believe. Thanks for your help ExohJosh!Earlap
O
4

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
Opossum answered 5/5, 2020 at 20:28 Comment(1)
Welcome to SO, please refer to this for better presenting your answer: meta.https://mcmap.net/q/77258/-passing-a-class-object-to-a-function-probably-by-pointer-not-reference-cBeeler
V
0

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.

Venavenable answered 3/6 at 22:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.