I want to run a small monitoring jar file every 2 hours in my ubuntu machine.
java -jar mymonitoringtool.jar
I don't know how to set it up.
You can set up a crontab with the following expression for your requirements.
0 */2 * * * path/to/java -jar path/to/mymonitoringtool.jar &> path/to/monitor-$(date "+%Y%b%d-%H%M%S").log
You should first check for any existing cron jobs by running
If you don't have any cron jobs yet, you can put the above line in a text file (say my-cron-job.txt) and run
crontab my-cron-job.txt to activate it. You can then verify that the job has been added by running
crontab -l once again.
If you do have existing cron jobs, you'll need to run
crontab -e and edit the crontab file. This will require you to work with a command-line editor (vi, by default).
The cron daemon only sets a limited number of environment variables, so it's preferable to specify the full path to java executable. You will also need to specify the full path to your executable JAR file.
I'm assuming that you want to collect the output from the
java -jar command, so I have added a log file to the command. The output and error streams from the command will be redirected to this log file, which will be named with a timestamp (monitor-2018Nov03-200000.log, for example).
Once everything is set, you should monitor the cron job for the first one or two executions. If everything works as you need, you can then leave it running.
As an external reference, here's an introduction on cron jobs. This should help you understand the how the scheduling actually works.
cron is Linux's internal job scheduler.It helps in schedule your command to run at particular interval or date_time.
crontab -eon the terminal
add this line
0 */2 * * * path/to/java -jar path/to/mymonitoringtool.jar
save and exit
Crontab Syntax as follows
00 */2 * * * path/to/java -jar path/to/mymonitoringtool.jar
0th-minute every-2nd-hour every-day every-month every-weekday run-this-command
For more options and information: https://help.ubuntu.com/community/CronHowto