要在 Unix 上后台运行数据库服务器,并使其能够独立于当前会话运行,可以将其作为守护程序来运行。
不要使用 '&' 在后台运行数据库服务器。如果使用 Unix &(和符号)命令在后台运行数据库服务器,将不会奏效—服务器会立即关闭或停止响应。必须改为将数据库服务器作为守护程序来运行。
同样,如果尝试在程序内使用典型的 fork()-exec()
序列在后台启动服务器,也不会奏效。如果需要这样做,请将 -ud 选项加入数据库服务器选项列表。
可以按以下方式之一将 Unix 数据库服务器作为守护程序来运行:
启动数据库服务器时使用 -ud 选项。例如:
dbsrv12 -ud demo |
使用 dbspawn 工具来启动数据库服务器。例如:
dbspawn dbsrv12 demo |
使用 dbspawn 的一个优点是,dbspawn 进程在确认守护程序已经启动并已准备好接受请求之后才会关闭。如果守护程序由于某种原因而无法启动,则 dbspawn 的退出代码将不为零。
使用 -ud 选项直接启动守护程序时,在守护程序将自身初始化或尝试打开命令中指定的任一数据库之前,dbeng12 和 dbsrv12 命令会创建守护程序进程并立即返回(退出并允许下一个命令得到执行)。
如果想要确保守护程序运行的一个或多个应用程序能够使用数据库服务器,可以使用 dbspawn 来确保守护程序在启动应用程序之前就已运行。以下示例说明如何使用 csh 脚本对此进行测试。
#!/bin/csh # start the server as a daemon and ensure that it is # running before you start any applications dbspawn dbsrv12 demo if ( $status != 0 ) then echo Failed to start demo server exit endif # ok, now you can start the applications ... |
本示例使用 sh 脚本来测试守护程序是否在启动应用程序之前就已运行。
#!/bin/sh # start the server as a daemon and ensure that it is # running before you start any applications dbspawn dbsrv12 demo if [ $? != 0 ]; then echo Failed to start demo server exit fi # ok, now you can start the applications ... |
从 C 程序中生成守护程序。例如:
... if( fork() == 0 ) { /* child process = start server daemon */ execl( "/opt/sqlanywhere12/bin/dbsrv12", "dbsrv12", "-ud", "demo" ); exit(1); } /* parent process */ ... |
请注意,其中使用了 -ud 选项。
![]() |
使用DocCommentXchange讨论此页。
|
版权 © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |