Avatar of Faraz PatankarFaraz Patankar

Cron Jobs on Railway

Railway now supports Cron Jobs natively. It may be a better fit for you than what’s written here. Check out https://docs.railway.app/reference/cron-jobs for details!


Recently, we’ve had a few users ask us how they can go about deploying their cron jobs on Railway. While we don’t have first-party support for cron jobs, it is very easy to deploy them using language-level libraries.

This should be possible with all the languages we support. In this post, we will take JavaScript as an example and use the node-cron library to deploy a simple cron service on Railway.


At Railway, we treat a cron job just as we would anything else you deploy on our platform. Whether it is a web application, an API, or a cron job, for us, they’re all just services within a project.

So let’s go ahead and scaffold a brand new TypeScript package using DTS.

npx dts-cli create cron-service

Once the setup is complete, we can cd into cron-service and add the node-cron package.

yarn add node-cron
yarn add -D @types/node-cron

Next, we can replace the existing code in src/index.ts with a basic cron job.

import cron from 'node-cron';

cron.schedule(`*/1 * * * *`, async () => {
  console.log('running your task...');
});

The code above will simply log running your task... after every single minute as that’s what we have set the cron schedule as.

Before we run this, we will make one minor change to the scripts inside our package.json file which will help us when we deploy our service on Railway.

{
  ...
	"scripts": {
    ...
    "start": "node dist/index.js"
	}
}

In the snippet above, we update the start script to use the built code from our dist directory. This will ensure the correct command is run when we deploy our service on Railway.

To run our cron job, we simply build and run our code.

yarn build
yarn start
Demoing the cron service

Demoing the cron service

As you can see in the demo above, our message is logged to the console once every minute. In the next section, we will deploy this service to Railway.

We can deploy our new cron service to Railway by either adding it to an existing project or creating a new project. In this example, I will use the Railway CLI to create a new project and deploy the service to that project.

Creating a new project on Railway

Creating a new project on Railway

Once our project is created, we can simply run railway up and our code will automatically be deployed to our project.

A demo of our cron service running on Railway

A demo of our cron service running on Railway

As you can see in the screenshot above, our cron job is run every minute as specified in our code. Everything was automatically built and deployed with no additional configuration needed.

You can click here a live demo of the project we just deployed or click the button below to deploy your own in just one click!

We hope this post helps anyone looking to deploy cron jobs on Railway and would love for people to contribute similar tutorials in other languages and frameworks. Feel free to reach out to us on Discord if you’re interested.