What is proper way to deamonize console app

Hello all

I have been trying to get a console app to autostart using Systemd.service. It won’t start. I can however start it manually. I think it has to do with it not being daemonized due to an error shown in the log file:

Sep  4 17:37:19 AxcysEmbeddedController /home/pi/Public/axcys/AxcysFacilitySecurityManager/AxcysFacilitySecurityManager[6149]: Trying to daemonize the Axcys application.
Sep  4 17:37:19 AxcysEmbeddedController systemd[1]: axcys-embedded-engine.service holdoff time over, scheduling restart.
Sep  4 17:37:19 AxcysEmbeddedController systemd[1]: Stopping Axcys Embedded Engine...
Sep  4 17:37:19 AxcysEmbeddedController systemd[1]: Starting Axcys Embedded Engine...
Sep  4 17:37:19 AxcysEmbeddedController systemd[1]: axcys-embedded-engine.service start request repeated too quickly, refusing to start.
Sep  4 17:37:19 AxcysEmbeddedController systemd[1]: Failed to start Axcys Embedded Engine.
Sep  4 17:37:19 AxcysEmbeddedController systemd[1]: Unit axcys-embedded-engine.service entered failed state.

So I checked my Daemonize code and was originally using this:

#If Not DebugBuild Then // Do not try to daemonize a debug build
  System.Log( System.LogLevelCritical, "Trying to daemonize the Axcys application.")
  If Not App.Daemonize Then
    System.Log( System.LogLevelCritical, "Could not daemonize the Axcys application.")
    Return -1
  End If
  
  System.Log( System.LogLevelCritical, "Daemonized the Axcys application.")
#EndIf

So then I tried using the latest sample code from the help:

#If Not DebugBuild Then // Do not try to daemonize a debug build
  If (args(1) = "start" Or args(1) = "-d") Then // Check for command-line parameter to daemonize
    If Not App.Daemonize Then
      System.Log( System.LogLevelCritical, "Could not daemonize the app.")
      Return -1
    End If
  End If
#Endif

That last code would cause an immediate crash, even when starting it manually.

Am I daemonizing properly?

I want to use this with systemd.service to autostart - which does not work either…

Thanks,
Tim

If args.Ubound>0 And (args(1) = "start" Or args(1) = "-d") Then // or it could crash without parameters End