在Linux服务器上运行用java编写的计划任务
我需要每天在特定时间在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 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.
您可以编写一个 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.
通常在J2EE应用程序中配置 Quartz Schedular
Generally in J2EE app Quartz Schedular is configured
我会在 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
Linux 有一些服务包装器,但通常最简单的就是
There are service wrappers for linux, but its often its simplest to just
可以从 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.