KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Check if Server is Ran as a Service
PRODUCT: 4D Server | VERSION: 15.x | PLATFORM: Mac & Win
Published On: August 1, 2017

Below is a utility method that can check if a 4D Server Database is being ran as a service.

The method is based on the command line service controller "sc" commands.

// Method: currentServerServiceStatus
// Description
// Get the current server service status
//
// Return
// $0 - 1 (Running), 0 (Not running), -1 (Not registered)
// ----------------------------------------------------

C_LONGINT($0;$i)
C_TEXT($name_t;$cmmd_t;$in_t;$out_t;$err_t)

If (Application type=4D Server) & (Folder separator="\\")
   $name_t:=Structure file
   $i:=Length($name_t)
   Repeat
      Case of
         : ($name_t[[$i]]=".")
            $name_t:=Substring($name_t;1;$i-1)
         : ($name_t[[$i]]=Folder separator)
            $name_t:=Delete string($name_t;1;$i)
            $i:=1
      End case
      $i:=$i-1
   Until ($i=0)
   
   $cmmd_t:="sc query \"4DS "+Lowercase($name_t)+"\""
   SET ENVIRONMENT VARIABLE("_4D_OPTION_HIDE_CONSOLE";"true")
   LAUNCH EXTERNAL PROCESS($cmmd_t;$in_t;$out_t;$err_t)
   
   Case of
      : (Position("RUNNING";$out_t)>0)
         $0:=1 // current server service is running
      : (Position("STOPPED";$out_t)>0)
         $0:=0 // current server service is not running
   Else
      $0:=-1 // current server application is not registered as a service
   End case
Else
   $0:=-2 // This is not 4D Server
End if

The method is valid as long as only one instance of the database is being ran. If the database is ran as a service and a second instance is ran as an application, the code will still result in the database being ran as a service as the first instance still appears in the command line's 'sc query' results.

Microsoft Documentation:
More on the Service Controller