2

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?

3

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

Cron job

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.

Examples

  1. Making a script

    Here's a script, myjavawrapper.bash.

    #!/bin/bash
    
    # Do any CLASSPATH stuff here
    $ java -jar myapp.jar
    

    Then put myjavawrapper.bash in 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

  2. Add an entry to /etc/crontab

    Add a line such as this to the crontab file:

    */30  *  *  *  * root  (cd /path/to/class/file; java HellowWorld)
    

    The above will run java HelloWorld every 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.

  • 1
    I'm not sure if 30 * * * * will run every 30 minutes. Isn't it supposed to be */30 * * * there? I think the command as it is will run at e.g 01:30 02:30 03:30 – Valentin Bajrami Nov 5 '13 at 8:20
  • 1
    @val0x00ff - thanks, you'd be correct. That runs it at 30 minutes past the hour. I'll fix. – slm Nov 5 '13 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.