Running the Unix database server as a daemon

To run the Unix database server in the background, and to enable it to run independently of the current session, you run it as a daemon.

Do not use '&' to run the database server in the background

If you use the Unix & (ampersand) command to run the database server in the background, it will not work—the server will either immediately shut down or stop responding. You must instead run the database server as a daemon.

As well, attempting to start a server in the background from within a program using the typical fork()-exec() sequence will not work. If you need to do this, add the -ud option to the list of database server options.

You can run the Unix database server as a daemon in one of the following ways:

  1. Use the -ud option when starting the database server. For example:
    dbsrv11 -ud demo
  2. Use the dbspawn tool to start the database server. For example:
    dbspawn dbsrv11 demo

    One advantage of using dbspawn is that the dbspawn process does not shut down until it has confirmed that the daemon has started and is ready to accept requests. If for any reason the daemon fails to start, the exit code for dbspawn will be non-zero.

    When you start the daemon directly using the -ud option, the dbeng11 and dbsrv11 commands create the daemon process and return immediately (exiting and allowing the next command to be executed) before the daemon initializes itself or attempts to open any of the databases specified in the command.

    If you want to ensure that a daemon is running one or more applications that will use the database server, you can use dbspawn to ensure that the daemon is running before starting the applications. The following is an example of how to test this using a csh script.

    #!/bin/csh
    # start the server as a daemon and ensure that it is
    # running before you start any applications
    dbspawn dbsrv11 demo
    if ( $status != 0 ) then
       echo Failed to start demo server
       exit
    endif
    # ok, now you can start the applications
    ...

    This example uses an sh script to test whether the daemon is running before starting the applications.

    #!/bin/sh
    # start the server as a daemon and ensure that it is
    # running before you start any applications
    dbspawn dbsrv11 demo
    if [ $? != 0 ]; then
       echo Failed to start demo server
       exit
    fi
    # ok, now you can start the applications
    ...
  3. Spawn a daemon from within a C program, for example:
    ...
    if( fork() == 0 ) {
          /* child process = start server daemon */
          execl( "/opt/sqlanywhere11/bin/dbsrv11",
    "dbsrv11", "-ud", "demo" );
       exit(1);
    }
    /* parent process */
    ...

    Note that the -ud option is used.

See also