0

I'm trying to setup a Cron job on my system by adding the following line

17 12 * * * Rscript ~/path/to/file/script.R > ~/output_`date +\%d\%m\%y`.txt 2>&1

yet, I cannot see the file the output is being written to. I've consulted the following answers, but to no avail:

Why did my crontab not trigger

CronJob not running

When I run the following command on the terminal:

Rscript ~/path/to/file/script.R > ~/output_`date +\%d\%m\%y`.txt 2>&1

I get the output file as expected. I also added the following line to crontab:

* * * * * echo hi > ~/output.txt 2>&1

and it works just fine. I'm not sure what is wrong with the first command. Any help would be appreciated. Thanks.

Community
  • 1
  • 1

2 Answers2

1

Try Below trick, Create one script script.sh like below -

cat script.sh
Rscript ~/path/to/file/script.R > ~/output_$(date +\%d\%m\%y).txt 2>&1

And then create below entry in crontab.

17 12 * * * /bin/bash /path/to/script.sh
VIPIN KUMAR
  • 3,019
  • 1
  • 23
  • 34
  • why is this better? You are just replacing backticks with `$()`. – fedorqui Apr 12 '17 at 11:51
  • @fedorqui - Replacing backticks is just code improvement and the trick is to move the code into a separate script and execute it by cron(which is the Ideal syntax for cron job) – VIPIN KUMAR Apr 12 '17 at 12:08
  • Then it is important to highlight this aspect of your answer. Also, I don't think it is going to work as is, you need to either put the shebang or indicate the binary in the cronjob – fedorqui Apr 12 '17 at 12:17
  • @VIPINKUMAR tried running the script according to your trick, but couldn't make it work. Executing `script.sh` on the terminal works fine, `35 19 * * * bash script.sh` does not ( I tried running it at 7.35 PM local time) – WitchKingofAngmar Apr 12 '17 at 14:08
  • please mention the absolute path in cron job, like this - `35 19 * * * bash /home/user/script.sh` – VIPIN KUMAR Apr 12 '17 at 14:11
  • @fedorqui - Your Suggestion has been considered, I will add them to Answer if trick solves this Question. Thank you! – VIPIN KUMAR Apr 12 '17 at 14:15
  • @VIPINKUMAR still no luck. Same issue. – WitchKingofAngmar Apr 12 '17 at 14:17
  • @WitchKingofAngmar - Please make one more change like this `35 19 * * * /home/user/script.sh` and Is there any error message, please paste here. – VIPIN KUMAR Apr 12 '17 at 14:21
  • @VIPINKUMAR sorry for the delay. Still no progress. I did not get an error message. – WitchKingofAngmar Apr 12 '17 at 14:59
  • in fact it should be `35 19 * * * /bin/bash /path/to/script.sh` – fedorqui Apr 13 '17 at 09:11
1

I fixed this by replacing Rscript in my crontab with /usr/local/bin/Rscript (or wherever your Rscript is located - do which Rscript to find out).

Serenthia
  • 1,222
  • 4
  • 22
  • 40