Linux run java in background

java background/daemon/service cross platform best practices

I understand that my requirements may be unrealistic but I am hoping there is some sort of «best practice» for this type of situation.

Ideally, the program would mainly be running as a daemon/service. and clicking on its icon in the status bar would show a window to change settings,stop the daemon/service, etc. Currently, the app is only a swing desktop app.

7 Answers 7

You can run a Java application as a service (Windows) or daemon (Linux) using the Apache Commons daemon code.

Structure

Daemon is made of 2 parts. One written in C that makes the interface to the operating system and the other in Java that provides the Daemon API.

Platforms

Both Win32 and UNIX like platforms are supported. For Win32 platforms use procrun. For UNIX like platforms use jsvc.

Java code

You have to write a Class (MyClass) that implements the following methods:

* void load(String[] arguments): Here open the configuration files, create the trace file, create the ServerSockets, the Threads * void start(): Start the Thread, accept incoming connections * void stop(): Inform the Thread to live the run(), close the ServerSockets * void destroy(): Destroy any object created in init() 

You can turn any Java program into a service/daemon using the Java Service Wrapper. It is used by multiple OSS projects, and ships as part of the Nexus Maven Repository Manager so that it can be installed as a service out of the box. To use it, you, the author, just need to create a configuration file and then run a simple batch file to create the service on Windows or copy an init script to the correct runlevel on Linux.

You can use the SystemTray classes and install your app as any other in the default platform.

For windows it could be an scheduled task that run at startup. For Linux and OSX I don’t know (besides crontab wich is somehow too technical) but I’m pretty sure they both have a way to do the same thing easily.

Unfortunately (as of today) Apple hasn’t finished the 1.6 port.

It won’t be a real demon, but an app like Google Desktop.

I’ve heard Quartz is a good option. But I’ve never used it.

If you dont need free solution, you can use Advanced Installer (www.advancedinstaller.com), it can make win-service as well as MacOS installer from your JAR, and more..

Check out JDIC, the Java Desktop Integration Components project. It supports desktop integration like system tray (or equivalent) with a cross-platform API.

Читайте также:  Что за собака linux

Others have mentioned Quartz, which is an enterprise job scheduler. It can be lightweight, depending on the jobs that are scheduled, but it doesn’t have any features that are inherently desktop-oriented. On the contrary, many of its features depend on enterprise support like a relational database. If your application is primarily scheduling tasks, a headless Quartz service executing jobs, with a desktop client to interact with the service is reasonable approach.

The Apache Directory Daemon project sounds like the best cross platform way to do this (with Java wrappers for JSVC under POSIX and procrun under windows).

People sometimes have difficulties finding prunsrv.exe and/or prunmgr.exe (components of procrun), its not well documented on the apache site, generally it can be found in the archives (note. they say that procrun is tomcat5.exe)

you dont need to implement daemon interface nor download it at all, prunsrv can be used to turn any app to windows service

browse that archive for other platforms

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Run Java console app as a daemon (background)

I’ve developed a Java console application that when start, open a console window and remain in foreground, i want to start that application in background . Now i launch the application by this command line :

Is there a way to achieve this behaviour ? It’s enough change the command line parameter or i need to do some change on my code ?

your question is not clear. Do you want to run as a Windows service? Or do you want a console windows? or both?

I’m on Windows and as a windows service could be a great solution . Is there a way to run a Java console application as a Windows ServicE ?

4 Answers 4

The answer is operating system dependent.

