0

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.

  • cron is your friend, for this. – ctrl-alt-delor Nov 3 '18 at 19:00
  • What needs to happen around switching to/from daylight saving? Is it very important that the job is run every two hours and is not allowed to skip or add an hour around then? – Kusalananda Nov 3 '18 at 20:29
  • yes, it should run every two hours. No skipping I guess as it would not effective represent 2 hours of monitoring rather less than 2 hours. We are planning to run some statistics on it. So, duration is of importance. – user3243499 Nov 4 '18 at 7:37
2

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 crontab -l.

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.

https://www.howtoforge.com/a-short-introduction-to-cron-jobs

1

cron is Linux's internal job scheduler.It helps in schedule your command to run at particular interval or date_time.

type crontab -e on 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

  • Is path/to/java required if I have setup JAVA_HOME? I can see java -version works in my machine. – user3243499 Nov 3 '18 at 19:59
  • no need to add path if environmental variable(JAVA_HOME) is set. But somehow if environmental variable messed up , added java path in cronjob won't brake – diveinsky Nov 4 '18 at 15:07

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.