Recent

Author Topic: Console application  (Read 5440 times)

ertank

  • Sr. Member
  • ****
  • Posts: 274
Console application
« on: January 20, 2018, 12:35:03 am »
Hello,

I am using Lazarus 1.8.1 (fixes_1_8 branch) and fpc 3.1.1 (trunk). Both built from scratch. My system is a Debian 9. Lazarus and fpc last check out was 3 days ago. OS updated & upgraded 3 days ago, too.

I am trying to build myself a kind of server. I use database access (Zeoslib), http (IdHTTPServer), tcp (IdTCPServer), some routines from mORMot framework. All of these are put in a TDataModule as well as all my logic code.

I have enabled threading using -dUseCThreads in custom project options.

My project has only following code:
Code: [Select]
program httpserver_tcpserver;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces,
  SysUtils,
  uData,
  zcomponent,
  indylaz;

begin
  try
    DM := TDM.Create(nil);
    try
      if not DM.IdTCPServer1.Active then Exit();
      if not DM.IdHTTPServer1.Active then Exit();

      if not DM.DB.Connected then
      begin
        DM.Log('No database connection. Exiting...');
        Exit();
      end;

      while True do Sleep(250);
    finally
      DM.Log('server stopped.');
      DM.Free();
    end;
  except
    on E: Exception do
    begin
      WriteLn('error: ' + E.Message);
    end;
  end;
end.
uData is not included in application using project manager, but included as a unit.

When I try to run compiled application in console, I get following warnings (I tried to translate them to English as Linux locale is not English. They should be something similar):
Code: [Select]
(project1:2076): Gtk-WARNING **: Theme device module_path could not be found in "adwaita",
(project1:2076): Gtk-WARNING **: Theme device module_path could not be found in "adwaita",
What I would like is a pure console application and no GUI. I am not bothered with warnings normally, but I cannot start my application at boot time using /etc/rc.local. What I observe is that it cannot become a daemon but sticks to a TTY. I believe that behavior prevents it to auto-run at system reboot. I can manually run it if a user login and run the executable. Executable do not require root access rights, and can runs as a regular user.

if I remove LCL, FCL dependency from project manager, also remove Interfaces unit from uses and compile. I get linking errors at my very final "end." line above. Says "generic error while linking". (Yes, I received that warning dialog displayed at me about removing LCL will result funny errors).

I started a new project using Project->New Project->Program which is indicated as command line program. Added uData to my uses and I continue to get these warnings above. Exactly same error if I do not include Intercafes unit, let LCL stay as a dependency in project manager.

Things do not change if I set my widget to "nogui".

I am stuck and any help is appreciated.


 

Lutz Mändle

  • Jr. Member
  • **
  • Posts: 65
Re: Console application
« Reply #1 on: January 20, 2018, 01:33:45 am »
Set the widgetsettype to nogui and compile with cleanup and build. The widgetsettype selects the version of the interfaces unit.
« Last Edit: January 20, 2018, 01:39:20 am by Lutz Mändle »

ertank

  • Sr. Member
  • ****
  • Posts: 274
Re: Console application
« Reply #2 on: January 20, 2018, 03:12:12 pm »
I converted my application to use LazDaemon and to become a daemon. However, It hangs on the console when I run it.

Attached project is re-producing my problem.

Basically, I am looping in Execute event as following:
Code: [Select]
procedure TDaemon1.DataModuleExecute(Sender: TCustomDaemon);
begin
  Log('executing...');

  while Self.Status = csRunning do
  begin
    Sleep(1000);
  end;
end;

When I run daemontest in shell I get following behavior:
Code: [Select]
ek@debian:~/daemontest$ ./daemontest -r
^Cek@debian:~/daemontest$ cat daemontest.log
2018-01-20 17:06:12.348   daemontest started
2018-01-20 17:06:12.348   executing...
ek@debian:~/daemontest$
Which means, my daemon cannot go in background by itself as regular linux daemons do. (Pay attention to "^C" in above screen output)

I appreciate any help on modifying my code so that it does behave like a real daemon under linux OS.

Thanks & regards.

Lutz Mändle

  • Jr. Member
  • **
  • Posts: 65
Re: Console application
« Reply #3 on: January 21, 2018, 03:32:48 pm »
The daemon needs the commandline option -b to go in the background by itself. This is the designed behavior of FPCs daemonapp framework. Look in ....../packages/fcl-extra/src/unix/daemonapp.inc.
Be aware that by self backgrounding the daemon changes it's working directory to '/' so that your Log procedure probably don't work. Use an absolute path for the logfile which points to a location where the daemon can write.

ertank

  • Sr. Member
  • ****
  • Posts: 274
Re: Console application
« Reply #4 on: January 21, 2018, 04:24:51 pm »
I see following options printed on the screen if I execute the application without any parameter
Code: [Select]
Where command is one of the following:
  -i --install   To install the program as a service
  -u --uninstall To uninstall the service
  -r --run       To run the service

Same display printed if I execute the application with "-d" or "-b" parameter.
« Last Edit: January 21, 2018, 04:34:26 pm by ertank »

Lutz Mändle

  • Jr. Member
  • **
  • Posts: 65
Re: Console application
« Reply #5 on: January 21, 2018, 04:46:07 pm »
The shown output is valid for windows because on that os this is the established behaviour. But installing or uninstalling does not work that way in linux. The relevant methods for installing and uninstalling in daemonapp.inc for unix are empty.

On unix/linux the only working option from the help message is -r/--run and the option -b/--background is not mentioned, execute the application with "-r" and "-b" parameter.
« Last Edit: January 21, 2018, 04:48:31 pm by Lutz Mändle »

ertank

  • Sr. Member
  • ****
  • Posts: 274
Re: Console application
« Reply #6 on: January 21, 2018, 07:07:40 pm »
Using "-r" and "-b" together did put the application in background. I still shall do more tests. However, I now hope that it will be fine for my needs.

Thank you.

 

TinyPortal © 2005-2018