Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
Java
while(true) {
server = new SERVER();
}



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
Posted
Updated 14-May-18 1:20am

Quote:
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.


Quote:
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.
Non blocking user input is achieved using multiple threads in your application. See, for instance: Java Multithreading[^].


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

You could run your server as Linux service at startup. There are many tutorials, available on the web shouwing how to do that.
 
Share this answer
 
Comments
Member 13826016 14-May-18 7:43am    
tnx for reply.
but this is not work?!!
public SERVER() throws Exception {
scanner = new Scanner(System.in);
Thread t = new Thread(new Runnable() {
public void run() {
while(true) {
if(scanner.hasNextLine())
act = scanner.nextLine().toLowerCase();
}
}
});
t.start();

while(true) {
if(act.equals("start"))
MainClass();
else if (act.equals("start"))
print("stop");
}
Member 13826016 14-May-18 7:49am    
you said :"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."
can you show in example codes?
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900