Using Delphi 10.2 To develop daemon on Linux

Source: Internet
Author: User

Delphi 10.2 supports Linux, and the official only supports command-line programming, which is to do Linux server-side development.

Since it is a Linux server-side development, then the normal command line run the program, and then wait for a black window to open the way it

Too low (at present there are individual language big coffee, often open a black window on windows, looks very disgusting), then if

To avoid this embarrassing problem?

In fact, there are similar features of Windows services under Linux, Linux Daemon is one of the ways, after the command line runs

Return directly and create a similar process in the background. accepts client access. Some of the most common Linux services are basically

To work like this. such as famous Apache,mysql and so on. Please refer to this article for specific Linux Daemon introduction.

Today we use Delphi to develop a background HTTP server to illustrate how Delphi develops Linux daemon.

Create a new project and build a console application.

Because this can only be run on Linux, join Linux support directly.

Because we want to do HTTP server, we need to put some controls, so we add a datamodule.

Then put a tidhttpserver control on the Datamodule.

Add the following event to the Oncommandget.

procedure Tdmf.idhttpserver1commandget (acontext:tidcontext;  Arequestinfo:tidhttprequestinfo; Aresponseinfo:tidhttpresponseinfo); begin      aresponseinfo.contenttext:='I am Delphi for Linux service'; End;

All right.

We'll go back to the engineering document

Enter the following code

 ProgramProject2;{$APPTYPE CONSOLE}{$R *.res}usesposix.unistd, Posix.systypes, System.sysutils, DMPinch 'Dmp.pas' {Dmf:tdatamodule};procedureDaemon;beginDMF:=TDMF.Create(Nil); Dmf. Idhttpserver1.active:=True; Try    RepeatSleep (Ten* +); untilFalse; finallyDMF.  Free; End;End;varpid:pid_t;beginPID:=Fork; ifPID =0  Then   beginWriteln ('Starting service');   Daemon End;End.

Compile and run.

First, let's look at the running process in the system: PS-EF

There is no information on the PROJECT2.

We run this program

After running, the program immediately returns

We're looking at what's happening in the system process.

We can see that this Project2 is still in progress.

So is this process working properly?

Let's open the browser and see what happens.

You can see that this program runs very normally in the back.

Of course, since this is a demo, I did not do too much processing, in fact, because the daemon process can not directly interact with the foreground, we later write this application.

Log files should be used to record the operation, general problems, can quickly find the root cause of the problem.

Then this program has been running in the background, we compile and publish again, there is no way to overwrite, how to turn off the background process?

The approach is simple and rough (I like it anyway).

Use the KILL command for Linux.

Find the PID of the application and kill directly

Then use PS-EF to see

Project2 is gone, the whole world is quiet.

Using Delphi 10.2 To develop daemon on Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.