• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在OS X上通过crontab执行Node.JS脚本

用户头像
it1352
帮助1

问题说明

我一直试图弄清楚如何在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)?

正确答案

#1

您可以使用两个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.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfgabk
系列文章
更多 icon
同类精品
更多 icon
继续加载