设为首页 收藏本站
查看: 764|回复: 0

[经验分享] Creating a Linux Daemon (service) in Delphi

[复制链接]

尚未签到

发表于 2017-6-10 06:40:12 | 显示全部楼层 |阅读模式
DSC0000.png

  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:
DSC0001.png

  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/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-385664-1-1.html 上篇帖子: 【第一篇】:Linux系统的安装以及服务控制 下篇帖子: PHP开发者的Linux学习之路
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表