With the introduction of the Linux target for Delphi, a wide range of possibilities are opened up to Delphi developers, to create Linux server applications. Unfortunately there are currently a limited number of project types available from the RAD Studio IDE, and those do not include creating a service (or Daemon as it’s called in the Linux world).

*note* In this post I am assuming you are already configured to deploy an application to Linux and understand how to launch it from the Linux command line. If this is not the case, see my earlier posts covering these subjects:

Under the Linux operating system, a Daemon is simply an executable which continues running while ignoring the standard input and output streams. That-is, they do not accept input from the keyboard, and they do not present data to the screen (except in special circumstances). So an application which simply continues to run in the background without using the standard input and output, is a Daemon.

One other property of a Daemon application is that it runs in the background. Applications run from the terminal window in Linux are provided with the standard input and output streams, and therefore the terminal becomes unusable until the application terminates. To demonstrate, create a new command line application and set it’s source code to read as follows…

  1. program Project1;
  2. {$APPTYPE CONSOLE}
  3.  
  4. {$R *.res}
  5.  
  6. uses
  7. System.SysUtils;
  8.  
  9. begin
  10. try
  11. while true do sleep(100);
  12. except
  13. on E: Exception do
  14. Writeln(E.ClassName, ': ', E.Message);
  15. end;
  16. end.

If you run this application from the command line, you’ll find that it enters into an infinite loop (the while loop), and you do not regain control of the terminal window until you issue [CTRL] + [C] to abort the application.

Lets now turn this application into a Daemon. Alter your source code to read as follows…

  1. program Project1;
  2. {$APPTYPE CONSOLE}
  3.  
  4. {$R *.res}
  5.  
  6. uses
  7. System.SysUtils,
  8. Posix.Unistd;
  9.  
  10. begin
  11. try
  12. if fork()<>0 then begin
  13. exit;
  14. end;
  15. while true do sleep(100);
  16. except
  17. on E: Exception do
  18. Writeln(E.ClassName, ': ', E.Message);
  19. end;
  20. end.

Now, when you attempt to run this application from the command line, it will immediately exit. It will appear as though the application did nothing. However, issue the instruction:

  1. ps -e

and you should see something like this:

You’ll notice that “Project1” is actually still running, it’s simply been moved to the background.
As a side note, you can kill the application by making a note of it’s process ID (in my case 16898 from the screenshot) and issuing the appropriate kill instruction:

  1. kill 16898

You could now replace the infinite while loop with whatever application logic you need within your daemon.

How does this work?

The key to this working is the “fork()” method, which can be found in the unit “Posix.Unistd”. The fork method is a part of the standard posix API, who’s documentation may be found here: https://linux.die.net/man/3/fork

What fork does is to create a copy of which-ever process calls it, in our case Project1.
So at the moment fork is called, our application execution either continues in the originating process, or it continues in the copy process.

The return value of the call to fork tells us which instance of the process we are executing in. A return value of zero tells us that we are executing in the child process (the copy), and any other value tells us that we are executing in the parent process (the originator). In fact, when the return of fork is not zero, it is the process ID of the newly created child process. We simply call ‘exit’ to exit the application when we determine that we are in the parent process, allowing the child copy to proceed on to the infinite while loop.

Conclusion

At this point you are able to create daemon applications. You may wish to research the Linux (posix) API’s in a little more detail to see how to handle ‘signals’ which may be used to gracefully terminate your application, rather than using the brute force termination of a kill instruction. You might also want some way to communicate with your daemon process. There are many ways to communicate with the process, but possibly the most familiar to a Delphi developer would be to use TCP/IP. Turning your service into a TCP/IP server would allow you to send data and instructions to it.

Regardless of what you decide to do with your daemon, I hope you found this brief instructional useful.

http://chapmanworld.com/2017/04/05/creating-a-linux-daemon-service-in-delphi/

