Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

edilsoncichon's avatar

I solved by adding a cron on crontab of the linux, through the commands: 1 - To edit the file of crontab: crontab -e 2 - I put in the file this command: * * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

tr0n's avatar

Same here. With the added problem that im using dokku.

If i run the command manually it works but when i run it through the schedule:run it doesn't.

This is my schedule method contents in the Console Kernel class:

// setup log file for affiliates
$cronLog = storage_path('logs/cron.log');
if (!File::exists($cronLog)) {
    File::put($cronLog, '');
}

$schedule->command('submissions:make')->everyMinute()->withoutOverlapping()->appendOutputTo($cronLog);
$schedule->command('posts:fetch')->everyMinute()->withoutOverlapping()->appendOutputTo($cronLog);

So, if i do this: dokku --rm run app-name php artisan posts:fetch it works but if i do: dokku --rm run app-name php artisan schedule:run it doesn't.

I mean it actually outputs this:

Running scheduled command: (touch /app/storage/framework/schedule-82355345381c6f450cb8fed77c343879; '/app/.heroku/php/bin/php' 'artisan' submissions:make; rm /app/storage/framework/schedule-82355345381c6f450cb8fed77c343879) >> '/app/storage/logs/cron.log' 2>&1 &
Running scheduled command: (touch /app/storage/framework/schedule-47be294322d57655f246304019e30640; '/app/.heroku/php/bin/php' 'artisan' posts:fetch; rm /app/storage/framework/schedule-47be294322d57655f246304019e30640) >> '/app/storage/logs/cron.log' 2>&1 &

But inside of one of this tasks i have a mail sending that never is sent. So the schedule:run itself works but doesn't execute the inside ones.

Now i can only make it work putting manually every command directly in the crontab. Like this:

* * * * * sudo -u dokku dokku --rm run app-name 'php artisan submissions:make' > /dev/null 2>&1
* * * * * sudo -u dokku dokku --rm run app-name 'php artisan posts:fetch' > /dev/null 2>&1

I tried everything mentioned in the thread with no results, please if anyone has an idea i would love to hear it.

Laravel 5.1 Dokku 0.3.15

dalamar's avatar

For those who still have an issue scheduling artisan commands: try to use --force param since you all probably have problems with production instances. See example from the official documentation: https://laravel.com/docs/5.4/scheduling#defining-schedules

$schedule->command('emails:send --force')->daily();
$schedule->command(EmailsCommand::class, ['--force'])->daily();
1 like
jbruni's avatar

Oh my... Here is how I unlocked my Laravel 5.4 scheduled command, so it started working again. Are you ready? Here it goes...

Using tinker, create a Schedule instance, like this:

>>> $schedule = app(Illuminate\Console\Scheduling\Schedule::class);

Then, create a Scheduling Event: copy your $schedule-> line from your Kernel.php file. Put all in a single line. Prepend $command = to it. Paste it all into tinker, like this:

>>> $command = $schedule->command('my:command')->everyMinute()->withoutOverlapping()->emailOutputTo('[email protected]')->description('My Scheduled Task');

Now that you have the $command event instance, you can get its "mutexName", as follows:

>>> $mutexName = $command->mutexName();

Output will be something like framework/schedule-7ffe5133226c0404ce844a81bddf986e40476b7f

This is the Cache key which locks the "withoutOverlapping" job.

If you try Cache::get($mutexName); you will get true - meaning that the job is locked.

To finally resolve the situation and unlock the job:

>>> Cache::forget($mutexName);

And you are done.

The withoutOverlap is managed by using Cache. In my case, there were no schedule related files at storage/framework, because we are using Redis.

3 likes
andreich1980's avatar

Thanks @jbruni @dalamar !

But I still have a problem.

$schedule->command('queue:work --force --tries=5')
            ->everyMinute()
            //->withoutOverlapping()
            ->sendOutputTo(storage_path('queue-work.log'))
        ;

        $schedule->command(GrabGroupsInfoCommand::class, ['--force'])
            ->everyMinute()
            //->withoutOverlapping()
            ->sendOutputTo(storage_path('grab-groups-info.log'))
        ;

When I run php artisan schedule:run in console (Windows 10 x64) the output is

Running scheduled command: "d:\dropbox\openserver\modules\php\PHP-5.6-x64\php.exe" "artisan" queue:work --force --tries=5 > "D:\Dropbox\OpenServer\domains\domain.dev\storage\queue-work.log" 2>&1

And it "freezes", no info about GrabGroupsInfoCommand is running. But new jobs that were created after I ran the command - they are processed.

What should I do to make EVERY schedule command run and don't just "freeze" on queue:work?

Maybe I can not run queue:work from the schedule? Where are you guys place queue:work command to run?

Updated P.S. Well, I think that schedule isn't an appropriate place for the queue:work command and I have to run it somewhere else: as a separate cron-task or make a new console command that will check if there's no process with the queue-worker and run it (since I don't have Supervisor installed on a shared hosting)

leonardharley's avatar

I was having a similar problem whereby scheduled commands were not running.

My quick fix was: php artisan cache:clear

The ->withoutOverlapping() seems to be cache managed - thanks @jbruni your post gave me the hint...

1 like
ijklim's avatar

Thanks to @jbruni for helping me solve the scheduled task problem on my end as well. Kept trying to look for mutex file, none found. All because the mutex system on my end is cache based instead of file based.

osteel's avatar

Hi @gregkaleka, I know it's a long shot since you posted the original question 3 years ago, but I've got the exact same issue as you (calling the command directly works, but running php artisan schedule:run does not, even though it outputs Running scheduled command: '/usr/bin/php7.2' 'artisan' ...).

Did you find the issue, and can you remember what it was by any chance?

(Disclaimer: the problems other participants to this thread were having are different (e.g. I am not using withoutOverlapping), but I tried the proposed solutions anyway to no avail)

Edit: actually @Electronick looks like you had that same issue too, any hint as to how to fix it would be greatly appreciated :)

