Recent

Author Topic: [SOLVED]Running Application in background (daemonizing)  (Read 1906 times)

BSaidus

  • Hero Member
  • *****
  • Posts: 648
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
[SOLVED]Running Application in background (daemonizing)
« on: April 27, 2021, 07:51:43 pm »
Hello.
I have this program running in GUI perfectelly, but when trying to start it in "nogui" mode ( in background)
It finishes and exits.
the program lunch an HttpServer.
I tried to make it run with the help of while loop but It consume CPU.
So, Is there anyway to do it run in backgroud without using the loop. ?
Thank you.
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Interfaces, // this includes the LCL widgetset
  10.   Forms,
  11.   Classes,
  12.   SysUtils,
  13.   Unit1,
  14.   uamjayserver
  15.   { you can add units after this }
  16.   ;
  17.  
  18. {$R *.res}
  19.  
  20. var
  21.   FsParamNogui: String;
  22.   bOK: Boolean ;
  23.  
  24. begin
  25.  
  26.   if ParamCount = 0 then
  27.   begin
  28.     RequireDerivedFormResource := True;
  29.     Application.Scaled := True;
  30.     Application.Initialize;
  31.     Application.CreateForm(TForm1, Form1);
  32.     Application.Run;
  33.   end
  34.   else
  35.   begin
  36.     bOK := True;
  37.     FsParamNogui := ParamStr(1);
  38.     if CompareText(FsParamNogui, 'nogui') = 0 then
  39.     begin
  40.       try
  41.         StartServer( bOK );
  42.         //while bOK do
  43.         //  Application.ProcessMessages;
  44.       finally
  45.         StopServer();
  46.       end;
  47.     end;
  48.   end;
  49.  
  50. end.        
  51.  
« Last Edit: April 28, 2021, 06:27:37 pm by BSaidus »
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: Running Application in background (daemonizing)
« Reply #1 on: April 28, 2021, 02:12:49 am »
Have you looked at the Wiki article Daemons and Services?

zamronypj

  • Full Member
  • ***
  • Posts: 140
    • Fano Framework, Free Pascal web application framework
Re: Running Application in background (daemonizing)
« Reply #2 on: April 28, 2021, 02:18:21 am »
You can wait using TEventObject.waitFor(). while somewhere in your code you set event when receive termination  signal
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

BSaidus

  • Hero Member
  • *****
  • Posts: 648
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Running Application in background (daemonizing)
« Reply #3 on: April 28, 2021, 09:37:55 am »
thank you for your responses,
@trev
first I proceed by creating a service but I got a probleme when Copying the service to make 2 Exe
 first exe : "hhhdm1.exe"  that reads "hhhdm1.json" config file.
 2nd exe :  "hhhdm2.exe" that reads "hhhdm2.json" config file.
 ( Note that the "hhhdm2.exe"  is a copy of "hhhdm1.exe" ).
the 2 services reads config from json file that containe different infos.

When trying to install the 2nd service after installing succesfully the 1st, An error is displayed saying that the service exists (They have the same name).

So, Is there anyway to bypass this situation without recompiling many times.

I have coded this and it works but I encountred problem when uninstalling
Code: Pascal  [Select][+][-]
  1. procedure TAmjayDaemonMapper.AmjayDaemonMapperInstall(Sender: TObject);
  2. var
  3.   cmname: String;
  4. begin
  5.   cmname := ChangeFileExt( ExtractFileName(ParamStr(0)), '' );
  6.   DaemonDefs[0].Name := cmname ;
  7.   DaemonDefs[0].DisplayName := cmname;
  8. end;  
  9.  
  10.  

PS: Look at the attachement.

Thanks you.
« Last Edit: April 28, 2021, 09:43:36 am by BSaidus »
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: Running Application in background (daemonizing)
« Reply #4 on: April 28, 2021, 02:04:01 pm »
Is your Windows operating system 64 bit? Is your service compiled for 64 bit?
« Last Edit: April 28, 2021, 02:09:02 pm by trev »

BSaidus

  • Hero Member
  • *****
  • Posts: 648
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Running Application in background (daemonizing)
« Reply #5 on: April 28, 2021, 02:46:13 pm »
@trev
I'm under 64 bit but my program compiled for 32 bit
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

BSaidus

  • Hero Member
  • *****
  • Posts: 648
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Running Application in background (daemonizing)
« Reply #6 on: April 28, 2021, 06:27:17 pm »
I do not know which miracle happened but It works after insperating from /packages/fcl-extra/exemples/demon.pp

thanks for help.
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: [SOLVED]Running Application in background (daemonizing)
« Reply #7 on: April 29, 2021, 01:20:32 am »
That's a little odd, but glad it worked out.

 

TinyPortal © 2005-2018