【问题标题】:Create cron executing node.js file创建 cron 执行 node.js 文件
【发布时间】: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


【解决方案1】:

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

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

.sh 文件可能如下所示:

#!/bin/bash cd "$(dirname "$0")" node ./app.js

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

【讨论】:

  • 我放了同样的,但像 1 * * * * 一样的 cron 现在每分钟执行一次来测试,它不工作。
  • @Ary 日志说什么?
  • 我在问题中写道:Nov 17 14:55:01 name CRON[4677]: (root) CMD (/usr/local/rtm/bin/rtm 14 > /dev/null 2> /dev/null)
  • 你试过在没有节点的情况下运行不同的 crons 吗?我认为这更像是一个一般的 cron 问题,而不是与节点有关的问题
  • 如果一切都失败了,只需让您的应用永久运行并使用npmjs.com/package/node-cron计时。
猜你喜欢
  • 2021-03-16
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
  • 2012-08-14
  • 2014-07-27
  • 1970-01-01
  • 2019-08-28
  • 2011-09-19
相关资源
最近更新 更多