Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

r - cronR add_cron doesn't execute script at all Mac OS High Sierra

I'm trying to create a cronjob using cronR. When I do it without addin my cronjob never starts

library(cronR)

script <- "~/Documents/Job/MammyClub/Ostatki/Rscripts/XML/AnitaXML.R"
cmd <- cron_rscript(script)
cron_add(command = cmd, at = "17:46", frequency = "minutely", id = "test_job1")
#> Adding cronjob:
#> ---------------
#> 
#> ## cronR job
#> ## id:   test_job1
#> ## tags: 
#> ## desc: 
#> 46 17 * * * /Library/Frameworks/R.framework/Resources/bin/Rscript '~/Documents/Job/MammyClub/Ostatki/Rscripts/XML/AnitaXML.R'  >> '~/Documents/Job/MammyClub/Ostatki/Rscripts/XML/AnitaXML.log' 2>&1

Cron is running on the machine

Macintosh:~ aleksandr$ sudo cron start
Password:
cron: cron already running, pid: 240
Macintosh:~ aleksandr$ 

When I trying to create cronjob using addin the error occurs

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union


Attaching package: ‘gmailr’

The following object is masked from ‘package:dplyr’:

    id

The following object is masked from ‘package:utils’:

    history

The following objects are masked from ‘package:base’:

    body, date, labels, message

Auto-refreshing stale OAuth token.
[1] "price-2021-01-12.xlsx"

Execution halted

The script itself runs well in RStudio and via Terminal too

options(java.parameters = "-Xmx1024m")
library(XML)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(gmailr)
#> 
#> Attaching package: 'gmailr'
#> The following object is masked from 'package:dplyr':
#> 
#>     id
#> The following object is masked from 'package:utils':
#> 
#>     history
#> The following objects are masked from 'package:base':
#> 
#>     body, date, labels, message
gm_auth_configure()
gm_auth()

# ищем конкретное письмо
thread <- gm_threads(search = "magbabyopt", #поиск по поставщику
                      label_ids = "Label_8632852764550356173", #папка остатки
                      num_results = 1) #самое крайнее письмо

thread_id <- gm_id(thread) #id письма с которым будем работать

my_message <- gm_message(thread_id) #данные этого письма

att_ids <- gm_attachments(my_message) #данные по всем вложениям письма

att_ids$filename
#> [1] "price-2021-01-12.xlsx"
#если вложений несколько нужно писать паттерн по выбору
my_attach <- gm_attachment(att_ids$id, thread_id)

#сохраняем вложение, если вложений несолько, то пишем паттерн как выше для пути (filename)
gm_save_attachment(my_attach, glue::glue("~/Downloads/{filename}", filename = att_ids$filename))

ostatki <- XLConnect::loadWorkbook(filename = glue::glue("~/Downloads/{filename}",
                                                         filename = att_ids$filename))%>%
  XLConnect::readWorksheet(., sheet = 1)%>%
  select(., article = `Артикул`, quantity = `ОстатокНаСкладе`,
         cost_price = `Цена3Значение`, price = `Цена1Значение`)%>%
  mutate(., article = trimws(article, "both"), quantity = as.numeric(quantity))%>%
  filter(quantity > 3)%>%
  mutate(., across(everything(), as.character))

# Делаем XML
#new xml
ostatki_xml <- newXMLDoc()

# items (table data) node
items_node <- newXMLNode("items", doc = ostatki_xml)

# rows (names/values) node
item_data <- apply(ostatki, 1, function(x){
  z <- newXMLNode("item") # создаем данные в каждый node по строкам
  addChildren(z, lapply(names(x), function(y) newXMLNode(y, x[y])))
})

xmlParent(item_data) <- items_node #записываем данные в каждый node

saveXML(ostatki_xml, file = "~/Downloads/Magbaby.xml")
#> [1] "/Users/aleksandr/Downloads/Magbaby.xml"
                          
RCurl::ftpUpload(to = "ftp://mammyclub.com/www/www.mammyclub.com/web/uploads/xmls/Magbaby.xml",
                 what = "~/Downloads/Magbaby.xml", userpwd = "secret_key))")
#> OK 
#>  0
#удаляем файл
unlink(c(glue::glue("~/Downloads/{filename}", filename = att_ids$filename),"~/Downloads/Magbaby.xml"))
#rm(list = ls())

What is the problem? .xls file with data the script works with has cyrillic encoding

I have to add language settings in crontab using crontab -e in Terminal, I think. But what settings?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...