Daemons and Services
- 1 What are daemons and services?
- 2 Install LazDaemon
- 3 Classes
- 4 Daemon - Step by Step
- 5 Daemon Methods
- 6 Example
- 7 Service Installation
- 8 See also
What are daemons and services?
Unix daemons and windows services are programs running without user interaction. For example www or ftp servers are called daemons under linux and services under windows. Because they do not interact with the user directly, they close their stdin, stdout, stderr descriptors at start.
With Free Pascal, Lazarus it is possible to write these daemons/services platform independent via the Lazarus lazdaemon package. To avoid name conlicts with the Delphi components these classes are called 'daemons'.
Before you can start, install the lazdaemon package. Either via Components / Configure installed packages or by opening/installing the lpk file directly: lazarus/components/daemon/lazdaemon.lpk. This package installs some new components and menu items in the IDE.
This is a TDataModule descendant that does all the work. There can be several TCustomDaemons classes and/or instances running at the same time and in the same process (multi threaded).
This is a TCustomApplication descendant which creates the TCustomDaemons. This does not need any change. It runs under windows until it receives the Stop call or under linux until the TERM signal.
This component handles the service registration. Each instance needs one entry in the property DaemonDefs.
Daemon - Step by Step
- When the daemon is started the command line parameters are parsed. The following are predefined:
- -i --install: register the daemon. This has no effect under unix.
- -u --uninstall: unregister the daemon. This has no effect under unix.
- -r --run: start the daemon. Windows does this normally itself.
- Create the TDaemonMapper
- Create one TCustomDaemon for each entry of DaemonDefs.
- install, uninstall or run every instance.
- if run: start every instance in its own thread and then wait for Stop/TERM signal.
Called when daemon should start. This method must return immediately with True.
Called when daemon should stop. This method must return immediately with True.
Called when daemon should be killed. This method must stop the daemon immediately and return with True. This is not triggered under Linux. Linux simply kills the daemon.
Called when daemon should pause. This method must return immediately with True. Under Linux this is not triggered because the kernel stops the whole daemon on STOP and continues it on CONT.
Called when daemon should continue after a pause. This method must return immediately with True. Under Linux this is not triggered.
Called when daemon is registered as windows service. This method should return True on success.
Called when daemon is unregistered as windows service. This method should return True on success.
Called after daemon was unregistered as windows service. This method should return True on success.
Called when a special signal was sent to the daemon. This method should return True on success.
Before you are able to create a Service or Daemon application you must first ensure that the Lazarus Daemon package "lazdaemon" is installed.
There is a simple example in examples/cleandir/. Read the README.txt.
You can install the service by executing the process with the Install parameter. Windows service manager will do the rest for you. You can configure the service and it's options from the service manager.
Configure the sample script located at Service Script.
SVC_ALIAS is the long description of your application SVC_FILENAME is the actual file name of your compiled service application SVC_DIR is the place your you copied the service application SVC_SERVICE_SCRIPT is the final name of the service.sh when you "Save As" the service.sh script.