Creating a Linux Daemon (service) in Delphi
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:
- http://chapmanworld.com/2017/02/28/embarcadero-delphi-linux-bootcamp/
- http://chapmanworld.com/2016/12/29/configure-delphi-and-redhat-or-ubuntu-for-linux-development/
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…
program Project1;
{$APPTYPE CONSOLE} {$R *.res} uses
System.SysUtils; begin
try
while true do sleep(100);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
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…
program Project1;
{$APPTYPE CONSOLE} {$R *.res} uses
System.SysUtils,
Posix.Unistd; begin
try
if fork()<>0 then begin
exit;
end;
while true do sleep(100);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
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:
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:
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的更多相关文章
- Linux daemon与service 学习笔记
service 常驻在内存中的进程,且可以提供一些系统或网络功能,就是服务. daemon service的提供需要进程的运行,所以实现service的程序我们称为daemon. eg: 实现 ...
- [转] Linux Daemon Writing HOWTO
Linux Daemon Writing HOWTO Devin Watson v1.0, May 2004 This document shows how to write a daemon in ...
- Linux命令service - 系统服务管理(转)
用途说明 service命令用于对系统服务进行管理,比如启动(start).停止(stop).重启(restart).查看状态(status)等.相关的命令还包括chkconfig.ntsysv等,c ...
- Azure的CentOS上安装LIS (Linux Integration Service)
Azure上虚拟化技术都是采用的Hyper-v,每台Linux虚拟机都安装了LIS(Linux Integration Service).LIS的功能是为VM提供各种虚拟设备的驱动.所以LIS直接影响 ...
- 创建 SysV 风格的 linux daemon 程序
本文介绍如何使用 C 语言创建 Linux 系统中 SysV 风格的 daemon 程序.注意:这是一种旧式的 daemon 程序写法,进入 systemd 时代后是不需要通过这样的方式创建 daem ...
- 在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service
在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service 1.在 /etc/rc.d/init.d/ 目录下创建一个名字和服务名完全相同的 shell 脚本文件 joyup ...
- 将Apache加入到linux系统service
将Apache加入到linux系统service 将apache加入到linux系统服务,用service命令来控制apache的启动和停止. 本文由乌合之众瞎写http://www.cnblogs. ...
- 时隔两年最近再次折腾opensuse 的一些笔记 - opensuse linux java service shell
时隔两年最近再次折腾opensuse 的一些笔记 - opensuse linux java service shell opensuse 一些常用命令: service xxx start/s ...
- Linux: service network/Network/NetworkManager
Linux:service network/Network/NetworkManager start 这三种有什么不同? 1.network service的制御网络接口配置信息改动后,网络服务必须从 ...
随机推荐
- gdal库对ENVI文件的一点支持不好
作者:朱金灿 来源:http://blog.csdn.net/clever101 使用GDALOpen函数打开ENVI的img文件,如果使用更新的方式即GA_Update会改写对应的hdr文件.改写h ...
- 【codeforces 534D】Handshakes
[题目链接]:http://codeforces.com/contest/534/problem/D [题意] n个人依次进入一个房间; 进进来的人会和房间里面没有组队的人握一次手; (这里的握手只计 ...
- 【BZOJ 3676】[Apio2014]回文串
[链接] 链接 [题意] 给你一个字符串s. 定义一个子串的出现值为它出现的次数*字符串的长度. 让你求里面的回文子串的最大出现值 |s|<=3e5 [题解] 马拉车算法里面. 只有在回文往外扩 ...
- Spring RestTemplate 专题
相同的参数(接口的入参json打印在日志了)在PostMan中返回预期的数据,但使用RestTemplate时去提示信息错误(参数中汉字).这种情况,搞得怀疑对RestTemplate的理解了使用Re ...
- 我眼中的robot framework
由于近期公司需要,需要一个测试框架对于公司的服务做自动化测试. 由于服务的复杂性,人工测试的方式越来越复杂,体现在以下方面: 1.人工测试步骤复杂,容易出错.服务的复杂性会使人工测试的准备工作,测试条 ...
- 1-5设定NetCore监听端口
问题的起源:启动一个.netCore项目,默认使用的是5000端口,当我们有很多个项目的时候(集群),不可能都使用5000端口. 方法1:set ASPNETCORE_URLS=http://127. ...
- WPF中取得系统字体列表
原文:WPF中取得系统字体列表 在GDI+中,我们可以通过如下方式取得系统所有字体: foreach(FontFamily f in FontFamily.Families){ // 处理代码} ...
- 借助Photoshop,Illustrator等设计软件进行WPF图形图像的绘制
原文:借助Photoshop,Illustrator等设计软件进行WPF图形图像的绘制 本文所示例子是借助第三方设计软件,制作复杂的矢量图形,转成与XAML酷似的SVG,再转换成xaml而实现的. 这 ...
- WPF自定义TextBox及ScrollViewer
原文:WPF自定义TextBox及ScrollViewer 寒假过完,在家真心什么都做不了,可能年龄大了,再想以前那样能专心坐下来已经不行了.回来第一件事就是改了项目的一个bug,最近又新增了一个新的 ...
- Android Studio右键选项中没有Git?
从Git clone一个Project并打开后,都会习惯性的像使用Eclipse一样,选中project右键,选择Git的相应版本号控制选项. 例如以下图,你仅仅看到了svn. 怎样配置才干在右键选项 ...