Unable to access jarfile using crontab - java

I have two jar file that I want to run using crontab. Let's say that there is an A and B jarfile. When I want to run the crontab for the A jarfile, the crontab work fine. But when I want to run for the B jarfile, the crontab is unable to access the jarfile. Where could go wrong with this? Is the problem is on the jarfile, or on the crontab settings?
Here is my crontab setting
0 12,14 * * * root /root/folderjarfile/runx.sh >> /root/folderjarfile/nohup.out

I think there may be problem with your jar file. Possible options to find it out.
Run that jar without crontab means submit java -cp jarPath main-method
If option-1 works fine then, You have to restart your cron tab
Stop Cron Job: /etc/init.d/crond stop
Start Cron Job: /etc/init.d/crond start

Cron usually does not run your profile. So you only get a basic environment.
try putting the following in your runx.sh
env |sort >/tmp/my_cron_env.txt
Let your job run and have a look at your PATH and other variables. You will probably find things that you expect to be there (e.g. JAVA_HOME) are missing.

Related

writing a cron job prgrammatically to crontab file does not run

I am writing a cron job to my crontab file using following java code
PrintWriter writer = new PrintWriter("/var/spool/cron/crontabs/multi","UTF-8");
String cronTabString="25 13 * * * sudo wget --tries=0 \"https://some-url.com:7443/DataUpdater/updateChildren?folderId="+folderId+"&clientId="+clientId+"&clientSecret="+clientSecret+"&refreshToken="+refreshToken+"&deptName="+deptName+"\"";
writer.println(cronTabString);
writer.close();
The code is successfully writing to cron tab file . But it does not run . My question is that is it even possible to execute a cron job in this manner ? According to me it is a valid cron tab entry , so it should get executed . Am i missing something here ?
Try the following:
crontab -r
crontab /var/spool/cron/crontabs/multi
(edit) I believe in Java, you can call these this way:
Runtime.getRuntime().exec("crontab -r");
Runtime.getRuntime().exec("crontab /var/spool/cron/crontabs/multi");
First one removes the existing crontab. Second one installs the new crontab. And then you can verify by doing:
crontab -l
The crontab scheduler is not just a file. It is a process that runs as a daemon. When you traditionally edit the crontab file its saves the crontab entries and reload the crontab process. Because of this reason, simply writing to the file will not help. You will need to reload or restart the cron daemon.
Update
You can restart the cron daemon if you have root access on the server by using the following command:
/etc/init.d/cron reload
So, in your Java program running on the server, you will need to do this:
Runtime.getRuntime().exec("/etc/init.d/cron reload");
This will run the system command to restart the cron daemon and reload the cron entries.
Hope this helps!
Update 2
Different flavors of Linux have different commands to restart services or daemons.
You can use this as a good starting point to check which command will go as a parameter into the exec method above.
Cron Command Syntaxes on different Linux Versions

Java JAR file does not execute in startup script in Ubuntu 14.04

The following process normally works for my startup scripts. However, when I introduce a command to execute a JAR file, it does not work. This script works while I am logged in. However, it does not work as a startup script.
In /etc/init.d I create a bash script (test.sh) with the following contents:
#!/bin/bash
pw=$(curl http://169.254.169.254/latest/meta-data/instance-id)
pwh=$(/usr/bin/java -jar PWH.jar $pw &)
echo $pwh > test.txt
Make script executable
In /etc/rc.local, I add the following line:
sh /etc/init.d/test.sh
Notes:
I make a reference to the script in /etc/rc.local, because this script needs to run last after all services have started.
Please do not ask me to change the process (i.e., create script in /etc/init.d/ and reference it from /etc/rc.local), because it works for my other startup scripts.
I have tried adding nohup in front of java command, and it still did not work.
Thanks
As written, there is insufficient information to say what is going wrong. There are too many possibilities to enumerate.
Try running those commands one at a time in an interactive shell. The java command is probably writing something to standard error, and that will give you some clues.

Crontab + ubuntu + java not working

If I directly run the bellow code in terminal it is working perfect.
/usr/bin/java -jar /var/jboss-as-7.1.1.Final/standalone/email_linux/notification_18.jar
But when I set the same as a crontab in ubuntu server then it is not working.
*/3 * * * * /usr/bin/java -jar /var/jboss-as-7.1.1.Final/standalone/email_linux/notification_18.jar
Any one know why unexpected behavior?
The crontab task is executed under a different user from the one with which you are testing the call. JBoss depends on a number of environment variables, which are probably undefined in that context. So find out what that particular JAR needs from the environment and then add it into a shell script which you run from the cron task (instead of directly running java from cron).
Make sure that you have included the absolute paths in the source code if you are reading or writing to a file, even if the jar file and the reading file are in the same directory.
It is resolved after specifying absolute path

Is there anyway to execute a jar file with a single word (or phrase) in Terminal?

I'm building a server in Java. I want to setup a single word option that will start and stop the server. "start" to start the server. "stop" to stop the server. I tried to use an executable file that launched the jar, but even using that strategy, I still have to type "./start". The punctuation is a little ugly.
I wanted something similar to "git". If you type "git" in Terminal, you are immediately using git's tools. I'm guessing this is because of a symlink? If so, how did the symlink for "git" get setup? I didn't have to manually set it up from what I remember.
If you would like to avoid sh or ./ while executing you may use alias, add following at the end in ~/.bashrc file:
alias sayhello="./sayhello.sh"
And then run the .bashrc file using following command:
source .bashrc
Finally, you should be able to execute your command using just:
sayhello
What OS are you working on? In windows you just have to add the location of start.exe to your PATH, then typing "start" in the command prompt will launch the executable and the jar

Error with crontab running a jar file

I'm running Ubuntu 10.10 with open jdk and crontab. I made a neat little Java application that does some cool stuff and exported it as a runnable jar file. I can run it from the command line just fine, but I needed something that would run the file once every day. So I wrote in a cron job to run a shell script that would actually invoke the jar file. The shell script works fine when run on its own and it looks something like this:
#!/bin/sh
/usr/bin/java -jar /root/proj/CoolStuff.jar
Works perfectly. So I added this to the crontab:
23 14 * * * /root/proj/runScript.sh > /root/proj/log.txt 2>&1
This does not run perfectly. In fact, it doesn't run. log.txt will come out saying "Error". Pretty nondescript, right? I've checked my environment variables, nothing fancy there. Is there anything else I might be missing? Any ideas as to where to go from here? Is there any way I can run this script everyday with ease?
Check the execution permission on that file.
The cron is running with different permissions, not the one you got when logged in.
Also you try to access /root.
Try to relocate your script to another "non-root" directory.
See what the environment for you crontab looks like by commenting out your current /usr/bin/java .. and insert set on a line by its own.
Now from you command line do set > tmpEnvVarList.txt and compare with what you see in log.txt.
It's almost certainly that your .profile or .bash_rc (or other) is setting env vars that are not available to you crontab. You'll need to add . .profile etc to you script.
cbO's ideas are good too.
I hope this helps.

Categories