Java crontab (Ubuntu) - java

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

Related

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!

Cron Job with Java.jar creates no File

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.

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

Writing a bash or some other script to restart another script

I am running a minecraft server for my son, i'm new at minecraft servers, anyway it seems it keeps dying as it cant keep up? ok so its a VM and i will be assigning more resources to it at some point but my question is as follows
I initiate the server to load up via a script
bukkit.sh is the script an inside it has
#!/bin/bash
java -Xmx1024M -jar /minecraftserver/bukkitserver/craftbukkit-1.7.1.jar -o true
now at somepoint during the day or night it will die as it cant keep up, is there a way i can have some other script run alongside it and see that the process has died and to run that script again to start the server.
when i run top i can see that java is at the top using all the resources so im 100% thats the minecraft java. Does the PID stay the same each time it loads up?
Would be great if someone could let a hand on this ...
I assume the java process remains in the foreground once it's launched? Is there any reason you can't just do:
while :
do
java -Xmx1024M -jar /minecraftserver/bukkitserver/craftbukkit-1.7.1.jar -o true
done
Then any time the java exits, the script will simply restart it.
EDIT:
You could create a script, lets call it craftbukkit.sh
Make sure it is execute by using: chmod +x craftbukkit.sh
Then inside the script you would write:
#!/bin/sh
ps auxw | grep craftbukkit-1.7.1.jar| grep -v grep > /dev/null
if [ $? != 0 ]
then
/path/to/your/bukkit.sh
fi
Change /path/to/your/bukkit.sh to wherever that minecraft startup script is.
Then you need to add this script to your crontab, this is accomplished by entering the crontab editor: crontab -e
If you want the script to run every 5 minutes, add a line that looks like this:
*/5 * * * * /path/to/craftbukkit.sh
Note that you must change the /path/to to point to where you have created the craftbukkit.sh script.

jar: command not found in Bash

I created one jar file in following location: '/usr/local/bin/niidle.jar'
But when I type command as follows:
jar tf /usr/local/bin/niidle.jar
then it shows the following error:
--The program 'jar' can be found in the following packages:
* java-gcj-compat-headless
* gcj-4.2
* sun-java5-jdk
* kaffe
* gcj-4.3
* cacao-oj6-jdk
* openjdk-6-jdk
* fastjar
* sun-java6-jdk
Try: apt-get install <selected package>
bash: jar: command not found
Do you have a solution for this?
Apparently the jar program is not found by the operating system. Have you installed any of the packages containing it, and set up your path correctly?
In order for the OS to find an executable, the path environment variable must include the directory where that program is located. Otherwise you need to specify its full path to access it.
Well, it can't find the jar program. Perhaps your JDK isn't on the path, or you haven't installed one? If you have installed one, try to find where it is, and look in there for the jar program.
Try runnning "java -jar /usr/local/bin/niidle.jar"
On Ubuntu 12.10 The program jar is in /usr/lib/jvm/jdk-[version]/bin
edit the file /etc/environment for add to the path. Close the session and open a new session.
You seems to be using either Debian or Ubuntu. And it is telling you that you did not install one of the packages containing the 'jar' executable. You will have to install one corresponding to the java package you use on your system. In order to know which java package you already have type this : dpkg -l | grep java
If you are like me and you already have everything installed, but you just get the same error when trying to type jar: it should be listed in your PATH.
Run echo $PATH.
If you're like me, it does not contain the entry like this /usr/lib/jvm/jdk1.8.0_66/bin.
Make sure it's not $JAVA_HOME/bin, all variables should be expanded!
If you don't have the entry, but have JAVA_HOME set, run export PATH=$PATH:$JAVA_HOME/bin
See if you got the jar command.
If it does, google about environment variables to set that effect forever.
Have a nice day.

Categories

Resources