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!
Related
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.
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
I'm trying to run a jar file multiple times (in a loop) and redirect its output to a file (using the append >> operator). I'm running, and must keep using, Windows 7. I tried doing this in a windows batch file and ran into the problem below so I installed Cygwin to make use of the bash script. My script is below:
for i in {1..10..1}
do
echo "Run $i"
java -jar myjar.jar -cl >> runresults.txt
echo "Sleeping..."
sleep 60
echo "Awake!"
done
The problem: The script (Windows batch or Cygwin bash) runs only some iterations before hanging up (usually never more than 3). There's no error given. I added the sleep command to ensure that the prior iteration had time to release any locks prior to any attempt to make the next run. I've increased the sleep time to 200+ seconds and the behavior is still the same. Can anyone help me out on this?
if [ ! -f runresults.txt ]; then
touch runresults.txt
fi
for i in {1..10}
do
echo "Running... $i"
java -jar myjar.jar >> runresults.txt &
echo "Sleeping 2 seconds..."
sleep 2
done
I have a class that is being executed through a script in unix (Solaris 9). Inside of the script the class is being run like this:
java -cp $CLASSPATH myPackage.component.MyMainClass $PROPERTIES_PATH/myMainClassProperties.properties
That shell needs to be monitored in order to see whether it works correctly or it fails, and it also is going to be run parallel with a different parameter file. So, my questions are:
How can I know what class is being executed if I use top. Is that possible?**
Is there a way to uniquely identify the class so it doesn't crash when running parallel?
Will it always be shown as myPackage.component.MyMainClass in the table of processes?
When I say parallely, I refer to something like:
java -cp $CLASSPATH myPackage.component.MyMainClass $PROPERTIES_PATH/myMainClassProperties.properties
and in another window/session/job
java -cp $CLASSPATH myPackage.component.MyMainClass $PROPERTIES_PATH/mySomeOtherProperties.properties
**When I say so, is because top shows something like this:
PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
8545 batman 47 4 10 190M 112M sleep 0:04:00 0.07% java
9022 joker 91 4 10 286M 211M sleep 0:01:00 0.09% java
You can let top tell you the complete command, that will include any arguments you pass to the VM.
Also ps will give you the complete command as well (with the matching arguments).
In my debian I can switch the displayed command with the c toggle (start top and hit c until it shows the whole command)
for ps I use the arguments -ef (but -f) alone should do for that situation.
You may want to read the output of man ps and man top
It was possible to get the info using ptree | grep java or ptree | grep MyMainClass
It lists a tree of the processes that are being executed as well as the command that is being runned in there for example:
13456 ksh
123476 java -cp java -cp /classpath/fullpath myPackage.component.MyMainClass /full/properties/path/myMainClassProperties.properties
The first line is a shell script, and inside it (next line) calls the java jar. This works for Solaris 9.
Thanks #Angelo-Neuschitzer for the heads up.
I am using a java program which sends email after finishing up some file transfers.I am using Eclipse to code up the program. How do I set up a cron job to execute this java program for a particular time. Also I have various jar files inside the project. Please suggest
Write a shell script to invoke your java program with the necessary
arguments.
Make sure that the classpath argument points to the jars that you need.
Make sure that the shell script has necessary unix
permissions.
Schedule the script to be invoked by setting up a cron
job.
For more info about cronjob look here http://en.wikipedia.org/wiki/Cron
just my 2 cents...
r0ast3d has a quick, clear answer - I did have to do some more searching to get each step done so I'll elaborate on his steps:
Write a shell script to invoke your java program with the necessary arguments.
Example:
!/bin/bash
echo "Running script."
cd ~/your/classpath/to/java
java -classpath .:somejar.jar path/to/your/Program
Separate your necessary classpaths with colons (:) rather than semicolons (;)
The path to your program should start with your package (find this at the top of the java program)
Make sure that the classpath argument points to the jars that you need.
You can check your import statements in your java program to make sure you are specifying all the necessary classpaths. You have to run this script from your java directory, and can use a single period (.) as your first classpath argument.
Make sure that the shell script has necessary unix permissions.
Run from a terminal: sudo chmod ### yourScript.sh
Where ### are numbers representing the correct permissions for your system setup.
Schedule the script to be invoked by setting up a cron job.
Run from a terminal: crontab -e
This will open your crontab editor. You can add a job in this way:
*/5 * * * * bash /home/scripts/yourScript.sh
Replace the path to the script with the correct location of your script. This job is set to run every 5 minutes. See http://www.adminschoice.com/crontab-quick-reference/ for a good reference on crontab.
Hope this helps someone out!
use quartz for more complex need or Timer for a simpler task
There is the cron4j library http://www.sauronsoftware.it/projects/cron4j/. I have used it before to schedule a java program to run weekly. The scheduling syntax is the same as a crontab. The thing is, it needs to be constantly running as a background process to work. I ended up just using normal cron, but it could be useful if you're not on an Unix-like system, and you don't have cron.