*nix: & Windows: (opens a new console): start Windows: (doesn't open a new console): start /b

If you are doing this in anything unix based then you can append & to the end which will spawn a new thread and keept it running in the background.

If you really just want it to run in the background, java -jar myapp.jar & will do the job. That way, it’ll still die when the shell closes, but you can keep using your shell.

If you really want it run as a daemon, nohup java -jar myapp.jar & will do the job. That way, it’ll continue to live when the shell closes.

Читайте также:  Bad sound in linux

If you want this to be reliable, you can prepare an init script or upstart job definition, or run it via Vixie cron(8) @reboot specifier to make it start at boot.

Given that you’re using Windows, you might consider Java Service Wrapper. I have used it on a project in the past.

Additionally you can do «javaw» instead of «java» which will run your program without a console window. This is typically how a GUI based java program should be run. download.oracle.com/javase/6/docs/technotes/tools/windows/…

Источник

Run java jar file on a server as background process

I need to run a java jar in server in order to communicate between two applications. I have written two shell scripts to run it, but once I start up that script I can’t shut down / terminate the process. If I press ctrl + C or close the console, the server will shut down. Could anyone help me how to modify this script to run as a normal server?

 #!/bin/sh java -jar /web/server.jar echo $! #> startupApp.pid 

4 Answers 4

#!/bin/sh nohup java -jar /web/server.jar & 

The & symbol, switches the program to run in the background.

The nohup utility makes the command passed as an argument run in the background even after you log out.

Thanks Anton,currntly im stopping the server by killing the process id.i don’t think it’s best practice.is thery command to to stop the server?

Short answer: it depends on the server. Long answer: as far as I know, there is no safe way to shut down a process without the process supporting such graceful shut downs. For example, if it’s a web server being terminated by an external signal, there is always a possibility that some requests will be lost. One way to solve this problem is to implement a graceful termination function in the server itself, e.g. by processing a special kind of requests. Then, the server can be terminated by sending it a request of that special kind. Otherwise, killing the process by its ID is the simple way.

Using nohup should always be combined with redirecting stdout and stderr explicitly — otherwise, you don’t get control of where the logs go, and end up with an ugly nohup.out created in whichever directory this script happens to be invoked from.

@Anton when I run a script(just like your answer) with SSH EXEC runjar.sh. It started streaming the logs in my local terminal and if close my terminal my jar gets killed. How can I run a jar in the background using SSH EXEC?

Источник

Launch springboot project in the background on Linux

We know there are three ways to start a springboot project:

  • Run Main Method Start
  • Start the application on the command line using the command mvn spring-boot:run
  • When you run «mvn package» for packaging, it is packaged into a JAR file that can be run directly and run directly using the «java-jar» command.
Читайте также:  Как определить мой адрес linux

We typically use the first two when developing and the third when deploying, but we don’t run in the background when using java-jar.

Let’s talk about how to get the springboot project started in the background on the server. In fact, there are many ways to do this, and here are two more useful ways:

nohup and Shell

This method is mainly implemented by using the nohup command, which is described in detail as follows:

Purpose: Run commands without interruption.

Description: The nohup command runs commands specified by Command parameters and any associated Arg parameters, ignoring all SIGHUP signals. Use the nohup command to run programs in the background after logoff. To run the nohup command in the background, add &to the end of the command.

When this is done, nohup outputs the logs from the execution results to the nohup.out file in the current folder, usually using the above command.
We can also manually specify a parameter to specify where the log file will be output, such as:

nohup java -jar xxx.jar > catalina.out 2>&1 &

If you do not need to output a log, you can use the following command

nohup java -jar xxx.jar >/dev/null &

So we just need to use the nohup Java -jar yourapp.jar &command to get yourapp.jar running in the background. However, for easy administration, we can also write some scripts to start the application through Shell, such as the following:

#!/bin/bash PID=$(ps -ef | grep yourapp.jar | grep -v grep | awk '< print $2 >') if [ -z "$PID" ] then echo Application is already stopped else echo kill $PID kill $PID fi
#!/bin/bash // Start via port 8888 nohup java -jar yourapp.jar --server.port=8888 & #!/bin/bash // Log output to yucoal.log with prod environment configuration parameters nohup java -jar yourapp.jar --spring.profiles.active=prod >/httx/logs/yuapp.log 2>&1 &
#!/bin/bash echo stop application source stop.sh echo start application source start.sh

system service

   org.springframework.boot spring-boot-maven-plugin true    
sudo ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp

After the soft connection is created, you can control the start, stop, and restart operations by applying the following commands to yourapp.jar

/etc/init.d/yourapp start|stop|restart

In summary, jar packages can generally be started in the background using the following commands:

Normally, we configure the log file ourselves when writing a java project. We don’t need to output the default nohup.out log in a production environment, so we can start the jar package with the following command

nohup java -jar xxxx.jar >/dev/null 2>&1 &

Hot Topics

  • Java — 6403
  • Python — 2524
  • Javascript — 1868
  • Database — 1639
  • Linux — 1537
  • Back-end — 1512
  • Spring — 1429
  • Algorithm — 1406
  • Front-end — 1257
  • Android — 1140
  • C++ — 1108
  • MySQL — 1045
  • Programming — 1042
  • network — 904
  • data structure — 866
  • Attribute — 865
  • C — 742
  • xml — 735
  • less — 697
  • github — 694

Источник

Оцените статью
Adblock
detector