【发布时间】:2017-04-01 04:30:49
【问题描述】:
我正在尝试每两个小时执行一次 javascript 文件。我正在使用 node.js,所以我创建了这个 cron
1 * * * * /usr/bin/node /var/www/html/uniphiData/js/recalculate_points.js
我的 recalculate_ponts.js 文件是这样的:
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 * FROM users', function (err, rows, fields) {
if (err) throw err;
rows.forEach(function (row, index) {
connection.query('SELECT count(*) AS friends FROM users WHERE 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) AS shareEvent FROM events WHERE shared LIKE "' + 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) AS entry FROM orders o, events e WHERE o.used = 1 AND o.userID = ' + row.ID + ' AND 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) AS purchase FROM orders o, events e WHERE o.guestList = 0 AND o.cheapList = 0 AND o.userID = ' + row.ID + ' AND 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 + ' WHERE ID = ' + row.ID, function (err6, changePoints, fields6) {
if (index == rows.length - 1) {
connection.end();
}
})
} else {
if (index == rows.length - 1) {
connection.end();
}
}
})
})
})
})
})
});
cron 的登录是这样的:
Nov 17 14:55:01 name CRON[4677]: (root) CMD (/usr/local/rtm/bin/rtm 14 > /dev/null 2> /dev/null)
当然没有执行 javascript 文件,我知道脚本可以工作,因为我在没有 cron 的情况下执行它。 我不知道我做错了什么,但我在互联网上搜索并尝试放置绝对路线和相对路线,但没有任何反应。
谢谢一百万
【问题讨论】:
-
您真的需要每两个小时运行一次整个文件,还是只安排您的查询(通过和在代码中)就足够了?在后一种情况下,您可以查看
cron模块。 -
@schroffl 是的,我需要运行所有脚本
标签: javascript mysql node.js cron