使用R Markdown将bash脚本作为Cron作业运行

时间:2021-09-02 00:08:32

I want to try how to render html from R markdown file but from a bash script running as Cron jobs. I don't know why everything works fine, except running as Cron jobs. What I do:

我想尝试如何从R markdown文件渲染html,但是从作为Cron作业运行的bash脚本中渲染。我不知道为什么一切正常,除了作为Cron工作运行。我做的事:

My script is a demo script from Rstudio

我的脚本是Rstudio的演示脚本

---
title: "test"
author: "sms"
date: "24 maja 2015"
output: html_document
---

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
summary(cars)
```
etc.

This script works without any problem in Rstudio.

这个脚本在Rstudio中没有任何问题。

Next I try to run an Rmd script from terminal:

接下来我尝试从终端运行Rmd脚本:

Rscript -e "require( 'rmarkdown' ); render('/home/sms/Dokumenty/R/test.Rmd', 'html_document')"

There wasn't any problem. It works.

没有任何问题。有用。

So I create bash script

所以我创建了bash脚本

#!/bin/bash

Rscript -e "require('rmarkdown'); render('/home/sms/Dokumenty/R/test.Rmd', 'html_document')"

Rscript -e“require('rmarkdown'); render('/ home / sms / Dokumenty / R / test.Rmd','html_document')”

and changed chmod 755 test.sh

并更改了chmod 755 test.sh

Also works like a charme.

也像魅力一样。

But when put in Cron file (admin and user):

但是当放入Cron文件(管理员和用户)时:

28 18 * * * sh /home/sms/Dokumenty/R/test.sh

It doesn't works

它不起作用

Any idea what am I doing wrong?

知道我做错了什么吗?

3 个解决方案

#1


1  

I had the same problem. The issue is because I was running the Rscript as root, but Rscript should be run as souza, current user. To fix that, I edited the crontab for user souza "sudo crontab -u souza -e" and the Rscript is run as souza and not as root. Now it is working

我有同样的问题。问题是因为我以root身份运行Rscript,但Rscript应该作为souza运行,当前用户。为了解决这个问题,我为用户搜索“sudo crontab -u souza -e”编辑了crontab,并且Rscript作为souza而不是root用户运行。现在它正在运作

#2


1  

Simplify, simplify, simplify.

简化,简化和简化。

First, I'd make it an Rscript, maybe called renderTest.R

首先,我将其设为Rscript,也许称为renderTest.R

#!/usr/bin/Rscript
library(rmarkdown)
setwd("/home/sms/Dokuments/R")
render("test.Rmd")   # I usually use default arguments

Second, make the script executable (chmod 0755 renderTest.R).

其次,使脚本可执行(chmod 0755 renderTest.R)。

Third, test the script. If you can run it as you, a cron job running as you should too.

第三,测试脚本。如果你可以像你一样运行它,那么你应该运行一个cron作业。

Fourth, add the cronjob running as you to have the same rights.

第四,添加运行的cronjob以拥有相同的权限。

Edit I just fixed a typo I copied from you: Documents/ not Documenty/.

编辑我刚刚修复了我从你那里复制过的拼写错误:Documents / not Documenty /。

#3


1  

I have the same problems.

我有同样的问题。

But it would work if you try to use knitr::knit2html instead of rmarkdown::render

但是如果你尝试使用knitr :: knit2html而不是rmarkdown :: render会有效

on idea why

在想法为什么

#1


1  

I had the same problem. The issue is because I was running the Rscript as root, but Rscript should be run as souza, current user. To fix that, I edited the crontab for user souza "sudo crontab -u souza -e" and the Rscript is run as souza and not as root. Now it is working

我有同样的问题。问题是因为我以root身份运行Rscript,但Rscript应该作为souza运行,当前用户。为了解决这个问题,我为用户搜索“sudo crontab -u souza -e”编辑了crontab,并且Rscript作为souza而不是root用户运行。现在它正在运作

#2


1  

Simplify, simplify, simplify.

简化,简化和简化。

First, I'd make it an Rscript, maybe called renderTest.R

首先,我将其设为Rscript,也许称为renderTest.R

#!/usr/bin/Rscript
library(rmarkdown)
setwd("/home/sms/Dokuments/R")
render("test.Rmd")   # I usually use default arguments

Second, make the script executable (chmod 0755 renderTest.R).

其次,使脚本可执行(chmod 0755 renderTest.R)。

Third, test the script. If you can run it as you, a cron job running as you should too.

第三,测试脚本。如果你可以像你一样运行它,那么你应该运行一个cron作业。

Fourth, add the cronjob running as you to have the same rights.

第四,添加运行的cronjob以拥有相同的权限。

Edit I just fixed a typo I copied from you: Documents/ not Documenty/.

编辑我刚刚修复了我从你那里复制过的拼写错误:Documents / not Documenty /。

#3


1  

I have the same problems.

我有同样的问题。

But it would work if you try to use knitr::knit2html instead of rmarkdown::render

但是如果你尝试使用knitr :: knit2html而不是rmarkdown :: render会有效

on idea why

在想法为什么