How to debug this scheduled .bat task in task scheduler using cmd and log file? - r

I'm trying to basically run the following .bat file as a scheduled task, while also logging errors in a .txt file:
In the 'program/script' box, I just have cmd. Then in the add arguments box I have:
/k ""T:\Some_folder\mybatchfile.bat" >>"T:\somelog.txt" 2>&1"
This had been working just fine originally before I tried to add the log function and calling cmd explicitly as seen in several posts, but I'd really like to add this function. I'm using /k for now so that I can watch the cmd window as things happen, but plan to replace it with /c so it closes when its done.
I tried many permutations of where my quotation marks are but am not having a lot of luck. I'm also intentionally using >> vs > in order to append the log, not overwrite it.
The contents of the .bat file are basically:
"C:\RDirectory\R.exe" CMD BATCH "T:\Some_folder1\Preworkforbatch.R"
copy T:\Some_folder2\some_data.csv "C:\Users\ABC1\Another_folder"
copy T:\Some_folder3\some_more_data*.csv "C:\Users\ABC1\Another_folder"
I'm wondering if part of it is that T is a network folder that is mapped? Thanks for your help.
edit:
Here is more info on the task:

"T:\Some_folder\mybatchfile.bat" >> "T:\somelog.txt" 2>&1
When redirecting the output of a batch file to a log file you will not see as much output in the cmd window. You have to repeatedly open, close, open the log file to see your progress. Use the title command in your batch to display progress info in the cmd window.
title This is %~F0. The time is %time%. The date is %date%. SLEEP 3600 FOR :RECORDRADIO

Ok this ended up working for me, by editing the batch file itself, and just running the batch file (not cmd explicitly) in the task scheduler:
mybatchfile.bat:
#echo on
"C:\RDirectory\R.exe" CMD BATCH "T:\Some_folder1\Preworkforbatch.R" >> "C:\Users\ABC1\Logfolder\mylog.txt" 2>&1
copy T:\Some_folder2\some_data.csv "C:\Users\ABC1\Another_folder" >> "C:\Users\ABC1\Logfolder\mylog.txt" 2>&1
copy T:\Some_folder3\some_more_data*.csv "C:\Users\ABC1\Another_folder" >> "C:\Users\ABC1\Logfolder\mylog.txt" 2>&1
Writing the log file to the network was causing errors. Writing to the local computer solved this issue. Not using double quotes also was key, instead just quotes around the file/path. This setup gives me an output for each line, so for every line 2>71 shows up, I get an output if there's an error or a completion message.
This is what the task scheduler looks like:

Related

Error that says Rscript is not recognized as an internal or external command, operable program or batch file [duplicate]

