在Linux服务器上运行用java编写的计划任务

发布于 2024-10-14 23:54:11 字数 347 浏览 2 评论 0原文

我需要每天在特定时间在Linux服务器上运行一段java代码。

目前,我正在 Ubuntu 桌面上通过从终端运行它来测试它,并根据需要正常运行。对于调度,我使用了此处中的库。

在实际服务器上,将其作为进程运行的正确方法是什么?

我的意思是,在我的桌面上,我从终端运行代码,指定接近当前时间的时间,第一次测试它,然后停止它(^Z)。

在服务器上,将其推到后台是正确的方法吗?我确信一定有更好的方法来实现这一目标。

谢谢

I need to run a piece of java code on a linux server everyday at a specific time.

For now I am testing it on a Ubuntu desktop by running it from the terminal and its functioning properly and as required. For scheduling, I have used the library from here.

On the actual server, what would be the correct way to run this as a process ?

I mean, on my desktop, i run the code from terminal specifying a time close to current one, test it for the first time and then stop it (^Z).

On the server would it be a right way to just push it to the background ? I am sure there must be a better way of achieving this.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

把回忆走一遍 2024-10-21 23:54:11

cron 运行它,就像(大部分)从命令行运行一样。将其包装在设置适当环境变量的 shell 脚本中可能是最简单的方法。

Run it from cron just like you would (mostly) from the command line. It's probably easiest to wrap it in a shell script that sets the appropriate environmental variables.

十雾 2024-10-21 23:54:11

您可以编写一个 shell 脚本来执行 Java 实用程序(您可能需要添加类路径和其他环境变量)并将其放入 crontab 中。

这是快速crontab 参考

You can write a shell script for executing your Java utility (you might need to add classpath and other environmental variables) and put it in your crontab.

Here is quick crontab reference.

诗化ㄋ丶相逢 2024-10-21 23:54:11

通常在J2EE应用程序中配置 Quartz Schedular

Quartz 是一种功能齐全的开源作业调度服务,可以与几乎任何 Java EE 或 Java SE 应用程序集成或一起使用 - 从最小的独立应用程序到最大的电子商务系统。 Quartz 可以用来创建简单或复杂的调度来执行数十、数百甚至数万个作业;其任务被定义为标准 Java 组件的作业,这些组件实际上可以执行您可以对其进行编程的任何操作。 Quartz Scheduler 包含许多企业级功能,例如 JTA 事务和集群。

Generally in J2EE app Quartz Schedular is configured

Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.

臻嫒无言 2024-10-21 23:54:11

我会在 Linux 机器上使用 crontab 来触发我的进程。 java 内部的调度也有效。但是使用 crontab 你不会一直运行 java 进程,而只是为了你的任务

i'd use crontab on a linux machine to trigger my process. Scheduling inside java works, too. but with crontab you won't have a java process running all the time, but only for your task

淑女气质 2024-10-21 23:54:11

Linux 有一些服务包装器,但通常最简单的就是

nohup java {etc} 2>&1 > my.log & 

There are service wrappers for linux, but its often its simplest to just

nohup java {etc} 2>&1 > my.log & 
烟若柳尘 2024-10-21 23:54:11

可以从 cron(8) 启动它。当然,这假设您的任务在某个时间终止。如果您想在系统启动时启动它并让它运行,那么,将启动脚本放入 /etc/init.d/ 中,并链接到正确的运行级别启动目录,并在内部使用 Quartz 等库来安排其工作。

Just can start it from cron(8). This of course assumes that your task terminates at some time. If you want to start it at system start time and having it running, then, put a start script into /etc/init.d/ with a link to the right runlevel start directory and internally use a library like Quartz to have its work scheduled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文