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

Since it is a Linux server-side development, then the normal command line runs the program, and then waits for a black window to open.

Too low (currently there are individual languages, often open a black window on Windows, it looks very disgusting), then if

Avoid this embarrassing problem?

    In fact, there are similar functions of windows services under Linux. Linux Daemon is one of them. After the command line runs.

Return directly, while creating the same process in the background. Accept client access. Some common linux services are basically

This works. For example, the famous apache, mysql and so on. For an introduction to the specific Linux Daemon, please refer to this article .

  Today we use delphi to develop a background http server to illustrate how delphi develops Daemon for Linux.

Create a new project and build a console application.

Because this can only be run in linux, so directly join linux support.

Because we have to do the http server, we have to put some controls, so 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);
     AResponseInfo.ContentText: = ' I am delphi for linux service ' ;
 end ;

All right.

We return to the project file

Enter the following code

Program Project2;


{ $R *.res } 
  Dmp in  ' dmp.pas '  { dmf: TDataModule } ;

Procedure daemon;

   : =Tdmf. Create ( nil );
   dmf.IdHTTPServer1.Active: = True;



      Sleep( 10 * 1000 );

    Until False;

  End ;

End ;

   Pid: pid_t;

  pid : = fork;
   if pid = 0  then 
    writeln( ' starting service ' );
   End ;

End .

Compile and run.

First let’s take a look at the running process in the system: ps -ef


There is no information for Project2 inside.

We run this program

We can see that this Project2 is still in the process.

So is this process working properly?

Let’s open the browser and see what happens?

It can be seen that this program runs very well later.

Of course, since this is a demo, I didn’t do much processing. In fact, because the daemon process can’t directly interact with the foreground, we will write this application later.

Log files should be used to record the running status. When problems occur, the root cause of the problem can be quickly found.

Then this program has been running in the background. When we compile and release again, there is no way to overwrite it. How to turn off this background process?

The method is very simple and very rude (I like it anyway).

Use the kill command of linux.

Find the pid of the app and kill it directly

Then use ps -ef to see

Project2 is gone.