Crontab : Running a jar file - unix

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.

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.

Check Cron job ran in last 10 minutes

I have multiple scripts running in cron and my cron log is very flooded .
I just need to write a unix command to check if one particular scheduled script ran in last 10 minutes .
You can "touch" some file from script and check atime of this file.
Something like "touch -a /var/run/app.state" from cron script.
And for check you can use stat command with custom format to extract access time of file and calculate difference between now and this time.

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 do I execute this command using crontab?

I run a list of tests using this command: "./kaboom testlist" which is written in Python.
This command can only be excuted in the directory /e/m/user/testing3/kaboom
I want to run this command every night at midnight using a cronjob. The only examples I've found online are prewritten shell scripts. So, I am not sure how to format this into a crontab so that it does what I want.
Any suggestions?
Say you want to run a job every night at 10pm then the crontab would look like:
00 10 * * * (cd /e/m/user/testing3/kaboom ; ./kaboom testlis ) > /tmp/cronjob.log 2>&1
Crontab entry just contains the commands you want to execute.
For examples on timings please check this geekStuff link.

Issues with cron

I'm really stuck. I've used cron on many machines, but I simply can not get it to work on an Ubuntu server. I've tried everything I can think over the weekend and I'm baffled. I'm hoping someone can help me out.
I can verify that cron is running via pgrep cron. However, pgrep crond returns nothing. I am trying to run a simple shell script (test.sh) with cron:
#!/bin/sh
/usr/bin/touch /home/jarvis/test.txt
I have run chmod +x on the shell script and my crontab looks like this:
01 * * * * /home/jarvis/test.sh
I also have a new line ending after the line.
Try redirecting all output to a file and see if there are any error messages that can help you diagnose the problme, i.e.
01 * * * * /home/jarvis/test.sh > /tmp/jarvis_test.log 2>&1
Also, if you created any of those files from a windows environment, don't forget dos2unix filename
edit
My point is that you won't know for what your script is doing in the crontab environment unless you see if it is outputing an error message. Error messages also go to local crontab user's email, so try mail (as the same user) and see if you have a bunch of messages from your crontab.
pgrep crond returning nothing sounds like the problem, but I'm not a sysadmin. Maybe you want to flag this and ask for moderator to move to https://serverfault.com/
I hope this helps.
I think it is related to the user permissions. Try putting in default crontab hourly,weekly files, if it works there then your cron is good. Also, check /var/log/messages for any cron entry. View /etc/crontab for detailed configuration.

Resources