Java Cron job to run a jar file
By:Roy.LiuLast updated:2019-08-17
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
To display the existing cron job, type crontab -l
References
From:一号门
COMMENTS