shell_exec("Rscript C:\R\R-3.2.2\bin\code.R ");
This is the call to script.On calling the above script, the error occurs.
I am trying to call my R script from the above path but no output is being shown. While checking the error logs of PHP, it says 'Rscript' is not recognized as an internal or external command, operable program or batch file.' The script is working fine on the Rstudio but not running on the command line.
Add the Rscript path to your environment variables in Windows:
Go to Control Panel\System and Security\System and click Advanced System Settings, then environment variables, click on path in the lower box, edit, add "C:\R\R-3.2.2\bin"
Restart everything. Should be good to go. Then you should be able to do
exec('Rscript PATH/TO/my_code.R')
instead of typing the full path to Rscript. Won't need the path to your my_code.R script if your php file is in the same directory.
You need to set the proper path where your RScript.exe program is located.
exec ("\"C:\\R\\R-3.2.2\\bin\\Rscript.exe\"
C:\\My_work\\R_scripts\\my_code.R my_args";
#my_args only needed if you script take `args`as input to run
other way is you declare header in your r script (my_code.r)
#!/usr/bin/Rscript
and call it from command line
./my_code.r
If you are running it in Git Bash terminal, you could follow a revised version of the idea suggested by #user5249203: in the first line of your file my_code.R, type the following
#!/c/R/R-3.2.2/bin/Rscript.exe
I assumed that your path to Rscript.exe is the one listed above C:\R\R-3.2.2\bin. For anyone having a different path to Rscript.exe in Windows, just modify the path-to-Rscript accordingly. After this modification of your R code, you could run it in the Git Bash terminal using path-to-the-code/mycode.R. I have tested it on my pc.
I faced the same problem while using r the first time in VS Code, just after installing the language package (CRAN).
I restart the application and everything worked perfectly. I think restarting would work for you as well.

nohup command getting permission issues

I am trying to run a filewatcher.sh script in background. It further invokes a java code through a jar file.
Whenever I run the command("nohup sh filewatcher.sh") through terminal, everything is ok. I have to hit enter two times so the output file is appended to nohup.out. But whenever I try to run the equivalent command("nohup filewatcher.sh >> nohup.out < /dev/null &") from a shell script automate.sh, I get following permission error.
error-log. Click to see snippet.
Could this an issue of permission for the automate.sh?
I have crossed check to see this script is having same permissions and ownership as the user when I log in and enter the nohup command from command line.
I also checked the ownership of the folders where the files are getting moved but everything is having same permissions and ownership.

R script to Task Scheduler

I have an R script that gets data from databases on another server and brings it into my database. I have it saved as "dataimport.R"
I followed a few answers from here and from other websites and created a batch file like this:
"C:\Program Files\R\R-3.4.0\bin\R.exe" CMD BATCH --vanilla --slave "C:\dataimport.R"
This is not working. The cmd window opens up but the tables are not recreated and I dont get any error. I wanted to run the Task Scheduler to automate the process. Any ideas on how to fix this?
I kept at it and interestingly the answer to this was this:
"C:\Program Files\R\R-3.4.0\bin\R.exe" "C:\dataimport.R"
I dont know the reason for this but as long as it works.
I've had loads of problems with this, but finally managed to get it to work. To be more comprehensive, here are some of the things I've tried (in case one of these work for other persons):
#echo off, R CMD BATCH C:\myfolder\script.R
R CMD BATCH C:\myfolder\script.R
Using the package taskschedulerR (somehow didn't work overnight)
Used the answer provided above ("C:\Program Files\R\R-3.4.0\bin\R.exe" "C:\dataimport.R")
and all kinds of variations and combinations off these. (can't remember them all exactly)
What finally worked was:
Make an R script and save it (C:\myfolder\Test.R for example)
Through notepad, fill in: "C:\Program Files\R\R-3.5.2\bin\x64\R.exe" CMD BATCH "C:\myfolder\Test.R" (also tried Rscript.exe, didn't work for me).
in Windows Task Scheduler (v1.0) do 'Create task'
fill in time triggers.
in Actions, make an action with Start a program and in the Program/Script line provide the location where your bat script is. C:\myfolder\Test.bat
in the Start in (optional) line: enter C:\myfolder\
Note: both your .bat file and .R script are in the "C:\myfolder" folder.

cron job not creating logs

I am trying to execute some sql statements using an unix script. The script is placed in crontab to run everyday at 12.00 midnight and get the output in a log file.
Though my script is running and I can see the changes in DB but the log file is not generating. However manually running the script is generating the log file. Please suggest a solution.
now=`date "+%d%m%y"`
LOG="table_partition_$now.log"
test=`sqlplus -s ${USER}/${CPWD}#${DB} << THEEND > $LOG
...
...
...
exit
This is my code snippet. Please suggest
sqlplus has no closing `
Also, can you say whether the script runs correctly outside of cron? If it only fails in cron, you may want to call
env > /tmp/mylatestslog.txt
at the start and compare the differences with your local environment. (May be differences in the user, or variables used from you personal .bashrc).
(PS. also edited the question to show one command per line.)

How to run a R language(.r) file using Batch file?

I want to run a R script file (.r) using batch file.
If R.exe is in your PATH, then your windows batch file (.bat) would simply consist of one line:
R CMD BATCH your_r_script.R
otherwise, you need to give the path of R.exe, so for example:
"C:\Program Files\R\R-2.13.0\bin\R.exe" CMD BATCH your_r_script.R
you can add certain input arguments to the BATCH command, such as --no-save, --no-restore
so it would be
R CMD BATCH [options] your_r_script.R
more info on options, etc at http://stat.ethz.ch/R-manual/R-devel/library/utils/html/BATCH.html
Note: R uses the command "BATCH" to non-interactively evaluate a script located in a file. Here we are running the command "BATCH" from a windows .BAT file, but that's merely a coincidence.
An answer to another question suggests using Rscript.exe, so your batch file would contain:
"C:\Program Files\R\R-3.0.2\bin\i386\Rscript.exe" your_script.R
pause
It is a good idea to add R to the windows environment path. In a comment in this question #chase gave a link that explains how to set the path on windows 7. Once R is added to the windows path, your batch file should become simply :
Rscript.exe your_script.R
pause
You can also directly call a R command by using the -e flag. For example this batchfile will tell R to set its current working directory to Documents, then it will print the working directory:
Rscript.exe -e setwd('Documents');getwd()
pause
I struggled with the syntax with the answers below, but this worked for me in the .bat file:
C:\Windows\System32\cmd.exe /k ""path to Rscript.exe"
"path to .R script""
Be sure to place both the path to Rscript.exe and the script in "" together and separately as above.
I doubt you will be able to run it using a batch file.
http://www.fileinfo.com/extension/r
Most known programs that use .r files do so for source code files it looks like. You will probably have to compile it using the program it was written for. I guess you could use a command line compiler from a batch file, but I don't know what language or applications you are using.
If you post the script file or give more information about it, we could probably help you better.

Resources