Thursday, 25 April 2013

Unix: Execute Java Command in crontab

For executing java command in crontab , create a shell script with the Java command in it and execute it in crontab :
For Example , take a look at following script :

#!/bin/bash
 #script name : javacheck placed in /home/user
cpath="/home/user"
/usr/lib/jvm/jdk1.6.0_25/bin/java -cp $cpath/jar1:$cpath/jar2 classname >/home/user/javacheck

Always provide absolute path of java binary as well as of the jars since crontab won't be able to read environment variables of user. Otherwise , you can manually set variables in script or in crontab itself so that they can be referred.
Make the file executable to user

Crontab Entry:
* * * * * /home/user/javacheck 

Don't put sh or bash in front of crontab entry.
Also, if you want to execute java in crontab without writing shell script, it can be done as follows;

* * * * * /usr/lib/jvm/jdk1.6.0_25/bin/java -version 2>/home/user/javaversion

Again don't include sh or bash in front of java command because sh or bash won't execute binary file. Also, output of java -version will be redirected as standard error not standard output, thats why i used standard error redirection here

No comments:

Post a Comment