我正在使用一个Java程序,在完成一些文件传输后发送电子邮件。我使用Eclipse编写程序。如何设置cron作业以在特定时间执行此Java程序?另外,我的项目中有各种jar文件,请给出建议。
我正在使用一个Java程序,在完成一些文件传输后发送电子邮件。我使用Eclipse编写程序。如何设置cron作业以在特定时间执行此Java程序?另外,我的项目中有各种jar文件,请给出建议。
关于cronjob的更多信息,请查看http://en.wikipedia.org/wiki/Cron
这只是我的个人意见...
r0ast3d已经给出了快速明确的答案。我需要做更多的搜索来完成每一步,所以我将详细介绍他的步骤:
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)
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.
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.
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.
有一个名为cron4j的库http://www.sauronsoftware.it/projects/cron4j/。我以前用过它来安排Java程序每周运行一次。调度语法与crontab相同。问题是,它需要作为后台进程不断运行才能工作。最终我只使用了普通的cron,但如果你不在类Unix系统上,并且没有cron,它可能会很有用。