Java app as a liunx daemon - getting arguments - java

I'm trying to make an web service which can also get commands via shell. So I want to make this app work like a daemon. So my question is how can I get arguments passed to shell while web service runs in the background? I can make script which starts this app as a daemon and give it a name but how can i get arguments in java passed later on?
EX. mydaemon add -stream somepage.com -channel channelName -start 2014-09-19 20:00:00 -end 2014-09-19 20:45:00
I can start a new thread in main method which gets system.in but will this get stuff sent to PID of my app. Btw, if this can make a change - I'm using Spring Boot in my app.

Related

Programmatically restart a Java application that can be run as a windows service

I'm trying to get my Java application to be able to restart itself programmatically.
I already have a working solution for when it's executed as a standalone application - the application invokes java.exe with its own classpath, jvm parameters, class name, and command line arguments; and then it exits (see my stack overflow answer).
But this approach doesn't work cleanly when the application is run as a windows service - instead of a restarted service, I end up with a java.exe process executed by the SYSTEM account, and the state of the service is stopped (since it exited). So the application does restart, but is no longer a service.
I know that windows restarts services that exit with a non-zero exit code but a) I'm not sure that it always works (see this question) b) I'm not sure how to identify the fact that the application is running as a windows service in the first place - System.getProperty("user.name") just seemed to return the hostname followed by a dollar sign ($) in my tests.
Can anyone suggest a solution?
The Launch4j package has a <restartOnCrash> parameter for services.

print word document (.docx) from a java application running as windows service

i have written a small java application which is called by an java application running as a windows service.
My java application calls the print method in order to print the word document:
java.awt.Desktop.getDesktop().print(new File(targetfile));
It works if i call the java application but it does not work if it is executed by the windows service.
What happens is that i have multiple ms word processes running in the background each time the window service tries to execute my java application.
Do you know how i can print from a windows service ?
Thanks,
Asad
If I had to guess... you need to run the Windows Service as a user with appropriate permissions to print.
By default a service will run as System or LocalUser, which will not be able to print. That's why it works when you call it but not the service. Try and set the Log On account for the service to your personal account and see if it works.

Custom Linux service status message using Java Service Wrapper

I'm building a standalone application to run as a Linux (Ubuntu) daemon service, using Java. For that, I'm using the Java Service Wrapper lib.
During the execution of the service I'd like to be able to query for some status of the tasks executed by the application. In other words, I'd like to be able to print a custom message when I type on terminal:
service my-app status
Currently the message it prints is something like this:
My App is running: PID:1000, Wrapper:STARTED, Java:STARTED
I understand I need to use a more complex integration method (rather than the simplest one), but I couldn't find how to intercept the "status" call to print my own message.
How can I customize that message, using the Java Service Wrapper?
I got a half-solution. Inspecting the wrapper script code, as suggested by #Naytzyrhc, I found that the wrapper lib reads 3 files to create the status message:
bin/my-app.pid to print the PID of the running process;
bin/my-app.status to print the status of the wrapper itself;
bin/my-app.java.status to print the status of the wrapped application.
So, in the application code, to override the status message, just write the message in the my-app.java.status file.
There's only one gotcha: if the status message contains line breaks, the service my-app status doesn't print them, because it uses the echo command (as stated in this question Capturing multiple line output into a Bash variable). To solve this problem, just change the line from:
eval echo `gettext '$APP_LONG_NAME is running: PID:$pid, Wrapper:$STATUS, Java:$JAVASTATUS'`
to:
eval echo `gettext '$APP_LONG_NAME is running: PID:$pid, Wrapper:$STATUS, Java:"$JAVASTATUS"'`
(Using double quotes on $JAVASTATUS).
This is a half-solution because it doesn't fire an event to the running application, as I wanted. But it works for customizing the status message: it depends on the application how often the message is updated.

How to run external application from a Java EE web application?

I have been working on a Web Application for Intranet use only. I work with Apache 7 and Windows Server 2003.
In one of my page, I need to open an external application that we can locate in C:/Program Files/etc... with some parameters already initialized. Of course, the user has to use the web application on the server to make it work (that will be the case).
To open the application, I use the ProcessBuilder object.
The problem:
When I work locally with Eclipse and run the server by hand, it works perfectly. Any application can open itself from a web page.
But when I use the tomcat windows service (and that's what we want to use on the server), it just never launches. Or to be more specific, it launches and stops the application directly. No java errors thrown and process.waitFor() with an exit value of 0. The fact is that we can run processes via the web application (I tried to run simple batch files), but when there is an UI involved, it will never appear.
Again, on the server this time, if I launch tomcat7.exe (that we can find in the %CATALINA_HOME%/bin directory) with a double click or cmd, the UI in the web application will appear. If I launch it with services.msc or tomcat7w.exe or tomcat7 start via cmd, it will not.
I thought of several things:
use another user to start the service
change the way the service is launched (StartMode: jvm, java. I did not succeed with exe)
I read Tomcat 7 Windows Service How-To many times but didn't find out anything to solve my issue.
Have you any idea of what is happening, and how to solve this issue ?
What is the big difference between running tomcat as a service and from the command line?
Option 1
If you open your service's properties window, go to the Log On tab then check the "Allow service to interact with desktop" check box you will get the behavior you want. Also depending on what app you what to run you may need to change the log on account.
see Launching GUI App from Windows Service - Window Does Not Appear
Option 2
Did you try start a cmd and there use
start /c "c:\path to\exe"
in cmd.exe type
help start
Option 3
You will need a daemon service that is not run as a service. windows puts certain restrictions on service apps.
This sleeping app can be started by tomcat or other your self. it can listen on a port or poll a folder for a new file, and when it gets a job to do it starts the app you want. Via port or text file you can send the parameters.

Trigger a Java program on startup of some windows service or program

I want my Java program or say some method within my class to be triggered when i start some Windows program(Application) .For example i want my program start executing(Trigger) when i start Realplayer.
Here is a psuedo-code of what you have to do:
Java application (Listener), registered as a Windows Service and runs on system stratup.
Listener application keep listening until some .exe file is opened.
Listener execute code (or call another application)
I think you need to make a jar and then use some sort of Java Service Wrapper.... Still not sure.
What you actually require is to register a service in the Windows services.
You can easily do this with DOS...
#echo off
start path\to\someApplication
start path\to\javaApplication
Enter the above code in notepad and save it as whateverNameYouWant.bat
When you run it, the batch file will launch the application you specified in the code AND the Java (.jar) application you specified in the code.

Categories