简体   繁体   English

如何在Mac OSX中使用crontab运行RScript

[英]How to run RScript using crontab in Mac OSX

I have searched around on how to do this on here and none have really solved my problem. 我在这里搜索了如何执行此操作,但没有一个能真正解决我的问题。 Partially because i do not understand what is going on....unfortunately 部分原因是我不明白发生了什么。

I have an Rscript I am trying to schedule to run every day called: JASON.r 我正在尝试安排每天运行的JASON.r叫: JASON.r

the code inside the script is the following: 脚本中的代码如下:

setwd("/Volumes/3TB/")
install.packages("quantmod");library("quantmod")
getSymbols("AAPL")
write.csv(AAPL,"/Volumes/3TB/AAPL.csv")
quit(save='no')

I have found THIS PAGE on how to do so, but i have been not been successful. 我已找到此页面的操作方法,但没有成功。 What I do is open terminal and type: 我要做的是打开终端并输入:

Jason-Guevaras-iMac:~ rimeallthetime$ sudo crontab -e

for which it returns the following: 为此,它返回以下内容:

crontab: no crontab for root - using an empty one

~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
"/tmp/crontab.fwiSAwWI4R" 0L, 0C

So then I came across THIS POST which had similar issues and when I type: 所以,后来我看到了这个帖子有相似问题,当我键入:

Jason-Guevaras-iMac:~ rimeallthetime$ sudo su -
Jason-Guevaras-iMac:~ root# crontab -u rimeallthetime -e
crontab: no crontab for rimeallthetime - using an empty one

~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
"/tmp/crontab.L1nPwJdBRi" 0L, 0C

That is where I am at currently.... 这是我在哪里目前....

I have MAC OSX El Capitan version 10.11.4 我有MAC OSX El Capitan版本10.11.4

In Terminal, open the crontab with: 在终端中,使用以下命令打开crontab:

sudo crontab -e 

Press i to go into insert mode. i进入插入模式。

Add the following line: 添加以下行:

0 */23 * * * Rscript /path/to/file/JASON.r

Press Esc to leave insert mode. Esc键退出插入模式。

Type ZZ ZZ型

You should see the following message: crontab: installing new crontab . 您应该看到以下消息: crontab: installing new crontab

You can verify the crontab file by using crontab -l . 您可以使用crontab -l验证crontab文件。

Misc: 其他:

This will run the file every 23 hours. 这将每23小时运行一次文件。

Each star/position corresponds to: 每个星号/位置对应于:

MIN HOUR DOM MON DOW CMD

Feel free to change it to suite your needs. 随时更改它以适合您的需求。

It might be worth mentioning that cron is still supported by OSX but it has been deprecated in favor of launchd . 值得一提的是,OSX仍支持cron ,但已弃用 cron ,以使用 launchd

To create a launchd job, you need to create a "plist" file giving all needed information to run the script and place it in folder ~/Library/LaunchAgents . 要创建启动的作业,您需要创建一个“ plist”文件,其中提供了运行脚本并将其放置在文件夹~/Library/LaunchAgents所有必需信息。 Here's an example plist file: 这是一个示例plist文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>jason</string>
        <key>ProgramArguments</key>
        <array>
                <string>Rscript /path/to/JASON.R</string>
        </array>
        <key>StartCalendarInterval</key>
        <dict>
                <key>Minute</key>
                     <integer>0</integer>
                <key>Hour</key>
                     <integer>23</integer>
        </dict>
</dict>
</plist>

Then you need to load this plist file into the launchd scheduler and start it: 然后,您需要将此plist文件加载到启动的调度程序中并启动它:

 launchctl load ~/Library/LaunchAgents/jason.plist
 launchctl start jason

In the second line the name jason corresponds to the field Label in the plist file. 在第二行中,名称jason对应于plist文件中的字段Label

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM