cron为什么不执行我的node.js脚本? [英] Why won't cron execute my node.js script?

查看:118
本文介绍了cron为什么不执行我的node.js脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的服务器每分钟执行一次节点脚本。如果我手动执行文件( ./ main.js ),该程序将完美执行,因此,我确定这不是问题。但是,当我将其交给cron执行时,什么也没发生。

I want my server to execute a node script every minute. The program executes perfectly if I execute the file manually (./main.js), so I'm pretty sure it's not the problem. But when I hand it over to cron to execute, nothing happens.

这是cron文件中的行。

Here's the line from the cron file.

* / 1 * * * * /home/bryce/scripts/wudu/main.js

这是示例日志:

10月11日15:21:01服务器CROND [2564]:(root)CMD(/home/bryce/scripts/wudu/main.js)

可执行文件: home / bryce / scripts / wudu / main.js

#!/usr/bin/env node

var program = require('commander');
var v = require('./cli/validation');
var a = require('./cli/actions');

program
  .version('0.0.1')
  .option('-u, --url', 'Register url')
  .option('-s, --selector', 'Register selector')
  .option('-p, --pagination', 'Register pagination')
  .option('-i, --index', 'Pass an index, to destroy')
  .parse(process.argv);

var args = process.argv.slice(2),
        mode = v.mode(args[0]),
        options = v.hasArgs(mode, program);

a.init(mode, options);

有人知道我为什么要保持无线电静音吗?

Any idea why I'm getting radio silence? Somewhere else I should be looking to debug?

更新:

我相信问题与我的问题有关相对文件路径,并且main.js从其自己的目录外部执行。

I believe the problem has to do with my relative filepaths, and main.js being executed from outside its own directory.

所以现在,我将 exe.sh 放入了 wudu 目录。看起来像这样:

So now, I've placed exe.sh in the wudu directory. It looks like this:

#!/bin/bash

cd ${0%/*}
./main.js mail

exit

现在,我将cron设置为每分钟执行一次此文件。我尝试从其他文件夹执行此文件,并且按预期工作。但是,cron仍然没有接过。

Now, I've set cron to execute this file every minute. I tried executing this file from other folders, and it works as expected. But again, cron isn't picking it up.

推荐答案

在shell脚本中封装执行,很可能在cron中执行的脚本没有设置相同的环境

Wrapping the execution in a shell script, it's likely the execution of the script in cron doesn't have the same environment set as when you run from the command line.

尝试通过设置 NODE_PATH 在cron中开始执行shell脚本。 & PATH

(如果需要这些值,请在命令行上输入: echo $ NODE_PATH echo $ PATH

Try prefacing the execution of the shell script in cron with setting NODE_PATH & PATH
(if you need these values, on a command line type: echo $NODE_PATH and echo $PATH)

因此,您的cron条目如下:

So, your cron entry would look like:

*/1 * * * * NODE_PATH=/usr/local/lib/node_modules PATH=/opt/local/bin:ABC:XYZ /home/bryce/scripts/wudu/exe.sh

只需确保将实际值替换为 NODE_PATH & PATH 以及您从第一次执行的 echo 命令获得的内容。

Just make sure to substitute the actual values for NODE_PATH & PATH with what you get from the echo commands that you first did.

这篇关于cron为什么不执行我的node.js脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