pkundariya's avatar

Hello guys,

I have still problem is there any one who can help me.

Thanks Parth

PreviousWeatherReport's avatar

For me the problem was that the crontab does not have the same environment variables as the webserver or when you run CLI from bash.

So having correct variables available in the crontab environment did the trick (in my case I created the .env file with the variables during the build).

amitshahc's avatar

Some how I also found the same issue and tried everything listed here but no solution yet. I am using Laravel 5.8 and when i run the command from my windows cmd using

php artisan subscription:fallback it's running and i can verify the entries in the database which been created by that job.

But the same command is not executing when i call php artisan schedule:run it prints the messages in before() and after() hooks plus sending the email but with blank content. but no code being executed inside the handle() method.

myLaravelProject >php artisan schedule:run
Running scheduled command: "D:\Installation\php7114\php.exe" "artisan" inspire >> "D:\Installation\Apache24\htdocs\spaadvisor.com\storage\logs/schedule/2019-08-29.log" 2>&1
Running scheduled command: "D:\Installation\php7114\php.exe" "artisan" subscriptions:fallback 2 --force >> "D:\Installation\Apache24\htdocs\spaadvisor.com\storage\logs/schedule/2019-08-29.log" 2>&1
subscriptions:fallback command is started..
Finished.!

FreeSubscriptionFallback:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Modules\SuperAdmin\Entities\Repository\Contract\CronjobRepository;

class FreeSubscriptionFallback extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'subscriptions:fallback {days=2: number of max days from today to calculate expiry date range}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Auto renewal of shop-owner\'s subscriptions to Free for which are expiring within next {days=2} days';

    /**
     * Create a new command instance.
     *
     * @return void
     */

    protected $repo;

    public function __construct(CronjobRepository $repo)
    {
        parent::__construct();
        $this->repo = $repo;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info('Subscription fallback command logic : STARTED');
        // dd('hello');
        $days       = (int) $this->argument('days');
        $shopowners = $this->repo->getActiveSubscriptions($days);
        // dd($shopowners);
        try {
            $shopowners->each(function ($shopowner) {
                $this->line('Creating default subscription for shopowner id:', $shopowner->id);
                $this->repo->beginTransaction();
                $this->repo->createSubscription_default($shopowner);
                $this->repo->commit();
            });

            $this->info('Subscription fallback command logic : ENDED');

        } catch (\Throwable $th) {
            $this->repo->rollback();
            $this->error('Subscription fallback command logic : ERROR', $th);
            report($th);
        }
    }
}

Kernel.php:

<?php

namespace App\Console;

use Artisan;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Modules\SuperAdmin\Jobs\SubscriptionRenewal;
use Modules\SuperAdmin\Entities\Repository\Contract\CronjobRepository;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\FreeSubscriptionFallback::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();
        $today    = \DateTimeOperations::today()->toDateString();
        $logfile  = config('constants.scheduler.logfile_path') . $today . '.log'; //storage_path("logs/schedule/". $today .".log");
        $cronRepo = resolve(CronjobRepository::class);

        $schedule->job(new SubscriptionRenewal($cronRepo))->daily()
            ->before(function () {
                echo "SubscriptionRenewal job is started..\n";
            })
            ->after(function () {
                echo "Databse queue is started..\n";
                echo $exitCode = Artisan::call('queue:work database --stop-when-empty');
                echo "Finished.!\n";
                echo Artisan::output();
            })
            ->appendOutputTo($logfile)
            ->emailOutputTo(config('constants.scheduler.report_email'));

        $schedule->command("inspire")->everyMinute()
            ->appendOutputTo($logfile)
            ->emailOutputTo(config('constants.scheduler.report_email'));

        $schedule->command("subscriptions:fallback 2 --force")->everyMinute()
            ->before(function () {
                echo "subscriptions:fallback command is started..\n";
            })
            ->after(function () {
                echo "Finished.!\n";
                // echo Artisan::output();
            })
            ->appendOutputTo($logfile)
            ->emailOutputTo(config('constants.scheduler.report_email'));
        // echo Artisan::output();
    }

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

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

@amitshahc You should start a new question.... they are free.

Have you checked the log file? My guess is that you are not catching an exception.

rajiv's avatar

Also check for storage folder permission issues. If need be run chown and chmod.

adiditm's avatar

I solved by putting command instantiation and call the handle() method in Kernel.php calling

    $schedule->call(function () {
        $scheculeCommand = new Commands\yourCommand;
        $scheculeCommand->handle();
    })->everyFiveMinutes();
greenway's avatar

In my case my app was down for maintenance, therefore I needed to call "php artisan up".

my function:

protected function schedule(Schedule $schedule)
    {
        Log::info("running Kernel::schedule()");
        $schedule->call(function() {
            Log::info("calling function");
        });
    }

output was just "running Kernel::schedule()" and never the 2nd log.

calling artisan up fixed the problem.

connorcho's avatar

edit the cron config with full path:

          • cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 to
          • cd /path-to-your-project && /path-to-your-php/php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
Snapey's avatar

@connorcho You win today's archaeologist prize for commenting on a 7 year old thread,

Previous

Please or to participate in this conversation.