genkio / blog

Stay hungry stay foolish
https://slashbit.github.io/blog/
0 stars 1 forks source link

Schedule node script in Linux #117

Open genkio opened 7 years ago

genkio commented 7 years ago

cron job is the way to do it, and here's the way to create a new scheduled job:

$ crontab -e

Say you'd like to have these two scripts to be ran every day at 8, then, just simply add these two lines at the end, then :wq, you're done.

0 8 * * * /usr/bin/node /home/neo/ur/index.js /home/neo/ur/urdb.json
0 8 * * * /home/neo/.nvm/versions/node/v6.8.1/bin/node /home/neo/inu-ok/index.js

You can find tons of articles online talk about cron job in detail, I just want to point couple of things you may need to be aware of.

  1. the full path of where node.js was installed needs to be given, notice I have two different paths here, one is the system default v4.6, and the other is the latest v6.8.1 installed via nvm.
  2. the first script tries to write data to some json file, the full path of that file needs to be supplied as well.

Lastly, run this command to verify the job status. (the last grep was targeting my user name neo)

$ sudo sudo grep --color -i cron /var/log/syslog | grep neo
Oct 20 08:00:01 sandbox CRON[27642]: (neo) CMD (/usr/bin/node /home/neo/ur/index.js /home/neo/ur/urdb.json)