How to schedule NodeJS scripts in Linux



Running NodeJS scripts as scheduled scripts can be a bit tricky. If you install
NodeJS normally through an apt-get, your Bash don't know 'node' command. So you cant just add 'node script.js' into crontab.

First create an .sh file. You can give it any name, but in this example I will use a name myScript.sh.
Sh files are linux shell executable files, they are equivalent to Windows bat files. So Sh files can be run through a linux console. In this example we are creating a SH -file, put a node command into sh file and call sh file from crontab. This will give us more control over our script running.

In sh file we are first telling Linux to use the first sh file executor we have available.

#!/usr/bin/env sh

Then we will change our current directory into scripts directory (this is not mandatory, you
can also use full paths).

cd /home/myUser/nodeScripts/

Now our script is running in nodeScripts folder. Next we call our nodejs script by calling
node with full path and nodejs scriptfile with relative path.

/root/local/bin/node myNodeScript.js

My node is installed under root user, but your install directory can variate. Find "Node"
file for correct path.

If node scripts writes something into console, we can stream those messages into file
by adding > /home/myUser/nodeScripts/logs/logFile.log after node command. So full
line with message forwarding would look like

/root/local/bin/node myNodeScript.js > /home/myUser/nodeScripts/logs/logFile.log

Next we have to setup crontab, which is Linuxes scheduler. Run command crontab -e
and add following line



0 1 * * * /full_path_to_sh_file/myScript.sh

This script will be run every day at 01:00. For more info about crontab syntax see page
http://www.adminschoice.com/crontab-quick-reference/

That's it. Now your node script is run every day at 01:00.

If you want to test crontab (+script), you can type "date" into linux console, pick-up a time and put it into
crontab syntax.

Summary:
1. Create a myScript.sh file and add following lines
#!/usr/bin/env sh
cd /home/myUser/nodeScripts/
/root/local/bin/node myNodeScript.js
2. Run crontab -e command
3. Add following line into crontab
0 1 * * * /full_path_to_sh_file/myScript.sh

The best way to predict the future is to implement it.

5 kommenttia:

  1. Thank for your sharing, it's useful for me.

    VastaaPoista
  2. nemco.com.au
    Node.js Web Development Services | Node.js Development Company
    Nemco is a top level Node.js Development Company based in sydney, Australia. provide supreme Node.js development service. Hire Node.js developers from Nemco
    node.js development services | node.js development Sydney

    VastaaPoista
  3. Excellent tips. Really useful stuff .Never had an idea about this, will look for more of such informative posts from your side... Good job...Keep it up
    node.js development
    node.js development company
    node.js development services
    node.js development Sydney

    VastaaPoista

  4. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Online Training from India. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    JavaScript Online Training from India

    VastaaPoista