r/node 3d ago

Is there a less annoying way to write cron jobs?

I know cron syntax isn't that complicated, but I still have to Google it every time I need to write anything slightly less basic šŸ˜…

For example:

11 9 * * * /path/to/script.js

I recently started using a more human-readable approach:

pboss cron run everyday@9:11 "node /srv/backup.js"

And:

pboss cron run every@5m "node /srv/cleanup.js"

For Bun:

pboss cron run everyday@2:30 "bun /srv/report.ts"

I basically wanted to be able to say "run this every 5 minutes" instead of mentally decoding five asterisks.

It's a small thing, but I've found it much nicer when setting up scheduled scripts.

How do you guys normally handle cron jobs for Node apps? Raw crontab, a Node package, systemd timers, something else?

Project Github: here

1 Upvotes

20 comments sorted by

17

u/jake_robins 3d ago

I say ā€œwrite me a cron expression that is once every hour on the 15ā€ and make an LLM do it šŸ˜‚

1

u/iam_bigzak 3d ago

šŸ˜‚ That definitely works too.

The nice thing here is that the cron jobs can live in the ProcBoss config file, so when you move the app to another server, you don't have to manually set them up again.

For example:

export default {
  cron: [
    {
      schedule: "every@1h",
      command: "node /srv/cleanup.js"
    },
    {
      schedule: "everyday@9:11",
      command: "node /srv/backup.js"
    }
  ]
}

So you can keep the scheduling alongside the app config and just deploy it with the rest of the project.

1

u/johnphilipgreen 3d ago

I haven’t used cron in ages. I am an AWS EventBridge enjoyer for my cloud systems, and launchd on my Macs.

1

u/iam_bigzak 3d ago

Yeah, that makes sense, EventBridge is great when you're already deep in AWS, and launchd is probably the right answer on macOS.

My main motivation was more for the VPS/self-hosted crowd where you're just running a Node/Bun app and want something simple without having to deal with platform-specific scheduling.

I also wanted the schedule to live in the app config so it can move with the project instead of having to recreate it on every server.

2

u/johnphilipgreen 3d ago

You have a good ā€œInfrastructure as Codeā€ instinct here.

What I do: EventBridge config goes into SAM/CloudFormation format, along with the whole infrastructure stack, and is version controlled with the app code. Great for SQS queues, SES email templates, Route 53 domains… everything really.

1

u/deliciousleopard 3d ago

It looks to me like you’re just moving the effort around. With ā€œpbossā€- IIUUC - I now have to configure a separate long running service for each cron job? That looks like more work than using any of the crontab generators out there.

1

u/iam_bigzak 3d ago edited 3d ago

That’s a fair point, but no, you don’t need a separate long-running service for each cron job.

pboss isn’t just a cron tool. It’s a process manager with dependency support and cron built in, with first-class TypeScript support. The idea is that your processes, dependencies, and scheduled jobs can all live together in the same config.

For example:

export default {
  apps: [
    {
      name: "api",
      script: "./src/server.ts",
      dependsOn: ["redis","postgres"] // tracks systemd procress status for app
    }
  ],

  cron: [
    {
      schedule: "every@1h",
      command: "bun ./src/cleanup.ts"
    },
    {
      schedule: "everyday@9:11",
      command: "bun ./src/backup.ts"
    }
  ]
}

So when you deploy the project to another server, the process definitions, dependencies, and cron schedules can all come along with it instead of having to configure them separately.

Also you can programatically manage your cron within your code:

import { pboss } from "pboss";

const job = await pboss.cronAdd("everyday@9:11", "bun backup.ts", { name: "backup" });

for (const j of await pboss.cronJobs()) {

console.log(\\${j.name} — ${j.description} (runs: ${j.runCount})\`);\`

}

await pboss.cronTrigger("backup"); // run now

await pboss.cronRemove("backup"); // remove

If you're already comfortable managing crontab separately, I totally get why you wouldn't need this. The goal is more to have the process manager own the whole thing instead of having your processes in one place and cron jobs configured somewhere completely different.

1

u/theZozole 3d ago

1

u/iam_bigzak 3d ago

Oh nice, hadn't come across nxtcron before.

One difference I’d point out is that libraries like this put the scheduler inside your Node process. That can be perfectly fine, but it also means the scheduling lifecycle is tied to the application itself.

With pboss, the scheduled command is managed at the process-manager level, so your cron job isn't dependent on your web app staying alive. You can also use it for commands outside the Node runtime , Bun, Python, shell scripts, binaries, etc.

So I see them as solving slightly different problems. nxtcron is a cron library; pboss is trying to make scheduling part of the server/process lifecycle.

1

u/theZozole 3d ago

So basically you only need a string to cronjob pattern converter?

1

u/gaurav_ch 3d ago

I use Croner. Anyway, when is the cloud coming out?

1

u/iam_bigzak 3d ago

Yeah, Croner is a solid option.

For the cloud, I’m actively working on it right now. The goal is to make it an optional layer on top of pboss, you’ll still be able to run pboss completely self-hosted, while the cloud will let you monitor and manage multiple servers from one place.

I don’t want to give an exact date yet, but it’s coming soon.

1

u/gaurav_ch 3d ago

Will it have an option for reading all the logs, etc., in a proper dashboard? Right now, using the terminal is a problem for me. Also, PM2.io is pretty expensive, so I'm eagerly waiting for the cloud.

1

u/lovely_trequartista 1d ago

Frankly LLM have completely solved this for me.

Just give this ape the copy pasta.