Rscript in Crontab not running in Ubuntu Server 18.04 - r

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!"

Related

Symfony 2.4 cron job not running

* * * * * /usr/bin/php5.6 -f /var/www/utt/app/console reservation:createPropertyStatements 15 --env=prod &>> /var/www/utt/createPropertyStatements.log
Symfony 2.4 cron job not running. The command runs when we run it manually eg:
/usr/bin/php5.6 -f /var/www/utt/app/console reservation:createPropertyStatements 15 --env=prod &>> /var/www/utt/createPropertyStatements.log
That works
I have also created a sample bash script that runs from the cron tab - */5 * * * * root /bin/bash /var/www/utt/timer.sh
and that runs as well.
we are logged into server via ssh as root and are accessing the crontab via - nano /etc/crontab

Scheduling R script to run with crontab on Mac

time crontab user.
I am on a MacOS Mojave Version 10.14.3.
I saved a cronjob with this command:
25 21 * * * Rscript /Users/jasonbaik/Documents/pbp-animation/update-data.R
However, nothing happens, and I see a mail:
Apparently, the command Rscript was not found...
However, when I just run Rscript /Users/jasonbaik/Documents/pbp-animation/update-data.R in my terminal, everything seems to be working...

Moving files through a crontab task doesn't work, but works if is executed manually

I'm trying to move files crontab to get that scheduled but the crontab is not moving the files. If I do it manually, it works... do you know what could be the possible reason? This is what I have:
13,29 * * * * mv $(grep -l "File was not FOUND" /home/user/test*) /home/user/temp
If I execute the following line it works without any problem:
mv $(grep -l "File was not FOUND" /home/user/test*) /home/user/temp
By default, cron jobs are run using /bin/sh. You should be able to set the shell to use by adding it just before your job like this:
SHELL=/bin/bash
13,29 * * * * mv $(grep -l "File was not FOUND" /home/user/test*) /home/user/temp
...or whichever shell you like.
Alternatively, if your crond does not support that notation, you may explicitly invoke the shell you like, using its -c argument:
13,29 * * * * bash -c 'mv $(grep -l "File was not FOUND" /home/user/test*) /home/user/temp'
Notice the enclosing single quotes. They are required as the whole command must be a single argument to the shell.
Yet another way would be to convert your command to use plain old bourne shell (sh) syntax, which I believe should be:
13,29 * * * * mv `grep -l "File was not FOUND" /home/user/test*` /home/user/temp
...using backticks for command substitution.
Create a simple Schell script. add #!/bin/bash at the top.
#!/bin/bash
mv /your/source/file/path /target/path
# In your case something like below should work.
# make sure your path is absalute path, starting from root folder.
# mv $(grep -l "File was not FOUND" /home/user/test*) /home/user/temp
and then execute above shell script in crontab like below.
13,29 * * * * /path/to/file/movescript.sh
make sure that your script have execute permission, and the user can move files. better run the script manually before scheduling
chmod +x movescript.sh will add execute permission.

Crontab not working but its working as script

Below is my Crontab:
*/5 * * * * /usr/bin/wget "http://localhost:8080/sample/index.jsp" --post-data "data=$(nohup sqoop import --connect 'jdbc:sqlserver://localhost;username=username;password=password;database=database' --table table1 --target-dir /user/data/ -m 1)&dt=$(date)&user=$USER"
i am scheduling but its not running
You have to use full paths in crontab, since it does not have same value of $PATH as your shell (ie use full path for nohup, sqoop )
This could be because your not in the same execution context. Depending on which shell you are running (adapt my old-school .kshrc), you might need to prefix the command with a source /home/myuser/.kshrc, like below:
*/5 * * * * source /home/myuser/.kshrc ; /usr/bin/wget "http://localhost:8080/sample/index.jsp" --post-data "data=$(nohup sqoop import --connect 'jdbc:sqlserver://localhost;username=username;password=password;database=database' --table table1 --target-dir /user/data/ -m 1)&dt=$(date)&user=$USER"
If I may, this could also look simpler to everybody if your command could be embedded in a small script. This could avoid this possibly misleading nohup you have... then if script is mycommand.sh:
*/5 * * * * source /home/myuser/.kshrc ; /home/myuser/mycommand.sh
(being given you did a chmod u+x /home/myuser/mycommand.sh to grant execution rights).

How to run an R script in crontab

Using crontab -e I've tried:
* * * * * Rscript /home/.../file.r
* * * * * /usr/lib/R/bin/Rscript /home/.../file.r
* * * * * /usr/bin/Rscript /home/.../file.r
* * * * * /home/.../foo.sh
where foo.sh contains:
sudo R CMD BATCH file.r
Just running $ ./foo.sh works.
$ R CMD BATCH file.r works.
Nothing I've tried in crontab works. Any ideas?
You'll need to have the full path in your foo.sh:
sudo R CMD BATCH /home/.../file.r
I should also add that the first version worked for me, although I set it to a specific time rather than * * * * *
For what it is worth here is CRANberries entry which has worked (multiple times) every day for 5+ years:
# every few hours, run cranberries
11 */2 * * * edd ~/cranberries/cranberries.r
Of note here are
The time specification, here 11 mins past the hour every two hours
The user
The complete path, using 'globbing' to expand ~ to $HOME for edd
and after that you just have to make sure the script is actually executable. Whether you use Rscript, littler or R BATCH CMD does not matter.
in my user level crontab I run a R script to collect tweets from various sources.
I used:
crontab -u myusername -e
added this to the end of my crontab file
0 0 * * * /home/bob/Documents/SWtweets/tweetbash.sh
I ran it as a user vs root, don't what difference it makes, but it only worked for me when I ran it at user level.
below is what I put in my tweetbash.sh shell script,
which tells the bash program to run the R script located at the path indicated.
#! /bin/bash
R CMD BATCH /home/bob/Documents/SWtweets/tweets.R
did chmod +x tweetbash.sh
it works for my purposes.
Bob

Resources