简体   繁体   English

从.jar文件触发特定类别的cron作业?

[英]Triggering specific class from .jar file for cron job?

I am running an AWS EC2 Linux instance, on which I uploaded a .jar file of a piece of software that I wrote. 我正在运行一个AWS EC2 Linux实例,在该实例上上传了我编写的软件的.jar文件。 In order to trigger the app periodically, I want to use cron - and setup crontab in the following way: 为了定期触发应用程序,我想使用cron-并通过以下方式设置crontab:

23 12 * * * java -jar  /home/ec2-user/Cube-Core/Cube.jar > /home/ec2-user/CubeCore/CronLogfiles/test.log 2>&1

The paths all match, and I redirected the sysout and syserr from the software to a testlog file. 所有路径都匹配,我将sysout和syserr从软件重定向到一个testlog文件。

However: when cron does the job, what does he trigger within the jar itself? 但是,当cron完成这项工作时,他在罐子本身中会触发什么? is he looking for a class called 'main' (that is just my assumption, since I am new to cron). 他是否正在寻找一个名为“ main”的课程(这只是我的假设,因为我是cron的新手)。 If this is the case, how can I tell him explicitly which class to trigger, since my project's main class is called differently? 如果是这种情况,由于我的项目的主类调用方式不同,我该如何明确告诉他要触发哪个类?

Also, I read only that some guys prefer to trigger a bash script with cron, and have the script itself trigger the .jar execution. 另外,我只读到有些人喜欢用cron触发bash脚本,并且让脚本本身触发.jar执行。 I, on other hand, am telling crontab directly to execute the .jar. 另一方面,我正在直接告诉crontab执行.jar。 Which of these 2 ways is the 'cleanest' and why, or is it in the end just a matter of not overclutting the crontab entries? 这两种方式中的哪一种是“最干净的”,为什么?或者最后只是不过度复制crontab条目?

Thank you all , 谢谢你们 ,

Cheers 干杯

The Java tutorials indicate you need to define the entry point in the manifest: Java教程表明您需要在清单中定义入口点:

Main-Class: classname

Depending on what you use to create your jar, there may be some configuration convenience to do that. 根据用于创建jar的内容,可能会有一些配置上的便利。

Good for you: you don't have to do anything special about the cron job. 对您有好处:您不必对cron工作做任何特别的事情。 Not so good for you: it's done upon creating the jar, so if it's not there, that's quite a hassle. 不太适合您:创建罐子时就完成了,所以如果不在罐子里,那就太麻烦了。

But tbh, I'd suppose that if the jar works normally, there shouldn't be anything extra to do. 但是tbh,我想如果罐子可以正常工作,那么就不需要做任何其他事情了。

1. Jar with single Main class 1.单主班罐

java -jar myjar.jar` 

It will get the main class details from your meta file within your jar, 它将从jar中的元文件中获取主要类的详细信息,

Main-class : com.yourpackage.Mainclass

2. Jar with more than one main class If you have more than one main class in your single jar then you can go with the below format, 2.具有多个主类的 jar如果单个jar中有多个主类,则可以采用以下格式,

java -cp myjar.jar MyClass 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM