Skip to content

How to set up a cron job that runs a Node.js app

New Course Coming Soon:

Get Really Good at Git

Find out how to set up a cron job that runs a Node.js app

Create a shell script in a file, for example called run.sh

#!/bin/sh
node app.js

Give it execution rights

chmod +x run.sh

Then

crontab -e

By default this opens with the default editor, which is usually vim.

| Tip if you’re new to vim: use i to enter insertion mode, so you can type/paste, then esc and wq to save and quit.

Now you can add one line for each cron job.

The syntax to define cron jobs is kind of scary. This is why I usually use a website to help me generate it without errors: https://crontab-generator.org/

You pick a time interval for the cron job, and you type the command to execute.

I chose to run this every every day at 10AM.

This is the crontab line I need to run:

0 10 * * * /Users/flaviocopes/dev/run.sh >/dev/null 2>&1

If all goes well, the cron job is set up.

Once this is done, you can see the list of active cron jobs by running:

crontab -l

You can remove a cron job running crontab -e again, removing the line and exiting the editor.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching May 21, 2024. Join the waiting list!
→ Get my Node.js Handbook
→ Read my Node.js Tutorial on The Valley of Code

Here is how can I help you: