cron job not creating logs - unix

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.)

Related

Close and reopen + run an R script from itself on Mac

I would like to write an R script that can close and reopen + run itself.
This is needed for an API query that I am trying to make and which seems to require me going through these steps once every hour to be able to make additional requests. I tried to use the source() function - and simply run my script from itself every hour- but with this the API keeps rejecting additional requests; it seems that actually closing and opening the program is necessary.
I also tried to use the system() command - as described here to actually open R and execute the script - but I was not able to figure out how to implement this in a Mac environment.
Would you have any suggestions on how to do this?
The usual way to run a script every x amount of time on a Unix system is a cronjob. I'm not familiar with macOS, but apparently it works just as on Linux.
Open the job list to edit it (you can of course use another editor instead of nano)
env EDITOR=nano crontab -e
Add a line/job to the file. This runs any command line command. You can use Rscript to run an R script.
0 * * * * Rscript "/path/to/your/script.R"
Exit and safe. This script should now run every hour.
If you want to change the timing check out crontab.guru. Check out this answer, if the cronjob reports Rscript to be missing.

Different behavior when Rscript & rstan is run as a cron job

I try to run an R script at regular intervals to update a webpage. The script runs fine when called from the terminal like this:
/usr/local/bin/Rscript /Users/me/path/myscript.R
However, if I try running it as a cron job, I get an error. I add the job to crontab like this:
46 10 * * * /usr/local/bin/Rscript '/Users/me/path/myscript.R' >> '/Users/me/path/mylog.log' 2>&1
The script does run in R, but aborts due to an error. Specifically, I fit some models using rstan, and get an initialization error. (The error only applies to some models, while others still run fine.) The initialization values are valid by definition, but do not seem to be used properly. It is like rstan is doing math differently (and wrong) when it is run through cron.
The session info from R is identical whether I run the script in the terminal or as a cron job. My question is what else might still differ depending on how the script is run. Could rstan be using a different version of C++ when run as a cron job? Are there other paths I may need to set to get this to work correctly?
Update: The script also works if I run it using R CMD BATCH in terminal, but not if I use R CMD BATCH in a cron job. Using launchd triggers the same issue. I also tried using CmdStan through cmdstanr, and the same same thing happens: Runs fine until added to a cron job.
Edit 2: The models I thought ran fine in cron, were not actually fine. The results were wrong, until I used the fix explained below.
It looks like I finally managed to fix this, and I'm posting my solution here for anyone who encounters the same problem.
I ran env in terminal to see my current user environment. I copy-pasted the full output to the top of my crontab file. (Simply adding the PATH variable was not sufficient. I suppose it was SHELL or perhaps both PATH and SHELL that did the trick, but I haven't explored this further.)
To edit my user's crontab, I ran crontab -e, then pressed i to edit the file, pasted everything from env at the top of the file, stopped editing by pressing ctrl + c, and quit by typing :wq and hitting enter.

Use crontab to automate R script

I am attempting to automate a R script using Rstudio Server on ec2 machine.
The R script is working without errors. I then navigated to the terminal on RStudio Sever and attempted to run the R script using the command - Rscript "Rfilename" and it works.
At this point I created a shell script and placed the command above for running the R script in there. This shell command is also running fine - sh "shellfilename"
But when I try to schedule this shell command using crontab, it does not produce any result. I am using the following cron entry :
* * * * * /usr/bin/sh ./shellfilename.sh
I am using cron for the first time and need help debug what is going wrong. My intuition is that there is there is difference in the environments used by the command when I run it on terminal and when I use the same in crontab. In case it is relevant information - am doing all of this on a user account created for myself on this machine so would differ from admin account.
Can someone help resolve this issue? Thanks!
The issue arose due to relative paths used in the script for importing files and objects. Changing this to absolute path resolved the described issue.

Crontab : Running a jar file

I have a jar file that generates reports and i want to schedule to run it on every sunday,
For this purpose, i am using the crontab functionality on linux,
i have created the crontab entry using
crontab -e
45 15 * * 1 /usr/java/default/bin/java -jar /home/name/example/withouttimer.jar
but the job does not run as it should, Can you please help me find the issue with it,
Is there a way to check crontab logs?, thanks
Probably the profile used when running cron jobs does not have some variables set (JAVA_HOME? CLASSPATH?)
Do a crontab that does printenv > myfile.txt and check what is defined.
As a last test, create a .sh file and run it, that does
echo 'hello'
printenv
echo 'goodbye'
and see if redirecting the execution of your script to a log shows something.
That job runs at 3:45 pm on every monday (0 is for sunday)Are you sure you are checking it at right time?
Try running a dummy program at lesser interval to check whether its working.
Finally got the solution,
There was a couple of things that i was doing incorrectly,
The timezone of the server was different, so i had to set it accordingly.
The jar was not able to locate the input file(bad location), had to fix it.
Works gr8 now, thanks for the suggestion on adding MAILTO= , helped in debugging.

Executing an script with crontab returns 127 error code

I am executing a script manually on my UNIX system manually, it runs successfully and updated required records in ORACLE database. How ever when I exwcute the same script with crontab my process exits with the error code 127.
On analysing further, I got there is some problem in these statements.
LOGFILE=sachin
ORALOGIN=abc/abc#abcd
cmd='sqlplus ${ORALOGIN} < SQLS >> ${SVC_HOME}/LOGFILES/${LOGFILE}.date +%Y-%m-%d';
eval $cmd
Please suggest a solution
Are you sure that ${ORALOGIN} and ${LOGFILE} are valid ENV variables when cron executes the script?
Sometimes scripts that work for users don't work for cron because cron executions don't have all the ENV variables that users have.
In the interactive enviroment do
env | grep ORACLE
Long time since I've used sqlplus, but I recall it required some environment variables to function (ORACLE_HOME?) and I'm suspecting your cron job environment does not have these set.

Resources