How can I use Crontab in Linux specifically for a Java program? I want to run a MIS Script. How can I crontab it and what should the path be?
Assuming this Java application is a console based app there is nothing inherently special you need to do just because it's a Java application.
If you have a Java
.class file, run the application like so:
$ java HelloWorld
If you have a
.jar file, run the applicaiton like so:
$ java -jar myapp.jar
To make either of the above methods a cron job simply add these to a Bash script and put that script into one of hte designated crontab directories or simply add the above command to a crontab entry.
Making a script
Here's a script,
#!/bin/bash # Do any CLASSPATH stuff here $ java -jar myapp.jar
myjavawrapper.bashin one of the cron job directories or system crontab:
$ ls -d1l /etc/cron* drwxr-xr-x. 2 root root 4096 Nov 1 23:58 /etc/cron.d drwxr-xr-x. 2 root root 4096 Nov 3 23:46 /etc/cron.daily -rw-r--r-- 1 root root 0 Jun 29 2011 /etc/cron.deny drwxr-xr-x. 2 root root 4096 Oct 8 2011 /etc/cron.hourly drwxr-xr-x. 2 root root 4096 Dec 18 2010 /etc/cron.monthly
-rw-r--r-- 1 root root 451 Jun 2 12:10 /etc/crontab drwxr-xr-x. 2 root root 4096 Aug 12 2011 /etc/cron.weekly
Add an entry to
Add a line such as this to the crontab file:
*/30 * * * * root (cd /path/to/class/file; java HellowWorld)
The above will run
java HelloWorldevery 30 minutes.
The above are just 2 methods, they aren't the only methods. This is just to give you some ideas and approaches on how to accomplish the task. There are several other ways.