咏南中间件随LINUX开机自动启动
LINUX DEAMON,LINUX也有类似WINDOWS的服务。
program ynCrossServer;
{$HINTS OFF}
{$APPTYPE CONSOLE} {$R *.res} uses
{$IFDEF MSWINDOWS}
MidasLib,
{$ENDIF }
{$IFDEF POSIX}
Posix.Unistd,
{$ENDIF}
uPlugHelp,
uUnidacPool,
Web.WebReq,
IdHTTPWebBrokerBridge,
uLog,
System.SysUtils,
WebservicesImpl in 'WebservicesImpl.pas',
WebservicesIntf in 'WebservicesIntf.pas',
WebModuleUnit1 in 'WebModuleUnit1.pas' {WebModule1: TWebModule},
uWebservices in 'uWebservices.pas' {ynWebServices: TDataModule},
uIDTcpSvr in 'uIDTcpSvr.pas' {idTcpSvr: TDataModule},
uIDHttpSvr in 'uIDHttpSvr.pas' {idHttpSvr: TDataModule},
uCrossHttpSvr in 'uCrossHttpSvr.pas' {crossHttpSvr: TDataModule},
uCrossTcpSvr in 'uCrossTcpSvr.pas' {crossTcpSvr: TDataModule},
uCrossWebSckSvr in 'uCrossWebSckSvr.pas' {CrossWebSckSvr: TDataModule}; {$IFDEF POSIX}
procedure daemon;
begin
try
if WebRequestHandler <> nil then
WebRequestHandler.WebModuleClass := WebModuleClass;
TUnidacPool.SetDBParams;
Log := TLog.Create;
uPlugHelp.LoadPlugs;
// ynWebServices := TynWebServices.Create(nil);
// crossTcpSvr := TcrossTcpSvr.Create(nil);
crossHttpSvr := TcrossHttpSvr.Create(nil);
// CrossWebSckSvr := TCrossWebSckSvr.Create(nil);
repeat
sleep(10000);
until False;
finally
if idTcpSvr <> nil then
FreeAndNil(idTcpSvr);
if idHttpSvr <> nil then
FreeAndNil(idHttpSvr);
if ynWebServices <> nil then
FreeAndNil(ynWebServices);
if crossHttpSvr <> nil then
FreeAndNil(crossHttpSvr);
if crossTcpSvr <> nil then
FreeAndNil(crossTcpSvr);
if CrossWebSckSvr <> nil then
FreeAndNil(CrossWebSckSvr);
uplughelp.UnloadPlugs;
var i: Integer;
for i := 0 to Length(DBPoolArray) - 1 do
FreeAndNil(DBPoolArray[i]);
FreeAndNil(log);
end;
end;
var pid: Cardinal;
{$ENDIF} begin
{$IFDEF MSWINDOWS}
if WebRequestHandler <> nil then
WebRequestHandler.WebModuleClass := WebModuleClass;
TUnidacPool.SetDBParams;
Log := TLog.Create;
uPlugHelp.LoadPlugs;
//idHttpSvr, crossHttpSvr,要绑定不同的端口,才能同时存在
// idTcpSvr := TidTcpSvr.Create(nil);
// idHttpSvr := TidHttpSvr.Create(nil);
// ynWebServices := TynWebServices.Create(nil);
// crossTcpSvr := TcrossTcpSvr.Create(nil);
crossHttpSvr := TcrossHttpSvr.Create(nil);
// CrossWebSckSvr := TCrossWebSckSvr.Create(nil);
try
Writeln('输入exit,按回车链,退出程序');
var s: string;
while True do
begin
Readln(s);
if sametext('exit', s) then
begin
Break;
end
else
Writeln('无法识别的命令');
end;
finally
if idTcpSvr <> nil then
FreeAndNil(idTcpSvr);
if idHttpSvr <> nil then
FreeAndNil(idHttpSvr);
if ynWebServices <> nil then
FreeAndNil(ynWebServices);
if crossHttpSvr <> nil then
FreeAndNil(crossHttpSvr);
if crossTcpSvr <> nil then
FreeAndNil(crossTcpSvr);
if CrossWebSckSvr <> nil then
FreeAndNil(CrossWebSckSvr);
uplughelp.UnloadPlugs;
var i: Integer;
for i := 0 to Length(DBPoolArray) - 1 do
FreeAndNil(DBPoolArray[i]);
FreeAndNil(log);
end;
{$ENDIF}
{$IFDEF POSIX}
pid := fork;
if pid = 0 then
begin
writeln('starting service');
daemon;
end;
{$ENDIF}
end.
1)将中间件部署文件复制到 /usr/yn/ 目录
2)创建并编辑/usr/lib/systemd/system/yn.service文件
[UNIT]
#服务描述
Description=ynCrossMiddleWare
#指定了在systemd在执行完那些target之后再启动该服务
After=MiddleWare [Service]
#定义Service的运行类型,一般是forking(后台运行)
Type=forking #定义systemctl start|stop|reload *.service 的执行方法(具体命令需要写绝对路径)
#注:ExecStartPre为启动前执行的命令
ExecStart=/usr/yn/ynCrossServer -start
ExecReload=/usr/yn/ynCrossServer -reload
ExecStop=/usr/yn/ynCrossServer -stop #创建私有的内存临时空间
PrivateTmp=True [Install]
#多用户
WantedBy=multi-user.target
重载系统服务:systemctl daemon-reload
人工启动服务:systemctl start yn.service
人工停止服务:systemctl stop yn.service
人工重启服务:systemctl reload yn.service
设置开机启动:systemctl enable yn.service