gpt4 book ai didi

javascript - 创建 cron 执行 node.js 文件

转载 作者:行者123 更新时间:2023-11-29 11:10:33 25 4
gpt4 key购买 nike

我试图每两个小时执行一个 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 的情况下执行它。我不知道我做错了什么,但我在互联网上搜索并尝试输入绝对路径和相对路径,但没有任何反应。

感谢一百万

最佳答案

您可以创建执行 .js 文件的 bash 脚本 (.sh)。您将使用 cron 调用 .sh 文件。

1 * * * * /path/to/file.sh

.sh 文件可能如下所示:

#!/bin/bash
cd "$(目录名 "$0")"
Node ./app.js

app.js 显然是您的 javascript 文件,与您的 .sh 文件位于同一文件夹中

关于javascript - 创建 cron 执行 node.js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40658165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com