如何在 Debian 服务器上连续自动运行 .jar?

2022-01-24 00:00:00 cygwin ssh debian java jar

我有一个 .jar 文件,我想一直在 Debian 服务器上运行.

I have a .jar file I want to run all the time on a Debian server.

目前我已经弄清楚如何通过 Cygwin 上的 ssh 访问服务器并启动 .jar.但是当我关闭开发机器上的 Cygwin 窗口时,它会终止服务器上的进程(我认为,因为它不再响应).

Currently I have figured out how to access the server via ssh on Cygwin and start the .jar. But when I close the Cygwin window on my development machine, it kills the process on the server (I think, as it's not responding any longer).

目前我是这样开始的:

java -jar myjar.jar packageName.fileNameOfFileWithMainMethod

我需要让这个文件在服务器上自动持续运行(它是我正在开发的系统的一个组成部分).

I need to make this file run automatically and continually on the server (it is an integral part of the system I am developing).

不幸的是,我对服务器管理或一般的非 Windows 操作系统(不是我选择或制造服务器)几乎一无所知,所以我真的不知道该做什么,也不知道该搜索什么(显然,因为我的搜索没有给出可用的结果).

Unfortunately I know next to nothing about server management, or non-windows operating systems in general (wasn't me who chose or made the server), so I really don't know what to do, nor what to search for (apparently, since my searching gave no usable results).

我已阅读(并编辑,因为文本很乱)this 问题,但尽管我觉得它可能暗示了正确的方向,但我并没有从中得到太多帮助.我也尝试了我最好的 googlefu,但它给我带来了很多只是切线相关的结果.

I have read (and edited because the text was a mess) this question, but although I feel it might be hinting in the right direction, I didn't get much help from it. I also tried my best googlefu, but it got me a lot of only tangentially related results.

我想我必须编写某种脚本(可能包含上面的代码行),对其进行一些处理并将其放在服务器上的某个特定位置以完成我想做的事情.

I guess I'll have to make some kind of script (possibly containing the code line above), do some stuff to it and put it somewhere specific on the server to accomplish what I want to do.

有人能解释一下这是怎么做到的吗?

Would someone be so kind as to explain how this is done?

推荐答案

一个简单的建议是使用 Linux 的 CRON.

One simple suggestion would be to run the jar file using Linux's CRON.

这篇文章来自 unix stack exchange 应该让您朝着使用 cron 运行 jar 文件的正确方向前进.

This article from unix stack exchange should get you going the correct direction for running a jar file using cron.

或者,这篇文章来自 mkyong.com 也清晰简洁.

例如:

  1. 使用 Cygwin 进行连接
  2. 运行crontab -e
  3. 输入 0 * * * * java -jar myjar.jar packageName.fileNameOfFileWithMainMethod 以在整点每小时运行一次 jar 文件.或者,要在服务器启动时启动一次,请输入 @reboot java -jar myjar.jar packageName.fileNameOfFileWithMainMethod
  1. Connect using Cygwin
  2. Run crontab -e
  3. Enter 0 * * * * java -jar myjar.jar packageName.fileNameOfFileWithMainMethod to run the jar file every hour at the top of the hour. Or, to start it once on server startup enter @reboot java -jar myjar.jar packageName.fileNameOfFileWithMainMethod

相关文章