• 咏南中间件随LINUX开机自动启动


    咏南中间件随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

  • 相关阅读:
    《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇:简介及目录》(附上完整工程文件)
    Genesis-3D开源游戏引擎简介!
    《Genesis-3D开源游戏引擎--横版格斗游戏制作教程:简介及目录》(附上完整工程文件)
    《Genesis-3D开源游戏引擎-官方录制系列视频教程:进阶实例篇》
    《Genesis-3D开源游戏引擎-官方录制系列视频教程:基础操作篇》
    《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇08:弹幕系统》本系列完结
    《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇07:全屏炸弹》
    《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇06:计分》
    《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇05:角色中弹》
    《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇04:碰撞检测》
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/11458640.html
Copyright © 2020-2023  润新知