在OS X上通过crontab执行Node.JS脚本 [英] Execute Node.JS script via crontab on OS X

查看:117
本文介绍了在OS X上通过crontab执行Node.JS脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图弄清楚如何在Mac OS X上使用crontabs.我编写了一个节点脚本,希望能够按计划执行.这是我在crontab文件中使用的行.

I have been trying to figure out how to use crontabs on my mac OS X I have a node script written that I would like to be able to execute in a scheduled manner. Here is the line I am using in my crontab file.

30 * * * * /usr/local/bin/node /Users/pmanca/Google Drive/JavaScript/code/Peter/marketing-tests/testBackup.js

它似乎没有执行.谁能看到我做错了吗?在Mac或Linux服务器上还有另一种方法可以完成我所寻找的吗?您是否还可以在Linux中对crontab作业使用同一行(除了需要更改的文件路径)?

It doesn't appear to be executing though. Can anyone see what I am doing wrong? Also is there another way on a mac or linux server to accomplish what I'm looking for? Also can you use the same line for a crontab job in linux as well(besides the file paths needing to change)?

推荐答案

您可以使用两个npm软件包之一,议程

You can use one of the two npm packages, cron or agenda

两者都可以在linux和OS X上工作.如果您的任务是轻量级的并且不需要将作业持久保存到db中,那么Cron最好.议程使用mongodb进行持久化.

Both work on linux and OS X. Cron is best if your tasks are lightweight and you don't need your jobs to be persisted into db. Agenda uses mongodb for persistence.

您可以在cron中将作业配置为:

You can configure a job in cron as:

var CronJob = require('cron').CronJob;
var job = new CronJob({
  cronTime:  '00 */30 * * * *',
  onTick: function() {
    /*
     * Runs every 30 minutes, every hour, every day, all week
     */
  },
  start: false,
  timeZone: 'America/Los_Angeles'
});
job.start();

要在服务器上后台运行,可以使用永远运行

To run in background on server you can run it with forever

记住cron在左侧还有第6个位置,保持秒数.否则语法是相同的.

Remember cron has an additional 6th place to the left for seconds. Otherwise the syntax is same.

这篇关于在OS X上通过crontab执行Node.JS脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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