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.
我希望我的服务器每分钟都执行一个节点脚本。如果我手动执行文件(./main.js),程序执行完美,所以我很确定这不是问题。但当我把它交给cron执行时,没有任何反应。
Here's the line from the cron file.
这是cron文件中的行。
*/1 * * * * /home/bryce/scripts/wudu/main.js
* / 1 * * * * /home/bryce/scripts/wudu/main.js
And here's a sample log:
这是一个示例日志:
Oct 11 15:21:01 server CROND[2564]: (root) CMD (/home/bryce/scripts/wudu/main.js)
10月11日15:21:01服务器CROND [2564] :( root)CMD(/home/bryce/scripts/wudu/main.js)
The executable: 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?
知道为什么我会收音机沉默吗?在其他地方我应该调试?
UPDATE:
I believe the problem has to do with my relative filepaths, and main.js being executed from outside its own directory.
我认为问题与我的相对文件路径有关,而main.js是从它自己的目录之外执行的。
So now, I've placed exe.sh
in the wudu
directory. It looks like this:
所以现在,我已经将exe.sh放在了wudu目录中。它看起来像这样:
#!/bin/bash
cd ${0%/*}
./main.js mail
exit
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.
现在,我已经设置了cron来每分钟执行一次这个文件。我尝试从其他文件夹执行此文件,它按预期工作。但同样,cron并没有把它拿起来。
2 个解决方案
#1
10
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.
将执行包装在shell脚本中,很可能在cron中执行脚本时没有与从命令行运行时相同的环境设置。
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中执行shell脚本的操作,并设置NODE_PATH&PATH(如果需要这些值,请在命令行类型:echo $ NODE_PATH和echo $ PATH)
So, your cron entry would look like:
所以,你的cron条目看起来像:
*/1 * * * * NODE_PATH=/usr/local/lib/node_modules PATH=/opt/local/bin:ABC:XYZ /home/bryce/scripts/wudu/exe.sh
Just make sure to substitute the actual values for NODE_PATH
& PATH
with what you get from the echo
commands that you first did.
只需确保将NODE_PATH和PATH的实际值替换为您从首次执行的echo命令中获得的值。
#2
1
I had the exact same problem, so I ended creating a passthrough script that would load my environment variables, then execute node like so:
我有完全相同的问题,所以我结束了创建一个加载我的环境变量的passthrough脚本,然后像这样执行节点:
##!/bin/bash
# Source bash profile to load env variables
# or any other that you want to set explicitly
. ~/.bash_profile
# tunnel that allows us to execute cron jobs
node $1 $2
To use just add it to your crontab
要使用它,只需将其添加到您的crontab
* * * * * <user> <passthrough_script> <arg1> <arg2>
#1
10
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.
将执行包装在shell脚本中,很可能在cron中执行脚本时没有与从命令行运行时相同的环境设置。
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中执行shell脚本的操作,并设置NODE_PATH&PATH(如果需要这些值,请在命令行类型:echo $ NODE_PATH和echo $ PATH)
So, your cron entry would look like:
所以,你的cron条目看起来像:
*/1 * * * * NODE_PATH=/usr/local/lib/node_modules PATH=/opt/local/bin:ABC:XYZ /home/bryce/scripts/wudu/exe.sh
Just make sure to substitute the actual values for NODE_PATH
& PATH
with what you get from the echo
commands that you first did.
只需确保将NODE_PATH和PATH的实际值替换为您从首次执行的echo命令中获得的值。
#2
1
I had the exact same problem, so I ended creating a passthrough script that would load my environment variables, then execute node like so:
我有完全相同的问题,所以我结束了创建一个加载我的环境变量的passthrough脚本,然后像这样执行节点:
##!/bin/bash
# Source bash profile to load env variables
# or any other that you want to set explicitly
. ~/.bash_profile
# tunnel that allows us to execute cron jobs
node $1 $2
To use just add it to your crontab
要使用它,只需将其添加到您的crontab
* * * * * <user> <passthrough_script> <arg1> <arg2>