• Recent Tutorials

  • Tripping The Class Fantastic: Introductory Quickie

    Daemonising A Process With Kylix

    For anyone who has never encountered Linux (or another Unix derivative), a daemon is the equivalent of a Windows service (and just in case... a service is a background application that is normally loaded and started during operating system startup).

    Delphi provides ready made support for creating services, although the way it handles it implies that you should put all your code in the service application. This is fine and dandy if you can guarantee you are not going to want to do too much interactive debugging, but generally, it ends up being a nightmare. I'll cover creating services and daemons in a later article as there is quite a lot you can do to make your life easier.

    So, for now, lets look at the process of daemonising a process with Kylix. A daemonised process is one that is running outside the confines of a user session. i.e. the process can be started and the user can log off without ending the process. Normally if you log off, the applications you started will be stopped, but a daemonised process is immune from this cull as it is no longer associated with you.

    Sounds slightly complex. In actual fact, creating daemons with Kylix is in many respects easier than creating services with Delphi as it takes a single call to achieve. Time for a quick example.

    Code: [View]
    And there you have it. After executing that command, your application is seperated from your user session and will continue to run indefinitely (providing it doesn't terminate, crash and the machine is not restarted). Seems simple enough, but there is just a little more to it. The only way to terminate this would be shut down the machine... hardly ideal for debugging, so we need to address this.

    With Windows services, Delphi provides you with events that are hooked up to catch service messages, the Stop message for example, allowing you to stop the service. With Kylix you have to create a set of signal handlers. The following code segment illustrates how to do this.

    Code: [View]
    The procedure 'killDaemon' should take no parameters and should be used to signal to the rest of your code that the daemon should terminate. It will be called when the process receives the signals SIGINT, SIGTERM and SIGKILL.

    That's the basics of daemonising a process. The next page provides a full set of example code that can be used to daemonize a process.