Creating a Linux Daemon (service) in Delphi的更多相关文章

  1. Linux daemon与service 学习笔记

    service 常驻在内存中的进程,且可以提供一些系统或网络功能,就是服务.   daemon service的提供需要进程的运行,所以实现service的程序我们称为daemon.   eg: 实现 ...

  2. [转] Linux Daemon Writing HOWTO

    Linux Daemon Writing HOWTO Devin Watson v1.0, May 2004 This document shows how to write a daemon in ...

  3. Linux命令service - 系统服务管理(转)

    用途说明 service命令用于对系统服务进行管理,比如启动(start).停止(stop).重启(restart).查看状态(status)等.相关的命令还包括chkconfig.ntsysv等,c ...

  4. Azure的CentOS上安装LIS (Linux Integration Service)

    Azure上虚拟化技术都是采用的Hyper-v,每台Linux虚拟机都安装了LIS(Linux Integration Service).LIS的功能是为VM提供各种虚拟设备的驱动.所以LIS直接影响 ...

  5. 创建 SysV 风格的 linux daemon 程序

    本文介绍如何使用 C 语言创建 Linux 系统中 SysV 风格的 daemon 程序.注意:这是一种旧式的 daemon 程序写法,进入 systemd 时代后是不需要通过这样的方式创建 daem ...

  6. 在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service

    在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service 1.在 /etc/rc.d/init.d/ 目录下创建一个名字和服务名完全相同的 shell 脚本文件 joyup ...

  7. 将Apache加入到linux系统service

    将Apache加入到linux系统service 将apache加入到linux系统服务,用service命令来控制apache的启动和停止. 本文由乌合之众瞎写http://www.cnblogs. ...

  8. 时隔两年最近再次折腾opensuse 的一些笔记 - opensuse linux java service shell

    时隔两年最近再次折腾opensuse 的一些笔记 - opensuse linux java service shell opensuse 一些常用命令:    service xxx start/s ...

  9. Linux: service network/Network/NetworkManager

    Linux:service network/Network/NetworkManager start 这三种有什么不同? 1.network service的制御网络接口配置信息改动后,网络服务必须从 ...

随机推荐

  1. XMPP之ios即时通讯客户端开发-配置XMPP基本信息(四)

    前文已经有配置open fire,接下来要通过XMPP框架链接到open fire的服务器: 1.首先要在系统偏好设置里面打开open fire的服务器 2.代码中设置xmpp的myJID 有几个名词 ...

  2. webtool小问题

    webtool小问题 erlang的观察工具如crashdump,appmon,cover等工具有二种不同的界面:gs(wx)和web.这些tool都遵循一定的接口,用户可以自行扩展这些功能,使的能够 ...

  3. 个人官网第8次升级(新功能、用户体验、修复bug、系统优化)

    1.新功能. 操作日志和搜索日志的Excel报表下载. 2.用户体验. 如果是通过搜索,进入到一篇内容, 搜索关键词需要高亮. 比如,搜索"程序员"出现若干内容链接,打开链接的页面 ...

  4. tomcat7,8 centos7 配置apr极好教程

    转自:http://blog.csdn.net/remote_roamer/article/details/51719891 第一次我自己是用的yum安装apr, apr-utils, tomcat- ...

  5. java常用api2

    calendar calendar=calendar.getInstance(); StringBuilder和C#的用法一样 hashSet无序的,不可重复 LInkedHashSet有序的,不可重 ...

  6. 源码分析之Dictionary笔记

    接下来我们一步步来熟悉 Dictionary的底层结构实现,下面的MyDictionary等同于源码中的Dictionary看待. 首先我们定义一个类 MyDictionary,类中定义一个结构Ent ...

  7. 编程算法 - 二叉搜索树(binary search tree) 代码(C)

    二叉搜索树(binary search tree) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 二叉搜索树(binary search tree)能 ...

  8. WPF 使用 SharpDX

    原文:WPF 使用 SharpDX 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee.io 访问 ...

  9. 一句话的设计模式(JAVA版)

    ·结构型模式: o适配器:用来把一个接口转化成另一个接口,如 java.util.Arrays#asList(). o桥接模式:这个模式将抽象和抽象操作的实现进行了解耦,这样使得抽象和实现可以独立地变 ...

  10. 在Microsoft Expression Blend 2 中绘制圆角矩形按钮

    原文:在Microsoft Expression Blend 2 中绘制圆角矩形按钮 /* 声明:转载请保留此信息:http://www.BrawDraw.com, http://www.ZPXP.c ...