I have a console application (written in Java), which should run on a Linux machine until it is stopped.
Logging is done by the application itself.
The application needs to be stopped whenever a new version is available (i. e. I login, stop the application, copy the new JAR file, and then launch it again).
What are the options for implementing this, apart from those specified below?
Known ways to do it:
1) Tanuki service wrapper
2) nohup java -jar myapp-1.32.jar &
I use Java Service Wrapper, but you already mentioned it. I think it should suit your needs.
Apache Commons Daemon is also popular: http://commons.apache.org/daemon/, but I never used it.
I'm using this startup script: http://shrubbery.homeip.net/c/display/W/Java+Daemon+Startup+Script
[Changed domain name - by wiki owner]
Some modern Linux distributions have switched to upstart. That's a daeomon starting and stopping all the other services. I'd definitely look into that. Since it solves some tricky problems with production ready start scripts. The downside is that it has no java specific functionality.
There also the apache commons deamons http://commons.apache.org/proper/commons-daemon/jsvc.html
Jsvc is a set of libraries and applications for making Java applications run on UNIX more easily.
Jsvc allows the application (e.g. Tomcat) to perform some privileged operations as root (e.g. bind to a port < 1024), and then switch identity to a non-privileged user.
Related
I have a Java daemon (system service running 24/7/365) that needs to deploy as an executable JAR, however I would like users (on Windows, Linux and Mac alike) to be able to start/stop the application via:
service myapp start
service myapp stop
(Or whatever is the equivalent for Windows/Mac services). Obviously, this requires something at the OS-level to map the myapp "service" to a particular method call from inside my Java app (perhaps, LifecycleManager#start() and LifecycleManager#stop() respectively).
I heard that Apache Commons Daemon can be used for this purpose, and after perusing their site, it looks like it does just this. However it looks like it's an old project and there really isn't any documentation for accomplishing what I am looking for.
So I ask: can commons-daemon do what I need, or do I need something else or in addition? How can I get a cross-platform daemon out of an executable JAR? Thanks in advance!
Yes, Apache Commons Daemon can run your jar as a service on Windows (using procrun) or as a daemon on *NIX (using JSVC). I only used it on Windows, so the rest only applies to procrun:
Procrun supports proper Windows service shutdown (it can invoke a stop method in your running application). It also has a little bit of extra functionality like optionally redirecting your stdout and stderr to separate log files, and running the service wrapper exe directly (assuming you renamed it to your service name) runs a non-service mode console that lets you see the output immediately. You probably want to use procrun's "jvm mode". Read the details of how to do that in the documentation.
If you need additional fancier capabilities, such as if the service needs to be able to restart itself, take a look at YAJSW (Yet Another Java Service Wrapper) instead. YAJSW is quite likely better overall, but I haven't tried it yet.
When starting a application, one can pass parameters to the application. But how can one pass parameters to a already running (Java) application / how can I handle such cases in my Java program?
In other words: How can I communicate from a .bat file / CLI processes with a already running Java application? Note that both things (CLI stuff and Java application) are my own applications and I can adapt the source code - I just don't know how ;-)
I prefer using socket for cross platform IPC, with help of Apache thrift . You can implement RPC method to use from CLI utility.
How I can run Java application at system startup on Windows/Linux/MacOS?
Any implementation of JNA/JNI shall be welcome.
Take a look at http://wrapper.tanukisoftware.org. It provides a wrapper for java applications to run under the various operating systems. You can use the community edition.
You can consider your application as a service under linux, and add a script that could start it under /etc/init.d (on most distributions, I think). You'll then have to make the needed links to the runlevel folders, to decide when to run your application. I think you can use chkconfig too
A basic example of script can be found here
I don't have most clues for the other two systems, however.
On windows you can use reg add
Add the path of your application to hklm/software/microsoft/windows/current version/run
To see more ,on command prompt type reg add /?
Alternatively yo can use system call to add it from your application itself
Take a look at http://yajsw.sourceforge.net/.
It's free and compatible reimplementation of TanukiSoftware Java Service Wrapper featuring free 64-bit support.
There is also a comparison table for YAJSW, JSW, ACD and L4J.
I'd like to log or record every time I start an application to gain insight into which applications I use most on my Windows system. I was thinking I could create an event in the event log and listen for it in a .Net program.
Questions:
Is this the best way to solve this problem?
If so, which .Net library should I use?
I am also open to using Java to solve this problem. Thanks!
In .NET, you could probably create a shell extension which you would register for EXE programs which would really just be a filter for the EXE extension. When your shell extension is called, you would execute the program (by invoking the old functionality) after you logged your information.
Note, however, that you can ONLY do this with .NET 4.0 or above, which is currently in beta. Because of the way that previous versions of the CLR worked, only one version was allowed to run in a process at a time (including explorer, the OS process).
.NET 4.0 introduces Side-by-Side (SxS) CLR instances within the same process, so it is safe to use it from .NET 4.0 on as a mechanism for shell extensions.
It will also require a good deal of COM interop, but it can be done.
In regards to LWoodyiii's comment asking if this can be done in older versions of .NET: Could it? Yes, it can be done, but the official decree from MS is that you shouldn't. The reason for this is because if someone else decides to run a shell extension, or interface with the OS in some way using .NET, and the version is different from the one that you are using, you run the risk of hosing the OS process.
maybe you can try to hook the CreateProcess API in system wide using unmanaged c++.
and in C# use .NET interop to handle events/notifies from you unmanaged hook module.
related links:
http://www.codeproject.com/KB/system/hooksys.aspx?msg=1322916
http://www.madshi.net/madCodeHookDescription.htm
I am looking for the best method to run a Java Application as a *NIX daemon or a Windows Service. I've looked in to the Java Service Wrapper, the Apache Commons project 'jsvc', and the Apache Commons project 'procrun'. So far, the Java Service Wrapper looks like it's the best option... but, I'm wondering if there are any other "Open Source friendly" licensed products out there.
I've had great success with Java Service Wrapper myself. I haven't looked at the others, but the major strengths of ServiceWrapper are:
Great x-platform support - I've used it on Windows and Linux, and found it easy on both
Solid Documentation - The docs are clear and to the point, with great examples
Deep per-platform support - There are some unique features in the window service management system that are supported perfectly by service wrapper (w/o restarting). And on Windows, you will even see your app name in the process list instead of just "java.exe".
Standards Compliant - Unlike many ad-hoc Java init scripts, the scripts for service wrapper tend to be compliant with LSB standards. This can end up being very important if you ever want high availability management from something like Linux Heartbeat/HA.
Anyway, just my 2 cents... :)
Another option is WinRun4J. This is windows only but has some useful features:
32 bit and 64 bit support
API to access the event log and registry
Can register service to be dependent on other services (i.e serviceA and serviceB must startup before serviceC)
Its also open source friendly (CPL) so no restrictions on use.
(full disclosure: I work on this project).
Are there any special attributes that you need to apply (like OS guided resource management) that you need to support? Otherwise, for Unix you should be able to daemonize your application by writing an appropriate init.d script and setting your app to start automatically.