Cron 如何让grunt每天自动运行?
我想每天自动运行量角器端到端测试 目前我能做什么Cron 如何让grunt每天自动运行?,cron,gruntjs,protractor,Cron,Gruntjs,Protractor,我想每天自动运行量角器端到端测试 目前我能做什么 $ grunt 在测试目录中,它们将运行 有没有办法让它们每天在特定时间自动运行 我尝试过使用cron,但根据,我必须将所有需要的路径都转换为绝对路径,这是我不想做的。使用节点脚本和setInterval: var exec = require('child_process').exec var path = require('path') var running = false var run = function(what, where)
$ grunt
在测试目录中,它们将运行
有没有办法让它们每天在特定时间自动运行
我尝试过使用cron,但根据,我必须将所有需要的路径都转换为绝对路径,这是我不想做的。使用节点脚本和
setInterval
:
var exec = require('child_process').exec
var path = require('path')
var running = false
var run = function(what, where) {
if (running === true) return
running = true
// by default, just run grunt
what = what || 'grunt'
// by default, run on grunt in the current working directory
where = path.resolve(where || path.join(process.cwd(), 'Gruntfile.coffee'))
what += ' --gruntfile=' + where
exec(what, { cwd: path.dirname(where) }, function(err, stdout, stderr) {
if (err || stderr) { /* log the error somewhere */ }
/* log the stdout if needed*/
console.log(stdout)
running = false
})
}
setInterval(function() {
run(/* set what to run, where to run */)
/* or even multiple gruntfiles and node projects */
}, 24 * 60 * 60 * 1000) // once a day
这与平台无关,易于启动和停止,易于维护和自定义
查看一个库,比如永远运行节点脚本。或者许多其他方式:nohup、monit、upstart等。使用Kyle Robinson Young的答案和节点模块,我想出了以下方法,每天下午5:00运行Grunfile:
var cronJob = require('cron').CronJob
, exec = require('child_process').exec
, path = require('path')
, running = false
;
var run = function(what, where) {
if (running === true) {
return;
}
running = true;
// by default, just run grunt
what = what || 'grunt';
// by default, run on grunt in the current working directory
where = path.resolve(where || path.join(process.cwd(), 'Gruntfile.js'));
what += ' --gruntfile=' + where;
exec(what, { cwd: path.dirname(where) }, function(err, stdout, stderr) {
if (err || stderr) {
console.log(err);
}
/* log the stdout if needed*/
console.log(stdout);
running = false;
});
};
new cronJob('00 00 17 * * *', function(){
console.log('Running Gruntfile ' + new Date());
var what = 'grunt'
, where
;
run(what, where);
}, null, true);
你应该指定你的操作系统。使用CI服务器怎么样?CI服务器听起来有点过头了,我只是希望能够定期运行测试。也许你可以尝试一下,我不认为CI是过头了,如果你碰巧在Github上,使用TravisCI很容易。如果您在本地运行,cron是可以的,但是看起来有点黑客。