Reputation: 793
I'm going to implement a small daemon application in Java. Below is my requirement. Could someone please give me some suggestion on how to do it?
For Windows, I can call several Win32 APIs to start Process and monitor its status. The daemon app could be a Windows service whose life-cycle is managed by Windows automatically.
Question is, how to do it against Linux. Furthermore, how to write one set of code to deal with both OSs rather than two?
Upvotes: 2
Views: 137
Reputation: 692
Since Java is platform independent, the whole idea of doing this in Java should be around avoiding platform-specific calls. Hence, please forget about Win32 APIs and the entire Windows-vs-Linux part of your question.
That said, what you are looking for is java.lang.Process. A Process is what Java uses to manage, well, another process. It doesn't matter what that other process is (Java or native, or Python, or...).
As stated on the Javadoc page for Process, you can use ProcessBuilder or Runtime to start a new process. You can then use Process.waitFor() to get notified when the other process terminates. To actually use that successfully, you may need to dive into Java's concepts of wait(), notify(), and InterruptedException. For this, I can recommend Bruce Eckel's "Thinking In Java", available for download here. In there, chapter 13 should help.
If you want to know more about the state of your monitored process, you would need to establish some additional means of communication between your daemon and the monitored process. This could for instance be a TCP socket. But that surely belongs into a different question.
Upvotes: 1