Java 程序的 Cron 作业 [英] Cron job for a Java Program

查看:19
本文介绍了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

推荐答案

r0ast3d 有一个快速、清晰的答案 - 我确实需要做更多的搜索才能完成每个步骤,所以我将详细说明他的步骤:

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. 编写一个 shell 脚本,用必要的参数调用你的 java 程序.示例:

  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

用冒号 (:) 而不是分号 (;) 分隔必要的类路径你的程序的路径应该从你的包开始(在java程序的顶部找到这个)

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)

确保类路径参数指向您需要的 jar.您可以检查 java 程序中的导入语句,以确保指定了所有必需的类路径.您必须从 java 目录运行此脚本,并且可以使用单个句点 (.) 作为您的第一个类路径参数.

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.

确保 shell 脚本具有必要的 unix 权限.

Make sure that the shell script has necessary unix permissions.

从终端运行:sudo chmod ### yourScript.sh

其中### 是代表系统设置正确权限的数字.

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

通过设置 cron 作业来安排要调用的脚本.

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

从终端运行:crontab -e

这将打开您的 crontab 编辑器.您可以通过以下方式添加作业:

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

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

将脚本路径替换为脚本的正确位置.此作业设置为每 5 分钟运行一次.请参阅 http://www.adminschoice.com/crontab-quick-reference/crontab 上的一个很好的参考.

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!

这篇关于Java 程序的 Cron 作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