It's an old topic but search function show this as a good place, so for next users
I've attach an working Daemon example as a ZIP file.
It's created visually with using Project -> New Project... -> Daemon (service) application
Created in:
Ubuntu 14.04 LTS with Cinnamon
Typhon IDE 4.8.0 Synchronize with Lazarus Source 29-3-2014 SVN Rev 44546
FreePascal 2.7.1 Source 29-3-2014 SVN Rev 27327
msapTemp.lpr
Program msapTemp;
Uses
{$IFDEF UNIX}
CThreads,
{$ENDIF}
DaemonApp, lazdaemonapp, DaemonMapperUnit1, DaemonUnit1
{ add your units here };
begin
Application.Initialize;
Application.Run;
end.
daemonmapperunit1.pas
unit DaemonMapperUnit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, DaemonApp;
type
TDaemonMapper1 = class(TDaemonMapper)
private
{ private declarations }
public
{ public declarations }
end;
var
DaemonMapper1: TDaemonMapper1;
implementation
procedure RegisterMapper;
begin
RegisterDaemonMapper(TDaemonMapper1)
end;
{$R *.lfm}
initialization
RegisterMapper;
end.
!!! Very IMPORTANT !!! - when you create a new daemon project yourself you have to put your TDaemon class name to DaemonClassName !
You click a DaemonMapperUnit1 tab on Editor, then F12 key [sometimes 2 times].
You have an TDataModule form called DaemonMapper1 on top - click on it.
On the Object Inspector you click DaemonDefs [...] button and then on the new window click Add.
Click on the new 0[zero] position and look on the Object Inspector.
There you have to in the DaemonClassName property put your TDaemon class name - in my example you have TDaemon1.
The ZIP file and sources are working and contain above steps completed.
daemonmapperunit1.lfm
object DaemonMapper1: TDaemonMapper1
DaemonDefs = <
item
DaemonClassName = 'TDaemon1'
Name = 'TDaemon1'
DisplayName = 'TDaemon1'
Options = [doAllowStop, doAllowPause]
WinBindings.Dependencies = <>
WinBindings.StartType = stBoot
WinBindings.WaitHint = 0
WinBindings.IDTag = 0
WinBindings.ServiceType = stWin32
WinBindings.ErrorSeverity = esIgnore
LogStatusReport = False
end>
left = 364
top = 504
end
daemonunit1.pas
unit DaemonUnit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, DaemonApp;
type
{ TDaemon1 }
TDaemon1 = class(TDaemon)
procedure DataModuleExecute(Sender: TCustomDaemon);
procedure DataModuleShutDown(Sender: TCustomDaemon);
procedure DataModuleStart(Sender: TCustomDaemon; var OK: boolean);
procedure DataModuleStop(Sender: TCustomDaemon; var OK: boolean);
private
{ private declarations }
public
{ public declarations }
end;
var
Daemon1: TDaemon1;
Log: TextFile;
implementation
procedure RegisterDaemon;
begin
RegisterDaemonClass(TDaemon1);
end;
{$R *.lfm}
{ TDaemon1 }
procedure TDaemon1.DataModuleExecute(Sender: TCustomDaemon);
begin
while Self.Status = csRunning do
begin
sleep(1000);
WriteLn(Log, datetimetostr(Now));
Flush(Log);
LogMessage(DateTimeToStr(Now));
end;
end;
procedure TDaemon1.DataModuleShutDown(Sender: TCustomDaemon);
begin
WriteLn(Log, 'Shutdown');
Flush(Log);
end;
procedure TDaemon1.DataModuleStart(Sender: TCustomDaemon; var OK: boolean);
begin
OK := True;
WriteLn(Log, 'Start');
Flush(Log);
end;
procedure TDaemon1.DataModuleStop(Sender: TCustomDaemon; var OK: boolean);
begin
OK := True;
WriteLn(Log, 'Stop');
Flush(Log);
end;
initialization
RegisterDaemon;
AssignFile(Log, 'msaplog.txt');
Rewrite(Log);
end.
daemonunit1.lfm
object Daemon1: TDaemon1
OldCreateOrder = False
OnStart = DataModuleStart
OnStop = DataModuleStop
OnShutDown = DataModuleShutDown
OnExecute = DataModuleExecute
Height = 172
HorizontalOffset = 342
VerticalOffset = 210
Width = 193
end
After build you can run it from Terminal[you have to enter folder where executable file is] with:
# ./msapTemp --run
Search for two files as an output:
msapTemp.log [internal Daemon logging file]
and
msaplog.txt [our file for logging with WriteLn]