How to run specific scripts in a cron job

joelanman

HOBBY

12 days ago

Hi, I want to run specific scrips in my codebase in a cron job. However it seems like the cron jobs in the UI only runs the start script? Is there a way to run specific scripts, something like

npm run send-emails

Question

6 Comments

joelanman

HOBBY

12 days ago

If I have to create a new service, there would be a lot of duplicate code with the main codebase. I can think of two workarounds:

  1. upload the same codebase but give it a different start command
  2. upload a light codebase that sends http requests to the main service (POST myservice.com/api/send-emails)

12 days ago

upload the same codebase but give it a different start command

Yep, this is what you wanna do, it would be another service that deploys from the same GitHub repo, you would just change the start command of that service and that's the command that will be run on the set schedule.


joelanman

HOBBY

12 days ago

thanks! I wonder if its worth adding that approach to the docs

https://docs.railway.app/reference/cron-jobs


12 days ago

Isn't it already? How would you improve this sentence?

Railway will look for a defined cron schedule on your service settings, and execute the start command for that service on the given schedule.


joelanman

HOBBY

12 days ago

Specifically the approach of uploading the same codebase, which was not intuitive to me - I'd previously deploy one codebase and run cron tasks on it. So maybe something like

If you have a main service like a website, a common approach is to deploy the same code on a second service and change the start command


12 days ago

Noted, thank you for the feedback!