Cron scheduled in routes/console.php not running (Laravel)
frankpulido
HOBBYOP

a month ago

Hello there, I am new to Railway… I have deployed a (not-dockerized) Laravel API using Railpack… I let Railpack deploy the Laravel service, then created and linked the MySQL service… All endpoints work, but the following command works only in localhost, not in my deployment :

routes/console.php

comment(Inspiring::quote());
})->purpose('Display an inspiring quote');

// The command is scheduled to run daily at midnight and clean useless past available slots.
// Check /app/Console/Commands/CleanPastAvailableSlots.php
Schedule::command('slots:clean-past')->dailyAt('00:01')->runInBackground();

Console/Commands/CleanPastAvailableSlots.php
(attached)

I would appreciate anyone's help, AI has not be trained in this issues yet…

$10 Bounty

5 Replies

domehane
FREE

a month ago

your cron isn't running because railway doesn't auto-run laravel's scheduler ,;do this in order :

  1. create railway/run-cron.sh:

#!/bin/bash
while [ true ]
do
    php artisan schedule:run --verbose --no-interaction &
    sleep 60
done
  1. make it executable: chmod +x railway/run-cron.sh

  2. create a new service in your railway project, connect same repo, go to settings > deploy > custom start command and add: chmod +x ./railway/run-cron.sh && sh ./railway/run-cron.sh

  3. copy all env variables from your main app to this new cron service

  4. deploy

this creates a separate service that runs your scheduler every minute. your main service handles web requests, cron service handles scheduled tasks; check the logs and you'll see it running.


domehane

your cron isn't running because railway doesn't auto-run laravel's scheduler ,;do this in order :create railway/run-cron.sh:#!/bin/bash while [ true ] do php artisan schedule:run --verbose --no-interaction & sleep 60 donemake it executable: chmod +x railway/run-cron.shcreate a new service in your railway project, connect same repo, go to settings > deploy > custom start command and add: chmod +x ./railway/run-cron.sh && sh ./railway/run-cron.shcopy all env variables from your main app to this new cron servicedeploythis creates a separate service that runs your scheduler every minute. your main service handles web requests, cron service handles scheduled tasks; check the logs and you'll see it running.

domehane
FREE

a month ago

the issue is that laravel's scheduler needs php artisan schedule:run to execute every minute, but railway doesn't do this automatically. the separate cron service with the while loop is railway's official fix for this exact problem


frankpulido
HOBBYOP

a month ago

thanks a lot for the feedback… the case is that is I link to the same repo I would have 2 Laravel services, full code… I think that I could create a third service (I already have Laravel and an MySQL that doesn't redeploys every time I update main branch in Github). I would like to link the sh command to my Laravel service (as I linked the MySQL) and just re-start 1 * * * as a custom build… (or make Laravel route defined in console.php work… I previously had an issue with a php extension that i could solve just adding a statement to the env variables in the Laravel service…

I found very strange not veing able to schedule a sh in a simpler way…


domehane
FREE

a month ago

yeah i get it seems weird but both services share the same mysql and codebase ; they just run different commands. your main service runs the web server, cron service only runs the scheduler (uses barely any resources)

railway doesn't have traditional cron like a vps. you can't run both web + scheduler in one service easily. the separate service IS the simple solution on railway

when you push to github, both redeploy automatically. the cron service literally just runs php artisan schedule:run every 60 seconds - no nginx, no heavy stuff

think of it like having one worker doing web requests and another checking your schedule. same codebase, different jobs. that's how railway's designed to work


no7r34l
PRO

a month ago

On Railway you won’t get a traditional cron running inside the same Laravel service automatically.

The pattern domehane suggested (second service that just runs php artisan schedule:run in a loop) is basically how you model workers on Railway:

Web service: runs PHP-FPM / web server and handles HTTP.

Cron service: same repo + env, but start command is your run-cron.sh script so it only runs the scheduler.

It feels like “two apps”, but in practice it’s one codebase with two different process types, which is the same idea you’d use on platforms like Heroku or Fly.


Loading...