Cron Job with Java.jar creates no File - java

I have a simple Java application on my Ubuntu 16.04 Server. This application generates a prices.txt file. The File is generated if I start it directly with:
java -jar TankstellenLogger.jar
If I schedule it with cron it doesn´t create that file and I have no Idea why? The file is running because in cron I see the Log.txt which I want to have the other output, but thats not all.
crontab -e
# m h dom mon dow command
* * * * * /home/dominik/startLogger.sh > /home/dominik/Log.txt
StartLogger.sh
#!/bin/bash
java -jar /home/dominik/TankstellenLogger.jar

"prices.txt" is possibly being written to a directory other than where you're expecting. Try adding this to the bottom of "startLogger.sh":
pwd
ls prices.txt 2>&1
That should print the working directory to "/home/dominik/Log.txt" as well as a check for whether "prices.txt" is there. If that info doesn't show up in "Log.txt", it may be a permissions problem.

Related

How to auto-trigger an executable JAR file

I have an executable JAR file on my desktop that I need to run once every three hours. I have no idea how to go about this. Please leave your suggestions/ideas.
You can use crontab (on Linux, Unix or MacOS) to run the jar at any time. To run the jar every 3 hrs, the cron expression would be
0 */3 * * * (java -jar ~/Documents/App/App.jar)
where ~/Documents/App/App.jar would be the location of the jar
For Windows OS, you may use schtasks. Check this link for the same : https://learn.microsoft.com/en-us/windows/desktop/taskschd/schtasks
You may also use the Task Scheduler of Windows to run the jar. Please check Run a jar file using windows scheduler for the same.
Probably, you can execute below command in command prompt to auto-trigger jar execution:
Schtasks /create /tn JarTask /tr C:\Users\abhinav\Desktop\MyApp.jar /sc hourly /mo 3

Java crontab (Ubuntu)

Having issues in setting up a Java crontab to run every 1 minute. New to this - have tried a few things though, appreciate any pointers
Java class runs on Ubuntu command without any issues
(Have updated CLASSPATH in bashrc for the dependent libraries etc.)
crontab -e
*/1 * * * * root (cd /usr/share/java; java packagename.class)
You can execute this script from anywhere. (That's because of the absolute paths)
#!/bin/sh
PATH=/usr/share/java;$PATH
java -cp /your/path a.b.c.ClassName &>/your/path/to/output

Script doesn't work from cronjob

I have a java program that has to run over the DB to reindex the entries. This is a job that has to be done once a week.
I have written a script that executes the program:
var=$(which java)
nohup $var -Xmx2048m -jar javaProgram.jar $* -d javaProgramResource -re > /nfs/inf/app_storage/logs/service/service_refresh.log 2>&1 &
The reason I put the Java path in a variable is becouse I want it to be generic, we have Java installed on different locations on every environment.
By executing this script manually, it works perfect.
./reindex.sh
Now, I wanted to create a cron job that executes this script once a week (every saturday at 6 o'clock in the morning - the duration of this job is about 16 hours becouse there are a lot of entries in the DB).
0 6 * * 6 cd /locattion/of/the/file; ./reindex.sh
Instead of getting the edited indexes in the log file (like when I start the script manually), I just get a message that says:
nohup: invalid option -- 'X'
Try `nohup --help' for more information
I guess it's a syntax error, but I'm not familiar with bash scripts and commands.
I found a solution.
In the .profile file were all the variables needed in the script.
I simply added the path of the .profile file to the cronjob and it worked just fine.
0 6 * * 6 . $HOME/.profile; cd /locattion/of/the/file; ./reindex.sh
Now the cronjob knows the Java-Path (becouse it's written in the .profile file) and the name of javaProgramResource, which is also different on every environment.
You guys gave me some very important input in which direction I should continue my investigation on this topic. Thanks!

Writing a bash script to run a java program

I'm rank new to bash thus the question.
I've a java program that I've exported as a .jar.
This file when run as
java -jar somefile.jar
goes into an infinite loop and awaits a file name. Based on the correct file path it generates an output.
How do I write a bash script to do automated testing of this project.
I need the scrip to do the following -
Run the program, which is run the same command
provide an array of 5 files as an input to the program
For each file write the output to an log file.
This should do it.
#!/bin/bash
files="$#"
for i in $files;
do
echo "Doing $i"
java -jar somefile.jar <<< "$i"
done
Make sure you chmod u+x filename it first. Then call it like this:
./filename firstfile secondfile thirdfile etc.
Other:
As sjsam pointed out, the use of <<< is a strictly bash thing. You are apparently using bash ("I'm rank new to bash..."), but if you weren't, this would not work.
Suppose my java program is HelloWorld.java. We can run it in 2 ways:
1st using executable jar
2nd by running java class from terminal
create a new text file and name it hello.sh
In hello.sh
!/bin/bash
clear
java -jar HelloWorld.jar
Save it and open terminal:
1 navigate to directory where your HelloWorld.jar is present
2 give permission to terminal to run the bash script by using the following command
sudo chmod 754 hello.sh
3 run you script by running the following command
./hello.sh

Redirect all output of the java program to log file

I have scheduled my jar file by saving this line at the end of the crontab:
30 12 * * * java -jar test.jar > test.log (I have also tried test.txt)
Cron has started the job, also created the log file, but there is nothing in that file. Any idea why is that? I am doing System.out.println() in my program.
I have added write permissions to everyone for that file.
Could it happen that the scheduler crashed before the app even started? When I have checked log of the crontab by executing grep CRON /var/log/syslog it gave me fine looking output, no error indication:
Sep 2 12:19:01 ip-172-31-18-162 CRON[2308]: (root) CMD (java -jar /opt/bitnami/apache-tomcat/webapps/apps/FullEmailReport/DJOF_FullEmail_DynamicContentReport_ScheduledPart.jar > /opt/bitnami/apache-tomcat/webapps/apps/FullEmailReport/data/app.txt)
Try adding 2>&1 to redirect System.err to the file too:
30 12 * * * java -jar test.jar > test.log 2>&1
This is a bit of an old question, but I thought it might be worth pointing out because no-one has mentioned it...
cron has no idea of your PATH, nor where java is. Back in my Solaris shell scripting days, we had to put full paths to everything in cron. Since I don't know where either your java installation is exactly, nor you test.jar file, I can't write the full exact command, but it might be something like:
30 12 * * * /usr/bin/java -jar /home/username/myTestStuff/test.jar > /user/username/myTestStuff/logfiles/test.log 2>&1

Categories

Resources