javascript - Create cron executing node.js file -
i'm trying execute javascript file every 2 hours. i'm using node.js create cron
1 * * * * /usr/bin/node /var/www/html/uniphidata/js/recalculate_points.js
my recalculate_ponts.js file this:
var mysql = require('/var/www/html/uniphidata/mysql');
var connection = mysql.createconnection({ host: 'host', user: 'user', password: 'pass!', database: 'database' }); connection.connect(); connection.query('select * users', function (err, rows, fields) { if (err) throw err; rows.foreach(function (row, index) { connection.query('select count(*) friends users invitedbyfriend = ' + row.id, function (err2, friends, fields2) { if (err2) throw err2; var realcountfriends = friends[0].friends; friends = friends[0].friends * 50; var id = '%;' + row.id + ';%'; connection.query('select sum(pointspershare) shareevent events shared "' + id + '"', function (err3, pointspersharing, fields3) { if (err3) throw err3; if (pointspersharing[0].shareevent == null) pointspersharing = 0; else pointspersharing = pointspersharing[0].shareevent; connection.query('select sum(e.pointsentry) entry orders o, events e o.used = 1 , o.userid = ' + row.id + ' , o.eventid = e.id', function (err4, pointsentry, fields4) { if (err4) throw err4; if (pointsentry[0].entry == null) pointsentry = 0; else pointsentry = pointsentry[0].entry; connection.query('select sum(e.pointsprepay) purchase orders o, events e o.guestlist = 0 , o.cheaplist = 0 , o.userid = ' + row.id + ' , o.eventid = e.id', function (err5, pointsprepay, fields5) { if (err5) throw err5; if (pointsprepay[0].purchase == null) pointsprepay = 0; else pointsprepay = pointsprepay[0].purchase; var shareapp = row.quantitysharedapp * 20; var newuser = 50; var realcount = friends + pointspersharing + pointsentry + pointsprepay + shareapp + newuser; var difference = realcount - row.totalpoints; if (difference != 0) { connection.query('update users set points = points + ' + difference + ', totalpoints = ' + realcount + ', monthpoints = monthpoints + ' + difference + ', friendsinvited = ' + realcountfriends + ' id = ' + row.id, function (err6, changepoints, fields6) { if (index == rows.length - 1) { connection.end(); } }) } else { if (index == rows.length - 1) { connection.end(); } } }) }) }) }) }) });
the log in cron this:
nov 17 14:55:01 name cron[4677]: (root) cmd (/usr/local/rtm/bin/rtm 14 > /dev/null 2> /dev/null)
of course not executing javascript file , know script works because execute without cron. don't know i'm doing wrong search in internet , try put absolute route , relative route, nothing happens.
thanks million
you can create bash script (.sh) executes .js file. call .sh file cron.
1 * * * * /path/to/file.sh
the .sh file can this:
#!/bin/bash cd "$(dirname "$0")" node ./app.js
app.js javascript file in same folder .sh file
Comments
Post a Comment