我们经常需要当一个进程挂了之后能自动被拉起来,保证服务正常运行,这时候可以考虑使用supervisor。概念 supervisor是用Python开发的一套通用的进程管理程序(CS模型),可以方便的启动、重启、关闭单个或多个进程(包括非Python进程)。 supervisor是类似于一个监管员,它在系统中是一个进程管理工具,当系统进程中断的时候supervisor能自动重新启动它。可以运行在各种类unix的机器上。 supervisor是一个CS模型的程序,supervisord是server端,supervisorctl是client端。下载python3pip的离线安装包 为了方便后面做离线安装包,这里将所有的依赖的包下载到本地,可以方便到无网络环境下安装。 aptgetdownloadpython3pipaptcachedependsipython3pipawkDepends:{print2}xargsaptgetdownload aptgetdownloadpython3distutilsaptcachedependsipython3distutilsawkDepends:{print2}xargsaptgetdownload aptgetdownloadpython3setuptoolsaptcachedependsipython3setuptoolsawkDepends:{print2}xargsaptgetdownload aptgetdownloadpython3wheelaptcachedependsipython3wheelawkDepends:{print2}xargsaptgetdownload本地安装python3pip【不需要网络】 dpkgipython3distutils3。10。6122。04all。deb dpkgipython3setuptools59。6。01。2all。deb dpkgipython3wheel0。37。12all。deb dpkgipython3pip22。0。2dfsg1all。deb下载supervisor并安装 pip3downloadsupervisor4。2。5py2。py3noneany。whl pip3installsupervisor4。2。5py2。py3noneany。whl 安装完成后,可以找到usrlocalbinsupervisorctl创建配置 mkdirpetcsupervisordconf。d echosupervisordconfetcsupervisord。conf修改配置 配置末尾的include注释,改为: vietcsupervisord。conf 〔include〕 filesetcsupervisorconf。d。ini;打开注释(;),将路径修改为etcsupervisordconf。d ;这句代码说明它会加载etcsupervisorconf。d文件夹中的所有。ini配置文件 〔rootbogonetc〕vietcsupervisord。conf 去掉注释前:;〔inethttpserver〕;inet(TCP)serverdisabledbydefault;port127。0。0。1:9001;ipaddress:portspecifier,:portforalliface;usernameuser;defaultisnousername(openserver);password123;defaultisnopassword(openserver) 去掉之后:〔inethttpserver〕;inet(TCP)serverdisabledbydefaultport:9001;ipaddress:portspecifier,:portforallifaceusernameroot;defaultisnousername(openserver)passwordroot;defaultisnopassword(openserver) 〔supervisord〕logfiletmpsupervisord。log;mainlogfile;defaultCWDsupervisord。loglogfilemaxbytes50MB;maxmainlogfilebytesb4rotation;default50MBlogfilebackups10;ofmainlogfilebackups;0meansnone,default10loglevelinfo;loglevel;defaultinfo;others:debug,warn,tracepidfiletmpsupervisord。pid;supervisordpidfile;defaultsupervisord。pidnodaemonfalse;startinforegroundiftrue;defaultfalsesilentfalse;nologstostdoutiftrue;defaultfalseminfds1024;min。availstartupfiledescriptors;default1024minprocs200;min。availprocessdescriptors;default200;umask022;processfilecreationumask;default022;usersupervisord;setuidtothisUNIXaccountatstartup;recommendedifroot;identifiersupervisor;supervisordidentifier,defaultissupervisor;directorytmp;defaultisnottocdduringstart;nocleanuptrue;dontcleanuptempfilesatstart;defaultfalse;childlogdirtmp;AUTOchildlogdir,defaultTEMP;environmentKEYvalue;keyvaluepairstoaddtoenvironment;stripansifalse;stripansiescapecodesinlogs;def。false;Therpcinterface:supervisorsectionmustremainintheconfigfilefor;RPC(supervisorctlwebinterface)towork。Additionalinterfacesmaybe;addedbydefiningtheminseparate〔rpcinterface:x〕sections。 将;usersupervisord修改为userroot 启动服务 supervisordcetcsupervisord。conf 启动完成后可以使用psefgrepsupervisor命令查看进程rootljdefaultstring:usrlocalSINOpackagepsefgrepsupervisorroot11593381016:46?00:00:00usrbinpython3usrlocalbinsupervisordcetcsupervisord。confroot1202622639110017:17pts100:00:00grepcolorautosupervisorrootljdefaultstring:usrlocalSINOpackage浏览器查看服务状态 http:ip:9001然后输入用户名和密码 即可登录supervisorweb管理页面,其界面如下 配置文件详解;Samplesupervisorconfigfile。〔unixhttpserver〕filevarrunsupervisorsupervisor。sock;socket路径;chmod0700;socket文件的权限;chownnobody:nogroup;socket所属用户及组;usernameuser;用户名;password123;密码;〔inethttpserver〕;是否启用服务,默认是关闭的(启用的话可以看到supervisor管理的服务状态);port127。0。0。1:9001;监听的IP及端口;usernameuser;用户名;password123;密码〔supervisord〕;supervisord全局配置logfilevarlogsupervisorsupervisord。log;supervisor日志路径logfilemaxbytes50MB;单个日志文件最大数logfilebackups10;保留多少个日志文件(默认10个)loglevelinfo;(loglevel;defaultinfo;others:debug,warn,trace)pidfilevarrunsupervisord。pid;pid文件路径nodaemonfalse;启动是否丢到前台,设置为false,表示以daemon的方式启动minfds1024;最小文件打开数,对应系统limit。conf中的nofile,默认最小为1024,最大为4096minprocs200;最小的进程打开数,对应系统的limit。conf中的nproc,默认为200;umask022;(processfilecreationumask;default022);userchrism;启动supervisord服务的用户,默认为root;identifiersupervisor;(supervisordidentifier,defaultissupervisor);directorytmp;这里的目录指的是服务的工作目录;nocleanuptrue;(dontcleanuptempfilesatstart;defaultfalse);childlogdirtmp;(AUTOchildlogdir,defaultTEMP);environmentKEYvalue;(keyvaluepairstoaddtoenvironment);stripansifalse;(stripansiescapecodesinlogs;def。false);thebelowsectionmustremainintheconfigfileforRPC;(supervisorctlwebinterface)towork,additionalinterfacesmaybe;addedbydefiningtheminseparaterpcinterface:sections〔rpcinterface:supervisor〕supervisor。rpcinterfacefactorysupervisor。rpcinterface:makemainrpcinterface〔supervisorctl〕serverurlunix:varrunsupervisorsupervisor。sock;useaunix:URLforaunixsocket;serverurlhttp:127。0。0。1:9001;useanhttp:urltospecifyaninetsocket;usernamechris;shouldbesameashttpusernameifset;password123;shouldbesameashttppasswordifset;promptmysupervisor;cmdlineprompt(defaultsupervisor);historyfile。schistory;usereadlinehistoryifavailable;Thebelowsampleprogramsectionshowsallpossibleprogramsubsectionvalues,;createoneormorerealprogram:sectionstobeabletocontrolthemunder;supervisor。;〔program:theprogramname〕;定义一个守护进程,比如下面的elasticsearch;commandbincat;启动程序使用的命令,可以是绝对路径或者相对路径;processname(programname)s;一个python字符串表达式,用来表示supervisor进程启动的这个的名称,默认值是(programname)s;numprocs1;Supervisor启动这个程序的多个实例,如果numprocs1,则processname的表达式必须包含(processnum)s,默认是1;directorytmp;supervisord在生成子进程的时候会切换到该目录;umask022;umaskforprocess(defaultNone);priority999;权重,可以控制程序启动和关闭时的顺序,权重越低:越早启动,越晚关闭。默认值是999;autostarttrue;如果设置为true,当supervisord启动的时候,进程会自动启动;autorestarttrue;设置为随supervisord重启而重启,值可以是false、true、unexpected。false:进程不会自动重启;startsecs10;程序启动后等待多长时间后才认为程序启动成功,默认是10秒;startretries3;supervisord尝试启动一个程序时尝试的次数。默认是3;exitcodes0,2;一个预期的退出返回码,默认是0,2。;stopsignalQUIT;当收到stop请求的时候,发送信号给程序,默认是TERM信号,也可以是HUP,INT,QUIT,KILL,USR1,orUSR2;stopwaitsecs10;在操作系统给supervisord发送SIGCHILD信号时等待的时间;userchrism;如果supervisord以root运行,则会使用这个设置用户启动子程序;redirectstderrtrue;如果设置为true,进程则会把标准错误输出到supervisord后台的标准输出文件描述符;stdoutlogfileapath;把进程的标准输出写入文件中,如果stdoutlogfile没有设置或者设置为AUTO,则supervisor会自动选择一个文件位置;stdoutlogfilemaxbytes1MB;标准输出log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小;stdoutlogfilebackups10;标准输出日志轮转备份的数量,默认是10,如果设置为0,则不备份;stdoutcapturemaxbytes1MB;当进程处于stderrcapturemode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB;stdouteventsenabledfalse;如果设置为true,当进程在写它的stderr;stderrlogfileapath;把进程的错误日志输出一个文件中,除非redirectstderr参数被设置为true;stderrlogfilemaxbytes1MB;错误log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小;stderrlogfilebackups10;错误日志轮转备份的数量,默认是10,如果设置为0,则不备份;stderrcapturemaxbytes1MB;当进程处于stderrcapturemode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB;stderreventsenabledfalse;如果设置为true,当进程在写它的stderr到文件描述符的时候,PROCESSLOGSTDERR事件会被触发;environmentA1,B2;一个kv对的list列表;serverurlAUTO;是否允许子进程和内部的HTTP服务通讯,如果设置为AUTO,supervisor会自动的构造一个url;Thebelowsampleeventlistenersectionshowsallpossible;eventlistenersubsectionvalues,createoneormorereal;eventlistener:sectionstobeabletohandleeventnotifications;sentbysupervisor。这个地方是自定义一个守护进程〔program:elasticsearch〕;定义一个守护进程elasticsearchenvironmentESHOMEusrlocalelasticsearch;设置ESHOME环境变量userelk;启动elasticsearch的用户directoryusrlocalelasticsearch;进入到这个目录中commandusrlocalelasticsearchbinelasticsearch;执行启动命令numprocs1;Supervisor启动这个程序的多个实例,如果numprocs1,则processname的表达式必须包含(processnum)s,默认是1autostarttrue;设置为随supervisord启动而启动autorestarttrue;设置为随supervisord重启而重启startretries3;设置elasticsearch重启的重试次数priority1;权重,可以控制程序启动和关闭时的顺序,权重越低:越早启动,越晚关闭。默认值是999;〔eventlistener:theeventlistenername〕;commandbineventlistener;theprogram(relativeusesPATH,cantakeargs);processname(programname)s;processnameexpr(default(programname)s);numprocs1;numberofprocessescopiestostart(def1);eventsEVENT;eventnotif。typestosubscribeto(reqd);buffersize10;eventbufferqueuesize(default10);directorytmp;directorytocwdtobeforeexec(defnocwd);umask022;umaskforprocess(defaultNone);priority1;therelativestartpriority(default1);autostarttrue;startatsupervisordstart(default:true);autorestartunexpected;restartatunexpectedquit(default:unexpected);startsecs10;numberofsecsprogmuststayrunning(def。1);startretries3;maxofserialstartfailures(default3);exitcodes0,2;expectedexitcodesforprocess(default0,2);stopsignalQUIT;signalusedtokillprocess(defaultTERM);stopwaitsecs10;maxnumsecstowaitb4SIGKILL(default10);userchrism;setuidtothisUNIXaccounttoruntheprogram;redirectstderrtrue;redirectprocstderrtostdout(defaultfalse);stdoutlogfileapath;stdoutlogpath,NONEfornone;defaultAUTO;stdoutlogfilemaxbytes1MB;maxlogfilebytesb4rotation(default50MB);stdoutlogfilebackups10;ofstdoutlogfilebackups(default10);stdouteventsenabledfalse;emiteventsonstdoutwrites(defaultfalse);stderrlogfileapath;stderrlogpath,NONEfornone;defaultAUTO;stderrlogfilemaxbytes1MB;maxlogfilebytesb4rotation(default50MB);stderrlogfilebackups;ofstderrlogfilebackups(default10);stderreventsenabledfalse;emiteventsonstderrwrites(defaultfalse);environmentA1,B2;processenvironmentadditions;serverurlAUTO;overrideserverurlcomputation(childutils);Thebelowsamplegroupsectionshowsallpossiblegroupvalues,;createoneormorerealgroup:sectionstocreateheterogeneous;processgroups。;〔group:thegroupname〕;服务组管理,可以将多个服务名写到这里管理(组名自定义);programsprogname1,progname2;上面配置好的服务名,比如elasticsearch,kibana,logstash;priority999;therelativestartpriority(default999);The〔include〕sectioncanjustcontainthefilessetting。This;settingcanlistmultiplefiles(separatedbywhitespaceor;newlines)。Itcanalsocontainwildcards。Thefilenamesare;interpretedasrelativetothisfile。Includedfilescannot;includefilesthemselves。〔include〕filesetcsupervisord。d。ini;具体进程 下面到了最关键步骤,自己定义一个ini文件,将需要守护的进程的配置放到 etcsupervisorconf。d这个目录下,如下图〔program:mainsoundbelt〕userjetsondirectorymntaialgorithmcommandpython3mainsoundbelt。pyautostarttrueautorestarttruestartretries1redirectstderrtruestdoutlogfilemntaialgorithmlogmainsoundbelt。logstderrlogfilemntaialgorithmlogmainsoundbelt。logstdoutlogfilemaxbytes1MBstdoutlogfilebackups5nodaemontrueenvironmentOPENBLASCORETYPEARMV8,LDPRELOADmntaialgorithmlibscikitlearn。libslibgompd22c30c5。so。1。0。0 注意上面的environment表示启动时需要的环境变量。修改项目配置文件后,重新加载rootljdefaultstring:usrlocalSINOpackagesupervisorctlreloadRestartedsupervisord 启动成功可以在网页上面看到running的状态