Docs for crons
viclafouch
HOBBYOP

4 months ago

Hello,

I’ve been reading the documentation about cron jobs here: https://docs.railway.com/reference/cron-jobs, but I’m having trouble fully understanding how to implement it in practice.

For example, besides setting the cron schedule value, where should I actually put my code file? I’m using Node.js. The docs mention the “start command,” but to me that sounds like the command used to start the project itself, not necessarily to trigger a cron.

What I feel is missing is a complete example:

  • Where exactly to place the file that will be executed?

  • How should the command be defined for Node.js?

  • What would a minimal working example look like?

Just as a comparison, on Vercel the cron job setup feels a bit clearer: you define the path of the file to run along with the recurrence, which makes it straightforward.

I’d really appreciate it if the docs could include a simple code example or step-by-step usage scenario to make this easier to understand.

Thank you!

$10 Bounty

6 Replies

Railway
BOT

4 months ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


vedmaka
HOBBY

4 months ago

The cron jobs configuration allows you to run any service on schedule, so your whole service basically should be something that performs a one-off operation. When you have Cron Schedule set on the Settings the Railway (as to my understanding) will start the service accordingly to the schedule.

The expectation here is that your service starts, does it job and then exits (compared to regular services that intended to be started and keep running). When Railway starts your service on a schedule it will execute the Startup command as set on the settings of the service (or use default one, i.e. from config or a Docker image)

What exactly the service does, and what's the start command/start script - is up to you. The simplest example could look like:

Say, you have a NodeJS script that you'd like to execute on a schedule. You can go and create a custom Docker image in your repository that's based on the Node official image https://hub.docker.com/_/node but also bundles your script, i.e.

# Use official Node.js image (change version if needed)
FROM node:20-alpine

# Set working directory
WORKDIR /usr/src/app

# Copy package.json if you have dependencies
COPY package*.json ./

# Install dependencies (skip if script has none)
RUN npm install || true

# Copy your script into container
COPY script.js .

# Run script by default
CMD ["node", "script.js"]

And your complete repo structure may look like this:

my-task-repo/
├── Dockerfile
├── package.json      
├── package-lock.json
└── script.js

And the script.js is a dummy idle "task" like this:

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

async function task() {
  console.log('Starting task...');

  // Sleep for 10 seconds, on real script you can do some useful work here
  await sleep(10000);

  console.log('The task has been completed!');
}

task();

Adding this repo as a service to your project should build the app using default Nixpacks just fine. Configuring the cron schedule on this service of, say, 0 0 * * * will make it to start, run the script.js and exit after 10 seconds on every midnight every day

This is what's the idea behind this on Railway to my understanding


viclafouch
HOBBYOP

4 months ago

Ok, so it's not like Vercel. Where you can have a folder crons inside a project ?

I have to create a separate project for my crons, right ?


viclafouch

Ok, so it's not like Vercel. Where you can have a folder crons inside a project ? I have to create a separate project for my crons, right ?

noahd
EMPLOYEE

4 months ago

For one off scripts that do some stuff it looks like people have done separate projects yes.

At least thats what i see from my initial search


viclafouch

Ok, so it's not like Vercel. Where you can have a folder crons inside a project ? I have to create a separate project for my crons, right ?

vedmaka
HOBBY

4 months ago

Up to your needs, I imagine it should be completely ok to just have a separate one-off service or set of services within the main app project depending on your project architecture


irazvan2745
FREE

4 months ago

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

official railway docs for cronjobs


Loading...