Gihan
Gihan

Reputation: 33

How to schedule a node.js file that runs in the terminal?

I'm running a node js file by using my mac terminal. (By running the command node myscript.js on my terminal).

The script is like this and it is for an API call.

const api = require('fxme');

const fxmeRest = new api.fxmeRest({
    key: '123',
    secret: '123', 
    timeout: 15000, 
    recvWindow: 10000, 
    disableBeautification: false,
   
    handleDrift: true
   
});

var schedule = require('node-schedule');

var j = schedule.scheduleJob('*/30 * * * * ', function(){
  console.log('The answer to life, the universe, and everything!');
});

    fxmeRest.newOrder({
        symbol: 'AUDUSD',
        side: 'BUY',
        price: '1',
    })
    .then((data) => {
        console.log(data);
    })
    .catch((err) => {
        console.error(err);
    });

It runs fine when I just simply type node myscript.js on my terminal.

But then I wanted to run it at a specific time. For example, I want the functions to called exactly at 01.22 PM on a specific date.

To achieve this I used node-schedule. So I inserted it on to top of my script like this

var schedule = require('node-schedule');

and made the following change to my code.

    var date = new Date(2022, 03, 23, 01, 22, 0);

var j = schedule.scheduleJob(date, function(){

    fxmeRest.newOrder({
        symbol: 'AUDUSD',
        side: 'BUY',
        price: '1',
    })
    .then((data) => {
        console.log(data);
    })
    .catch((err) => {
        console.error(err);
    });

});

And ran it like before using my terminal. But the issue is script is not running at all. (Not even at the specified time. What am I doing wrong here? Any assistance you can provide would be greatly appreciated.

Upvotes: 3

Views: 953

Answers (1)

axiac
axiac

Reputation: 72177

You don't need node-schedule. I suppose the script needs to be started and run continuously to work with node-schedule.

On Un*x-like systems (macOS, all Linuxes etc.) you can use the cron daemon provided by the system.

Run man -s 5 crontab in your terminal to learn how to describe a job in the crontab file.

Run crontab -e to edit your personal crontab. It is a text file (initially empty) where you can describe what commands to run and when. The manual page has several examples that can help you.

To run your script every 30 minutes you need to add the following line to the file:

*/30 * * * *  node myscript.js

Because the cron daemon runs as root it is possible that it cannot find node in its path. You can either use the full path to node in the command above or set the PATH environment variable on top of the crontab file.

Use type -p node to find the path of the node executable.

You should also set the MAILTO environment variable in the crontab, before any schedule, to provide an email address. Everything that your script produces at stdout and stderr (including any error messages) is captured by the cron daemon and sent as email to the email address provided in the MAILTO variable. It does not send any email if the script does not produce any output.


If you are not interested in the output of your script and don't want to receive emails from the cron daemon (maybe because the script produces some files whose presence and/or content can be used to learn about its outcome) you can redirect stdout to /dev/null and duplicate stdout to stderr. Add 1>/dev/null 2>&1 to the command line. Or simply add &>/dev/null, it has the same effect. Type man bash in your terminal and read about redirections to learn more about these.

Upvotes: 3

Related Questions