public Status forkish() throws IOException { if (isDaemon()) { posix.setsid(); OutputStream old_out = System.out; OutputStream old_err = System.err; System.setOut(new PrintStream(new FileOutputStream(out, true))); System.setErr(new PrintStream(new FileOutputStream(err, true))); old_err.close(); old_out.close(); if (pidfile != null) { FileOutputStream p_out = new FileOutputStream(pidfile); p_out.write(String.valueOf(posix.getpid()).getBytes()); p_out.close(); } return Status.child(posix.getpid()); } else { String[] envp = getEnv(Daemon.class.getName() + "=daemon"); List<String> argv = buildARGV(posix); jnr.ffi.Runtime runtime = jnr.ffi.Runtime.getSystemRuntime(); Pointer NULL = Pointer.wrap(runtime, 0L); IntByReference child_pid = new IntByReference(); int rs = posix.posix_spawnp( child_pid, argv.get(0), NULL, NULL, argv.toArray(new String[argv.size()]), envp); if (rs != 0) { throw new RuntimeException(posix.strerror(rs)); } return Status.parent(child_pid.getValue()); } }