如何在linux服务器上编写一个运行程序 [英] How to write a program for run always in linux server

查看:78
本文介绍了如何在linux服务器上编写一个运行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写一个程序,总是在linux服务器上运行?(例如Web爬虫服务器)我有两个问题要做。 1.总是运行程序的最佳方法是什么?我使用:

how to write a program for run always in linux server?(for example Web crawler server) I have two problems for do it. 1.what is best method for run always my program? i use :

while(true) {
server = new SERVER();
}







这种方式是真是假?



2.如何我的服务器总是听取用户输入而没有暂停1号循环,例如使用扫描仪输入用户,但这种方法暂停程序。



在最后,我想写一个安装在linux服务器上的程序,自动化工作不停。



我尝试过:



i试了一下我的主要课程




is this way is true or false?

2.how my server always listen to user input without pausing loop in No.1 for example use scanner for input user but this method pausing program.

in final , i want to write a program that install on linux server and automation works without stop.

What I have tried:

i tried put my main class in a while

推荐答案

引用:

while(true){

server = new SERVER();

}

while(true) {
server = new SERVER();
}

这很可能是一个错误:你的程序在无限循环中不断创建服务器的新实例。

你可能必须创建一个服务器实例,然后执行无限循环。 />




That is very likely a blunder: your program keeps creating new instances of the server in a endless loop.
You probably have to create one instance of the server wich, in turn executes a endless loop.


引用:

2.我的服务器总是如此听取用户的意见t没有在No.1中暂停循环,例如对输入用户使用扫描仪,但这种方法暂停程序。

2.how my server always listen to user input without pausing loop in No.1 for example use scanner for input user but this method pausing program.

使用应用程序中的多个线程实现非阻塞用户输入。例如,请参阅: Java多线程 [ ^ ]。





Non blocking user input is achieved using multiple threads in your application. See, for instance: Java Multithreading[^].


引用:

在最后,我想写一个安装在linux服务器上的程序,自动化工作不停。

in final , i want to write a program that install on linux server and automation works without stop.



您可以在启动时将服务器作为Linux服务运行。有许多教程,可以在网上找到如何做到这一点。


You could run your server as Linux service at startup. There are many tutorials, available on the web shouwing how to do that.


当系统启动或断电时,程序自动启动和停止。在Linux中,它们被称为守护进程并由系统服务管理(主要是 systemd - Wikipedia )控制[ ^ ]如今)。



要停止这样的守护进程,他们必须能够接收停止事件并终止自己。传统上通过处理 SIGTERM POSIX信号( Signal(IPC)来完成) - 维基百科 [ ^ ])。请参阅处理信号和例外 [ ^ ] for Java。



虽然并不总是需要提供这样的处理程序,但建议通过关闭文件和套接字以及终止子线程来执行正确的关闭。



如何使用start-stop-daemon制作Java守护进程 - Leonid Shevtsov [ ^ ]描述了如何启动和停止Java应用程序作为守护进程。



守护进程提供某种进程间沟通 - 维基百科 [ ^ ]可以是也用于处理停止条件。



但是这样的守护进程没有附加到用户shell,因此他们无法从键盘输入。如果应用程序应作为守护程序和普通应用程序执行,则常见的解决方案是使用命令行参数来定义执行状态。这在开发过程中特别有用。



如果应用程序必须处理异步事件,如键盘输入和侦听网络套接字而不阻塞其他执行路径,则必须实现这些在自己的线程中。这适用于所有类型的应用程序。



因此您必须了解上述概念。匹配Web研究关键字可能是例如javal linux daemon和java handle sigterm。处理IO事件和使用线程取决于要处理的异步事件的类型。
Programs started and stopped automatically when systems startup or power down are called services. With Linux they are called daemons and are controlled by the system service management (mainly systemd - Wikipedia[^] nowadays).

To stop such daemons they must be able to receive a stop event and terminate themself. That is done traditionally by handling the SIGTERM POSIX signal (Signal (IPC) - Wikipedia[^]). See Handle Signals and Exceptions[^] for Java.

While it is not always necessary to provide such a handler it is recommended to perform a proper shutdown by closing files and sockets, and terminating child threads.

How to make a Java daemon with start-stop-daemon - Leonid Shevtsov[^] describes how to start and stop a Java application as daemon.

When a daemon provides some kind of Inter-process communication - Wikipedia[^] that can be also used to handle stop conditions.

But such daemons are not attached to a user shell so that they can't get input from the keyboard. If an application should be executed as daemon and as normal application, a common solution is to use a command line argument to define the execution state. This is especially useful during development.

If an application has to handle asynchronous events like keyboard input and listening on network sockets without blocking other execution paths, these have to be implemented in own threads. This applies to all kind of applications.

So you would have to learn about the above concepts. Matching web research keywords might be for example "javal linux daemon" and "java handle sigterm". Handling IO events and using threads depends on the kind of asynchronous events to be handled.


这篇关于如何在linux服务器上编写一个运行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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