jrocc
jrocc

Reputation: 1346

Run node script in cron job

I am trying to run a node script with cron every minute. I dont believe it is running at all. When I run grep cron /var/log/syslog I dont see it running in the log.

Did I write the cron job wrong? If so how do I run a node script in a cron job?

 * * * * * node /home/ubuntu/Server/nodeScript.js

Upvotes: 0

Views: 2517

Answers (2)

Filip Petrovic
Filip Petrovic

Reputation: 125

The easiest way to run it every single minute is just to put your code in endless loop with timeout.

For example

function yourCode () {
    for {
        // your code
        timeout(60 * 1000)
    }
}
function timeout(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

Just be sure you run it with pm2 or supervisor so when it crashes it can restart automatically.

Upvotes: 0

smurp
smurp

Reputation: 91

Cron jobs don't run in a shell so you have to give the full path to the node binary.

/usr/bin/node (or whatever it is on that machine)

Try which node to find out

Upvotes: 4

Related Questions