简体   繁体   中英

Rscript in Crontab not running in Ubuntu Server 18.04

The following is my very simple Rscript called test.R that I want to run every minute

print("Hello, World!")

and I would like to automate this script in the hopes that I can do the same for future scripts. I use the crontab -e call and add the following:

* * * * * Rscript home/<username>/test.R

which fails to produce any results.

Per the examples of cron job to execute r script not working and Schedule a Rscript crontab everyminute , I have made the following variations in the crontab code

* * * * * Rscript "home/<username>/test.R"
* * * * * usr/bin/Rscript home/<username>/test.R
* * * * * usr/bin/Rscript "home/<username>/test.R"
* * * * * cd home/<username>/ && Rscript test.R
* * * * * cd home/<username>/ && usr/bin/Rscript test.R

all of which do nothing. I have also created a script called myScript.sh which contains Rscript /home/<username>/test.R , and tried assigning the task * * * * * home/<username>/myScript.sh all to no avail.

I have made sure to run chmod +x to make my files executable beforehand, and simply typing Rscript test.R produces my desired results.

What else can I do to make this work? Is there some issue with me running Ubuntu Server 18.04 vs running the desktop version?

Update

I have run all the above variations, with a / before home and usr . I have also switched to trying ~/test.R but I still get no results.

Is it possible that it is running correctly and the results are not being captured? Cron won't return results to active terminal.

The following worked for me on an Ubuntu Server 16.04

First, confirmed that the test script return was expected when run from Rscript terminal:

root@mytester:~/myrscripts# Rscript test.R

returns

  [1] "Hello, World!"

Second, setup cron job via:

crontab -e

entered this line

* * * * * Rscript ~/myrscripts/test.R >> ~/mycronlog.log

notice I am piping results to a log (appending)

Third, checked log after a few minutes:

root@mytester:~# cat mycronlog.log 
[1] "Hello, World!"
[1] "Hello, World!"
[1] "Hello, World!"
[1] "Hello, World!"
[1] "Hello, World!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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