hi !
don't know if it's efficient, but look at this

[pascal]
unit Daemon;

{************************************************* ************************************************}
{ }
{ Borland Community Chatbot }
{ Copyright (c) 2002 Dave Nottage }
{ }
{ This program is free software; you can redistribute it and/or modify it under the terms of the }
{ GNU General Public License as published by the Free Software Foundation; either version 2 of }
{ the License, or (at your option) any later version. }
{ }
{ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; }
{ without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. }
{ See the GNU General Public License for more details. }
{ }
{ You should have received a copy of the GNU General Public License along with this program; }
{ if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, }
{ MA 02111-1307 USA }
{ }
{ Original author: Dave Nottage }
{ Email: davidn@smartchat.net.au }
{ Snail mail: Borland Community Chatbot, 1 Victoria St, MILE END, SA, 5031, Australia }
{ }
{************************************************* ************************************************}

{************************************************* *********}
{ Borland Chat Daemonizer }
{************************************************* *********}

interface

type
TDaemonApplication = class(TObject)
private
FTerminated: Boolean;
FPID: pid_t;
FStdOutput: string;
FErrOutput: string;
FDetach: Boolean;
procedure InstallSignalHandlers;
procedure PerformDaemonMagic;
protected
function Daemonize: Boolean; virtual;
procedure DoSigHup; virtual;
procedure DoSigTerm; virtual;
procedure DoSigQuit; virtual;
public
constructor Create;
procedure Run;
property Detach: Boolean read FDetach write FDetach; // determines whether to fork or not.
property ErrorOutput: string read FErrOutput write FErrOutput; // change to setter method so it is dynamic
property PID: pid_t read FPID;
property StandardOutput: string read FStdOutput write FStdOutput;
property Terminated: boolean read FTerminated;
end;

var
Application: TDaemonApplication = nil;

implementation

uses
Libc;

{ handle SIGHUP & SIGTERM }
procedure DoSig(sig: longint); cdecl;
begin
case sig of
SIGHUP:
// Dispatch SIGHUP
Application.DoSigHup;
SIGTERM:
// Dispatch SIGTERM
Application.DoSigTerm;
SIGQUIT:
// Dispatch SIGQUIT
Application.DoSigQuit;
end;
end;

constructor TDaemonApplication.Create;
begin
inherited Create;
FStdOutput := '/dev/null'; // change to a constant (non-localised)
FErrOutput := '/dev/null';
FTerminated := False;
FDetach := True;
end;

procedure TDaemonApplication.PerformDaemonMagic;
begin
setsid()
// http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
Close(Input);
AssignFile(Output, FStdOutput);
Rewrite(Output);
AssignFile(ErrOutput, FErrOutput);
Rewrite(ErrOutput);
end;

function TDaemonApplication.Daemonize: Boolean;
begin
{ daemonize }
if FDetach then
begin
FPID := fork();
case FPID of
0:
begin
PerformDaemonMagic;
__chdir('/');
end;
-1:
// Forking error
else
// Halt;
end;
end
else
Result := True;
end;

procedure TDaemonApplication.InstallSignalHandlers;
var
SignalAction: TSigAction;
SignalSet: TSigSet;
begin
{ block all signals except -HUP & -TERM }

sigfillset(SignalSet);

sigdelset(SignalSet, SIGHUP);
sigdelset(SignalSet, SIGTERM);

pthread_sigmask(SIG_BLOCK, @SignalSet, nil);

{ setup the signal handlers }
FillChar(SignalAction, SizeOf(SignalAction), 0);
SignalAction.__sigaction_handler := @DoSig;
sigaction(SIGTERM, @SignalAction, nil);

FillChar(SignalAction, SizeOf(SignalAction), 0);
SignalAction.__sigaction_handler := @DoSig;
sigaction(SIGHUP, @SignalAction, nil);

end;

procedure TDaemonApplication.Run;
begin
if Daemonize then
begin
InstallSignalHandlers;
while not FTerminated do
begin
// Here we will allow whatever hooks into the daemon application, to
// do their thing.
// RunDataModules;
end;
end;
end;

procedure TDaemonApplication.DoSigTerm;
begin
FTerminated := True;
end;

procedure TDaemonApplication.DoSigHup;
begin

end;

procedure TDaemonApplication.DoSigQuit;
begin

end;

initialization
Application := TDaemonApplication.Create;

finalization
Application.Free;

end.
[/pascal]