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

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

确保classpath参数指向所需的jars。
您可以在java程序中检查import语句,以确保您指定了所有必需的类路径。您必须从您的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.

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

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

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

调度要调用的脚本

从终端运行: 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.

希望这能帮助别人!

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

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