Main Tutorials

Java – Cron job to run a jar file

Quartz is good, but often times we just need a simple scheduler system to run a jar file periodically. On *unix system, you can use the build-in cron to schedule a scheduler job easily.

In this example, we will show you how to create a cron job on *nix to run a jar file, by daily and hourly.

1. Create a Cron Job

To create a cron job, in terminal, type crontab -e to edit the cron job. Review following examples :

1.1 Run daily at 00:00, accepts two parameters.


# run everyday
0 0 * * * java -jar /home/mkyong/crawler/webcrawler.jar param1 param2

1.2 Run daily at 02:00 am, pass rir.name as a system property with -D option.


# run everyday at 2am
0 2 * * * java -jar -Drir.name="ripe" /home/mkyong/crawler/whoiscrawler.jar

1.3 Run hourly, assume this jar is logging message with the logback framework.


# run every hour
0 * * * * java -jar -Dlogback.configurationFile=/home/mkyong/logback.xml /home/mkyong/crawler/crawler.jar

Exit, save and override, done.

Display Cron Jobs
To display the existing cron job, type crontab -l

References

  1. Wikipedia – Cron
  2. Linux Crontab: 15 Awesome Cron Job Examples
  3. Crontab – Quick Reference

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Uday Kumar Nalam
7 years ago

Hi Sir,
I am a big fan of your blog, Spring Batch provides huge advantages. However, how would we achieve these all features without using spring batch ? Can you explain ?

Can you please let me know, what are the different batch procession systems available in Java and differences among them ?

Why I am asking this questions is ..if I want to go for Spring batch processing.. I need to know about some features of Spring DI and IOC.. So, where the people doesn’t know have idea about Spring.. it is bit difficult maintain Spring Batch..

Please explain the Bath processing by using Core java application….

Rickooni Dioser
2 years ago
prashant kushwaha
6 years ago

what is param1 and param2 here

Sorabh
8 years ago

I and trying to run a jar file using cron directly like the above command but it’s not working, however when I put the same command in a shell script and run that script through cron It is working absolutely fine.

john
9 years ago

hii i need a time locker and a date locker app to shedule a video unlock to set time and date