How can you execute a Node.js script via a cron job?

后端 未结 7 1530
深忆病人
深忆病人 2020-12-02 08:58

Quite simply, I have node script that I want to execute once a month.

30 6 1 * * node /home/steve/example/script.js

But this doesn\'t work,

相关标签:
7条回答
  • 2020-12-02 09:30

    These answers here saying using absolute path will all cause major problems for running a larger node app!

    Real Complete Solution

    Edit Cron Jobs

    crontab -e
    

    Find Node Path

    which node
    

    CD into the destination folder, then Change Cron Job according to Node Path and run script

    */2 * * * * cd /home/destination/path && /bin/node index.js
    

    This will then allow you to run a full NodeJS application without all the errors like how using an absolute path for your index.js file.

    0 讨论(0)
提交回复
热议问题