eclipse Java 程序的 Cron 作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7855666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cron job for a Java Program
提问by mona
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
我正在使用一个 java 程序,它在完成一些文件传输后发送电子邮件。我正在使用 Eclipse 对程序进行编码。如何设置 cron 作业以在特定时间执行此 java 程序。此外,我在项目中有各种 jar 文件。请建议
回答by r0ast3d
- Write a shell script to invoke your java program with the necessary arguments.
- Make sure that the classpath argument points to the jars that you need.
- Make sure that the shell script has necessary unix permissions.
- Schedule the script to be invoked by setting up a cron job.
- 编写一个 shell 脚本以使用必要的参数调用您的 java 程序。
- 确保 classpath 参数指向您需要的 jars。
- 确保 shell 脚本具有必要的 unix 权限。
- 通过设置 cron 作业来安排要调用的脚本。
For more info about cronjob look here http://en.wikipedia.org/wiki/Cron
有关 cronjob 的更多信息,请查看这里http://en.wikipedia.org/wiki/Cron
just my 2 cents...
只是我的 2 美分...
回答by Casey Murray
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:
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.
编写一个 shell 脚本以使用必要的参数调用您的 java 程序。例子:
!/bin/bash echo "Running script." cd ~/your/classpath/to/java java -classpath .:somejar.jar path/to/your/Program
用冒号 (:) 而不是分号 (;) 分隔必要的类路径 程序的路径应该从你的包开始(在 java 程序的顶部找到它)
确保 classpath 参数指向您需要的 jars。您可以检查 java 程序中的导入语句,以确保指定了所有必需的类路径。您必须从 java 目录运行此脚本,并且可以使用单个句点 (.) 作为您的第一个类路径参数。
确保 shell 脚本具有必要的 unix 权限。
从终端运行:
sudo chmod ### yourScript.sh
其中### 是代表系统设置正确权限的数字。
通过设置 cron 作业来安排要调用的脚本。
从终端运行:
crontab -e
这将打开您的 crontab 编辑器。您可以通过以下方式添加作业:
*/5 * * * * bash /home/scripts/yourScript.sh
将脚本路径替换为脚本的正确位置。此作业设置为每 5 分钟运行一次。有关crontab 的良好参考,请参阅http://www.adminschoice.com/crontab-quick-reference/。
Hope this helps someone out!
希望这可以帮助别人!
回答by Shahriar
回答by Christine
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.
有 cron4j 库http://www.sauronsoftware.it/projects/cron4j/。我以前用它来安排一个每周运行的 java 程序。调度语法与 crontab 相同。问题是,它需要作为后台进程不断运行才能工作。我最终只使用了普通的 cron,但是如果您不是在类 Unix 系统上并且没有 cron,它可能会很有用。