Java 程序的 Cron 作业
我正在使用一个java程序,它在完成一些文件传输后发送电子邮件。我正在使用Eclipse来编写该程序。如何设置一个 cron 作业来在特定时间执行这个 java 程序。我的项目内还有各种 jar 文件。请建议
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 program for a particular time. Also I have various jar files inside the project. Please suggest
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
论据。
权限。
工作。
有关 cronjob 的更多信息,请查看这里 http://en.wikipedia.org/wiki/Cron
只是我的 2 美分.. 。
arguments.
permissions.
job.
For more info about cronjob look here http://en.wikipedia.org/wiki/Cron
just my 2 cents...
r0ast3d 有一个快速、清晰的答案 - 我确实必须做更多搜索才能完成每个步骤,因此我将详细说明他的步骤:
编写一个 shell 脚本以使用必要的参数调用您的 java 程序。
示例:
用冒号 (:) 而不是分号 (;) 分隔必要的类路径
您的程序的路径应以您的包开头(在 java 程序的顶部找到它)
确保类路径参数指向您需要的 jar。
您可以检查 java 程序中的 import 语句,以确保指定了所有必要的类路径。您必须从 java 目录运行此脚本,并且可以使用单个句点 (.) 作为第一个类路径参数。
确保 shell 脚本具有必要的 unix 权限。
从终端运行:
sudo chmod ### yourScript.sh
其中 ### 是代表系统设置正确权限的数字。
通过设置 cron 作业来安排要调用的脚本。
从终端运行:
crontab -e
这将打开您的 crontab 编辑器。您可以通过以下方式添加职位:
*/5 * * * * bash /home/scripts/yourScript.sh
将脚本的路径替换为脚本的正确位置。该作业设置为每 5 分钟运行一次。请参阅 http://www.adminschoice.com/crontab-quick-reference/关于 crontab 的一个很好的参考。
希望这可以帮助别人!
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:
Write a shell script to invoke your java program with the necessary arguments.
Example:
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.
Hope this helps someone out!
使用 quartz 来满足更复杂的需求或 Timer 用于更简单的任务
use quartz for more complex need or Timer for a simpler task
有 cron4j 库 http://www.sauronsoftware.it/projects/cron4j/。我以前用过它来安排一个java程序每周运行一次。调度语法与 crontab 相同。问题是,它需要作为后台进程不断运行才能工作。我最终只使用了普通的 cron,但如果您不在类 Unix 系统上并且没有 cron,那么它可能会很有用。
There is the cron4j library http://www.sauronsoftware.it/projects/cron4j/. I have used it before to schedule a java program to run weekly. The scheduling syntax is the same as a crontab. The thing is, it needs to be constantly running as a background process to work. I ended up just using normal cron, but it could be useful if you're not on an Unix-like system, and you don't have cron.