Skip to main content

Node

How to run a node script on a schedule using cron

  • scheduling a Node.js script as a cron job won’t work by default on a Mac because cron’s default SHELL and PATH environment variables are different from the ones in your terminal and don’t include your zshrc settings and local commands like node, nvm or npm.
  • Here’s how to change that so your scripts will run the same way in cron as they do when you run them manually in your terminal:
  • match a way that uses a SHELL and PATH that don’t including node, npm, nvm/fnm (so Node version doesn’t have to be hardcoded)
    1. Update PATH in crontab to point to your local commands
    1. Save your script as an executable .sh file
    1. Set zsh as env in script (since cron uses SHELL=‘bin/sh’ by default)
    1. Source .zshrc in script
    1. Schedule job to run as often as you want
    1. Pipe all output to a file so you can inspect what happened (console.logs, errors, etc)
    1. Give cron full disk access (if your script reads/write from/to the file system) — System Preferences > …

Links:

Sending emails

  • Sending Email Using SendGrid
  • Resend • Build, test, and send transactional emails at scale 🛠️

Inbox