1

I'm trying to run a node script with crontab. I've tried first doing things like

* * * * * echo test > test.txt

to be sure crontab works (I'm trying to make the command work and then I'll change the crontab to something different so it doesn't run every minute).

The crontab above works. The thing is, when I try to use node, it doesn't run with the crontab. Running which node I get /usr/bin/node Here are the things I've tried.

Thanks!

* * * * * cd /path/to/script && node script.js

* * * * * cd /path/to/project && npm start (runs npx tsc && node build/script.js)

* * * * * cd /path/to/script && node script.js > test.txt (file is generated empty, even though, script has console.log)

* * * * * node /path/to/script/script.js

* * * * * echo test > test.txt && node /path/to/script/script.js (file gets generated)

Also I've tried all of the above replacing node by /usr/bin/node.

If I run any of these commands manually, it executes the program.

5
  • See logs Commented Jan 5, 2023 at 17:37
  • 1
    Try redirecting the errors to a file: node script.js > node.output 2>&1 Commented Jan 5, 2023 at 17:41
  • 1
    @Konrad, thanks. Seems like I have an issue with an authentication my script does. For some reason is failing when run with the crontab. I guess I can take it from here. If I can finally fix it thanks to this, I'll add it as an answer Commented Jan 5, 2023 at 17:46
  • Consider adding a shebang (i.e. #! /usr/bin/env node) to the script and set exec permission, so crontab would be simpler Commented Jan 5, 2023 at 17:54
  • btw if you use pm2 you can use cron timings in the restart strategies pm2.keymetrics.io/docs/usage/restart-strategies might be better suited then system cron Commented Jan 9, 2023 at 19:14

1 Answer 1

1

After going over a long time and testing a lot of stuff, I realized the issue was doing sudo crontab -e to set the crontab. I fixed it after running instead

sudo crontab -u username -e

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.