How to run multiple cron jobs?

I have a laravel project I need to run multiple cron jobs every minute?

I have something like this:

  app = ""
  cron = "cron -f"
  worker = "php artisan lesson:publish sqs --sleep=3 --tries=3 --max-time=3600"
  worker = "php artisan update:link"
  worker = "php artisan video:length"

But I believe I’m doing something wrong, Any Help?

Hi @awnigh!

In the above syntax you’ve shared, there’s one missing piece for the commands to individually run. Each process" needs a unique name. Otherwise, you’ll get an error during fly deploy with: “key worker is already defined”.

This means instead of using worker for each cron job, you can have worker_1, worker_2, so on so fort per cron job you want to declare. Or instead, aptly naming each process based on the job they’ll be running:

  app = ""
  cron = "cron -f"
  publish_sqs = "php artisan lesson:publish sqs --sleep=3 --tries=3 --max-time=3600"
  update_link = "php artisan update:link"
  video_length = "php artisan video:length"

Of course, that’s only going to run the each command once! So instead of relying on separate process per job, you can conveniently use Laravel’s built in scheduling feature to schedule running your commands per minute.

I see that aside from your “worker” processes, you’ve already added your “cron” process with cron -f. So since that’s already set, all you have to do is go over to your routes/console.php file and declare schedulers that will run your commands, like so:

use Illuminate\Support\Facades\Schedule;

Schedule::command('lesson:publish sqs --sleep=3 --tries=3 --max-time=3600')->everyMinute();

You can remove the other processes aside from app and cron:

  app = ""
  cron = "cron -f"

And send your changes to your Fly app with fly deploy! Let me know if it works

Astunishing :fire:, I’ve already figured it out. But your point at multiple worker made my day.


1 Like

That’s great to hear!