Reputation: 695
My main field is .Net but recently I have got something to do with Java. I have to create a shell utility in Java that could run in background reading few database records after specified duration and do further processing. It's a kind of scheduler. Now I have few concerns:
How to make this work as a service. I want to execute it through a shell script and the utility should start running. Off course the control should get back to the calling script.
Secondly, eventually i may want to stop this process from running. How to achieve this?
I understand these are basic question but I really have no idea where to begin and what options are best for me.
Any help / advise please?
Upvotes: 8
Views: 26070
Reputation: 1580
As I understand, you want something like this:
if ( System.in.avaliable() > 0 ) {
in = new BufferedReader( new InputStreamReader( System.in );
String InLine = in.readLine();
...
}
Am I right?
Upvotes: 0
Reputation: 195039
I assume that you are playing your java program with a Linux/Unix box. To run your application as a daemon, you can try
nohup java YourJavaClass &
To stop your application, you can either:
kill [psIdofYourApplication]
or
fg [your application job Id]
Ctrl-C
If you want to do some postprocessing after the application receiving 'kill/stop' signal. check out addShutdownHook(Thread hook)
Or sun.misc.SignalHandler
Upvotes: 3
Reputation: 1889
I would go for the running the program using a scheduler or a service. However, if you wish to use a bat file and do this programmatically, I have outlined a possible approach below:
In your Java program, you can get the PID programmatically, and then write it to a file:
public static void writePID(String fileLocation) throws IOException
{
// Use the engine management bean in java to find out the pid
// and to write to a file
if (fileLocation.length() == 0)
{
fileLocation = DEFAULT_PID_FILE;
}
String pid = ManagementFactory.getRuntimeMXBean().getName();
if (pid.indexOf("@") != -1)
{
pid = pid.substring(0, pid.indexOf("@"));
}
BufferedWriter writer = new BufferedWriter(new FileWriter(fileLocation));
writer.write(pid);
writer.newLine();
writer.flush();
writer.close();
}
You can then write a stop .bat file that will kill the running program in windows. You could do something like:
setlocal
IF EXIST app.pid FOR /F %%i in ('type app.pid') do TASKKILL /F /PID %%i
IF EXIST app.pid DEL app.pid
endlocal
Of course, app.pid is the file written by the Java method above.
I am not sure how you would be able to write a script that launches a java program, and reverts control on termination. I would be interested to see if anybody has a solution for that.
Upvotes: 3
Reputation: 3593
You didn't specify the platform. If on Windows you should look into integrating with the Service Control to create a Windows service. http://en.wikipedia.org/wiki/Windows_service. Once you've implemented the service hooks, it is possible to start and stop the service through the service control GUI or using net stop MyService
syntax from the command line.
Upvotes: 1
Reputation: 597076
ps ux
see pid
kill pid
Or you'd better provide a stopping script that signals the application, which does System.exit(0)
Upvotes: 1