File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes linux cron task to invoke java class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "linux cron task to invoke java class" Watch "linux cron task to invoke java class" New topic
Author

linux cron task to invoke java class

Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Hi Guys,

we have implemented a complex java module for searching relational databases.our database server is hosted on a linux machine.
in this java module, there is the feature of indexing database data.
one requirement in our project is to refresh the index daily & weekly with some sort of automatic cron task .

we would like to know how we can write a Linux shell script to invoke our index refresher (java class) automatically .and how can we schedule the index refresher so it is invoked automatically every day at a given time. we would also prefer to make the scheduling configurable (change time ,date ).

what is the best approach to implement this task ?

thank you for sharing your ideas with us.
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Calling a Java class from a shell script is very straightforward. You can can do something like this.


To set a scheduled task in Linux/UNIX, you can use crontab. Add a new entry in the crontab to call that shell script. For more information, do man crontab. This should also be fairly straightforward.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Thanks for prompt & very clear reply.

we will try this method.
thanks again.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Hi,
Actually i need to write a java command line utility that could be run in same way (like ant for example) in various OS.

for example if we call this cmd line utility 'abc' then it should be invoked from command line (unix) like : $abc [parameters options] (example: $ ant build.xml).
if i write a java command line application and want to invoke it as a cmd line utility with a given name 'abc' and list of params ,how can i do that ?

thanks.
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Hopefully I get you correctly.

Actually ant/java command itself isn't platform independent. If you're in Windows, it's called ant.exe or java.exe and if you're in Linux is just called ant or java. Both are binary files. You can't run ant.exe/java.exe in Linux and vice versa. All I want to say is both are different binaries even though they have the same name.

If you're thinking on writing a script, most of the projects that I know will provide the .bat or .cmd if it's used for Windows environment and .sh if it's used in Linux/UNIX environment. Take a look at tomcat. You'll see that there's catalina.bat in Windows and catalina.sh in Linux.

Hope it's clear enough for you.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Originally posted by Freddy Wong:
Hopefully I get you correctly.

Actually ant/java command itself isn't platform independent. If you're in Windows, it's called ant.exe or java.exe and if you're in Linux is just called ant or java. Both are binary files. You can't run ant.exe/java.exe in Linux and vice versa. All I want to say is both are different binaries even though they have the same name.

If you're thinking on writing a script, most of the projects that I know will provide the .bat or .cmd if it's used for Windows environment and .sh if it's used in Linux/UNIX environment. Take a look at tomcat. You'll see that there's catalina.bat in Windows and catalina.sh in Linux.

Hope it's clear enough for you.


Thanks anyway , i'm aware of the fact that those are binaries targeted to different os. i was wondering if it's easy to convert a java cmd line app into a binary ?(exe on windows or bin on linux).
if i'm not wrong this will involve using jini right ?

If i use a bash script for launching the java program ;how can i pass cmd line parameters to bash script and make them available as command arguments for the java program ?

thanks.
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959


If i use a bash script for launching the java program ;how can i pass cmd line parameters to bash script and make them available as command arguments for the java program ?

This should be easy


So to invoke that script, let's name it MyScript.sh
$ ./MyScript.sh arg1 arg2 arg3

Hope this helps.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

thanks much

your help is much appreciated.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Originally posted by Freddy Wong:
Actually ant/java command itself isn't platform independent. If you're in Windows, it's called ant.exe or java.exe and if you're in Linux is just called ant or java. Both are binary files. You can't run ant.exe/java.exe in Linux and vice versa. All I want to say is both are different binaries even though they have the same name.

If you're thinking on writing a script, most of the projects that I know will provide the .bat or .cmd if it's used for Windows environment and .sh if it's used in Linux/UNIX environment.


If you call java in Windows, the system will look for a java.bat, if not present for java.com, and finally for a java.exe - afaik.
So if there is no java.bat and no java.com, you may call java java on windows too - but there is a funky drive letter and slashes the other way round - probably a complete different path.

And there is a sh-like-shell for windows too: http://gnuwin32.sourceforge.net/

But I guess the problem is already solved,


http://home.arcor.de/hirnstrom/bewerbung
 
 
subject: linux cron task to invoke java class