Cron job for a Java Program

前端 未结 4 846
渐次进展
渐次进展 2020-12-05 22:19

I am using a java program which sends email after finishing up some file transfers.I am using Eclipse to code up the program. How do I set up a cron job to execute this java

4条回答
  •  情深已故
    2020-12-05 22:57

    r0ast3d has a quick, clear answer - I did have to do some more searching to get each step done so I'll elaborate on his steps:

    1. Write a shell script to invoke your java program with the necessary arguments. Example:

      !/bin/bash
      echo "Running script."
      cd ~/your/classpath/to/java
      java -classpath .:somejar.jar path/to/your/Program
      

      Separate your necessary classpaths with colons (:) rather than semicolons (;) The path to your program should start with your package (find this at the top of the java program)

    2. Make sure that the classpath argument points to the jars that you need. You can check your import statements in your java program to make sure you are specifying all the necessary classpaths. You have to run this script from your java directory, and can use a single period (.) as your first classpath argument.

    3. Make sure that the shell script has necessary unix permissions.

      Run from a terminal: sudo chmod ### yourScript.sh

      Where ### are numbers representing the correct permissions for your system setup.

    4. Schedule the script to be invoked by setting up a cron job.

      Run from a terminal: crontab -e

      This will open your crontab editor. You can add a job in this way:

      */5 * * * * bash /home/scripts/yourScript.sh

      Replace the path to the script with the correct location of your script. This job is set to run every 5 minutes. See http://www.adminschoice.com/crontab-quick-reference/ for a good reference on crontab.

    Hope this helps someone out!

提交回复
热议问题