Skip to content

Commit

Permalink
applied a patch from devmail@hispeed.ch:
Browse files Browse the repository at this point in the history
---
Hi Kohsuke,

Akuma, your "Java process daemonizer" is good, but it annoyed me that there 
was no way for me to decide the name and location of the PID file.

I have attached a patch which implements a straightforward way of achieving 
this functionality, while still preserving the old API and semantics.

Feel free to apply the patch as you see fit. I hope you find it useful, and 
that functionality like this finds its way into the next version.

Regards,
Olav Reinert



git-svn-id: https://akuma.dev.java.net/svn/akuma/trunk/akuma@47 f4debed6-9561-0410-b0a4-b640bb5a59ad
  • Loading branch information
kohsuke committed Dec 4, 2009
1 parent 18cf6bf commit e1f8857
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/main/java/com/sun/akuma/Daemon.java
Expand Up @@ -116,9 +116,19 @@ public static void selfExec(JavaVMArguments args) {

/**
* Prepares the current process to act as a daemon.
* The daemon's PID is written to the file <code>/var/run/daemon.pid</code>.
*/
@SuppressWarnings({"OctalInteger"})
public void init() throws Exception {
init("/var/run/daemon.pid");
}

/**
* Prepares the current process to act as a daemon.
* @param pidFile the filename to which the daemon's PID is written;
* or, <code>null</code> to skip writing a PID file.
*/
@SuppressWarnings({"OctalInteger"})
public void init(String pidFile) throws Exception {
// start a new process session
LIBC.setsid();

Expand All @@ -128,7 +138,7 @@ public void init() throws Exception {
LIBC.umask(0027);

chdirToRoot();
writePidFile();
if (pidFile != null) writePidFile(pidFile);
}

/**
Expand Down Expand Up @@ -158,11 +168,12 @@ protected void chdirToRoot() {
}

/**
* Writes out a PID file.
* Writes out the PID of the current process to the specified file.
* @param pidFile the filename to write the PID to.
*/
protected void writePidFile() throws IOException {
protected void writePidFile(String pidFile) throws IOException {
try {
FileWriter fw = new FileWriter("/var/run/daemon.pid");
FileWriter fw = new FileWriter(pidFile);
fw.write(String.valueOf(LIBC.getpid()));
fw.close();
} catch (IOException e) {
Expand Down

0 comments on commit e1f8857

Please sign in to comment.