Daemons and Services

From Lazarus wiki
Revision as of 15:13, 11 November 2012 by Aurawin (talk | contribs) (Linux (Debian))

English (en) español (es) français (fr) português (pt) русский (ru)

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'.

Install LazDaemon

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.

Classes

TCustomDaemon

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).

TCustomDaemonApplication

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.

TDaemonMapper

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.

Daemon Methods

Start

Called when daemon should start. This method must return immediately with True.

Stop

Called when daemon should stop. This method must return immediately with True.

Shutdown

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.

Pause

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.

Continue

Called when daemon should continue after a pause. This method must return immediately with True. Under Linux this is not triggered.

Install

Called when daemon is registered as windows service. This method should return True on success.

Uninstall

Called when daemon is unregistered as windows service. This method should return True on success.

AfterUnInstall

Called after daemon was unregistered as windows service. This method should return True on success.

HandleCustomCode

Called when a special signal was sent to the daemon. This method should return True on success.

Getting Started

Before you are able to create a Service or Daemon application you must first ensure that the Lazarus Daemon package "lazdaemon" is installed.

Example

There is a simple example in examples/cleandir/. Read the README.txt.

Service Installation

Windows

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.

Linux (Debian)

  1. !/bin/bash
  2. Lazarus service application shell script

SVC_START_OPTIONS="-r" SVC_STOP_OPTIONS="s"

  1. Edit SVC_ALIAS to the long description of your service application

SVC_ALIAS="Description of your program"

  1. Edit SVC_FILENAME to the actual name of your compiled service application

SVC_FILENAME="YourProgram"

  1. Edit SVC_DIR to where you place your compiled service application

SVC_DIR="/bin/YourFolder/"

  1. Edit SVC_SERVICE_SCRIPT to the name of this file without the extension

SVC_SERVICE_SCRIPT="service"

SVC_FILE=$SVC_DIR$SVC_FILENAME start() { if [ -f $SVC_FILE ]; then #reset echo -n "Starting "$SVC_ALIAS": " RETVALS=$(start-stop-daemon -S -b -x $SVC_FILE -- $SVC_START_OPTIONS)

         Count=${#RETVALS[@]}

RETVAL="[FAIL]"

         if [ $Count -eq 0 ]; then

RETVAL="[OK]" elif [ $Count -eq 1 ]; then if [ ${#RETVALS[0]} -eq 0 ]; then RETVAL="[OK]" else iStart=${#SVC_FILE} iLength=${#RETVALS[0]} Response=${RETVALS[0]:(iStart+1):7} RETVAL=$Response if [ "$Response" == "already" ]; then RETVAL="[OK]" fi fi

 	  fi

echo $RETVAL

         return 0

else echo $SVC_ALIAS" not installed" $SVC_DIR exit 2; fi }

stop() { echo -n "Shutting down "$SVC_ALIAS":" RETVALS=$(start-stop-daemon -K -x $SVC_FILE -- $SVC_STOP_OPTIONS) #additional PROCKILLS=$(killall -w -q -e $SVC_PROCESS_NAME $SVC_FILENAME) Count=${#RETVALS[@]} Index=0 RETVAL="[FAIL]" if [ $Count -eq 1 ]; then if [ ${#RETVALS[0]} -eq 0 ]; then RETVAL="[OK]" else Response=${RETVALS[0]:0:2} RETVAL=$Response if [ "$Response" == "No" ]; then RETVAL="[OK]" fi fi else RETVAL="[OK]" fi

echo $RETVAL

       return 0

}

case "$1" in

   start)
       start
       ;;
   stop)
       stop
       ;;
   status)
       status $SVC_SERVICE_SCRIPT
       ;;
   restart)
       stop
       start
       ;;
   *)

echo $SVC_ALIAS" [Invalid Startup Parameters]" echo "Usage: {start|stop|status|restart}"

       exit 1
       ;;

esac exit $?

Linux (Fedora)

See also