Restarting a node service on macOS boot

This was a pain to get right, so here’s what I landed on:

  1. Open System Settings > Energy Saver
  2. Turn on Start up automatically after a power failure
  3. Under Privacy & Security > Full Disk Access, add the executable /usr/sbin/cron
  4. Log in as root: sudo su
  5. Install pm2 globally: npm i -g pm2
  6. Don’t get trapped in vim: export EDITOR=nano
  7. Copy the PATH to clipboard: eval 'echo PATH="$PATH"' | pbcopy
  8. Edit crontab: crontab -e
  9. Paste in the PATH
  10. Create cron entries using the full path for file/directory references (you don’t need it for executables)

Example root cron:

PATH=/Users/steve/.bun/bin:/usr/local/bin:...

@reboot pm2 start /full/path/to/script.js

This creates a PM2 service for the root user. You can only see/remove its entries when logged in as root:

sudo su

# list services
pm2 list

# remove 1st entry
pm2 del 0