很多时候,我们需要将一些服务在Linux系统启动时即自动运行,省得每次都要去手动启动一遍,如Redis,MySQL,Nginx等。本文对CentOS与Ubuntu下开机自启动的配置方法进行整理,供参考查阅。CentOS7的开机自启动配置一。rc。local方式 rc。local是CentOS以前版本的方式,在CentOS7中仍然以兼容的形式存在,虽仍可用,但不推荐(推荐使用systemdservice)。 1、编写需要开机自启动的脚本,并添加执行权限〔rootdevserver1〕vimtestrclocal。sh!binbashtimedateFTechotimefromrc。localtmptest。log〔rootdevserver1〕chmodxtestrclocal。sh复制代码 作为测试,上述脚本打印一个时间到tmptest。log文件中 2、在etcrc。drc。local配置文件中添加脚本运行命令(使用绝对路径)〔rootdevserver1〕vimetcrc。drc。local!binbash。。。注释部分touchvarlocksubsyslocalroottestrclocal。shdevnull2devnull复制代码 3、添加etcrc。drc。local文件的执行权限 在centos7中,etcrc。drc。local没有执行权限,需要手动授权〔rootdevserver1〕chmodxetcrc。drc。local复制代码 以上三步,即可使roottestrclocal。shdevnull2devnull命令在服务器系统启动时自动运行。二。chkconfig方式 1、编写需要开机自启动的测试脚本,并添加执行权限〔rootdevserver1〕vimtestchkconfig。sh!binbashtimedateFTechotimefromchkconfigtmptest。log〔rootdevserver1〕chmodxtestchkconfig。sh复制代码 2、在etcrc。dinit。d目录下添加一个可执行脚本testchkconfig〔rootdevserver1〕vimetcrc。dinit。dtestchkconfig!binbashchkconfig:23459010description:testchkconfigroottestchkconfig。shdevnull2devnull〔rootdevserver1〕chmod755etcrc。dinit。dtestchkconfig复制代码 上述testchkconfig脚本的头部必须遵循一定的格式chkconfig:23459010,其中2345指定服务在哪些执行等级中开启或关闭,90表示启动的优先级(0100,越大优先级越低),10表示关闭的优先级。执行等级包括0:表示关机1:单用户模式2:无网络连接的多用户命令行模式3:有网络连接的多用户命令行模式4:保留未使用5:带图形界面的多用户模式6:重新启动 3、加入开机启动服务列表〔rootdevserver1〕chkconfigaddtestchkconfig〔rootdevserver1〕chkconfiglistNote:ThisoutputshowsSysVservicesonlyanddoesnotincludenativesystemdservices。SysVconfigurationdatamightbeoverriddenbynativesystemdconfiguration。Ifyouwanttolistsystemdservicesusesystemctllistunitfiles。Toseeservicesenabledonparticulartargetusesystemctllistdependencies〔target〕。netconsole0:off1:off2:off3:off4:off5:off6:offnetwork0:off1:off2:on3:on4:on5:on6:offtestchkconfig0:off1:off2:on3:on4:on5:on6:off复制代码 使用chkconfiglist可查看当前加入开机自启动的服务列表,但如Note部分所述,该命令只显示SysV服务,不包含原生的systemd服务,查看systemd服务可使用systemctllistunitfiles命令。 以上三步,即可使roottestchkconfig。shdevnull2devnull命令在服务器系统启动时自动运行。 chkconfig的其它命令参考chkconfiglist表示查看所有服务在各个运行级别下的状态。chkconfigtestchkconfigon表示指定服务在所有的运行级别下都是开启状态。chkconfigtestchkconfigoff表示指定服务在所有的运行级别下都是关闭状态。chkconfiglevel5testchkconfigon表示指定服务在运行级别5图形模式的状态下开机启动服务。chkconfiglevel5testchkconfigoff表示指定服务在运行级别5图形模式的状态下开机不启动服务。复制代码三。自定义systemdservice方式 CentOS7的systemd服务脚本存放在:usrlibsystemdsystem(系统级)usrlibsystemduser(用户级)下,以。service结尾。这里以nginx为例 1、在usrlibsystemdsystem目录下创建nginx。service文件〔devusertestserver1〕sudovimusrlibsystemdsystemnginx。service〔Unit〕DescriptionnginxserverDocumentationhttp:nginx。orgendocs依赖服务,仅当依赖的服务启动之后再启动自定义的服务Afternetwork。targetremotefs。targetnsslookup。target〔Service〕启动类型,包括simple、forking、oneshot、notify、dbusTypeforkingpid文件路径PIDFilevarrunnginx。pid启动前执行的操作ExecStartPreusrlocalnginxsbinnginxtcusrlocalnginxconfnginx。conf启动命令ExecStartusrlocalnginxsbinnginxcusrlocalnginxconfnginx。conf重载命令ExecReloadusrlocalnginxsbinnginxsreload停止命令ExecStopusrlocalnginxsbinnginxsstop是否给服务分配独立的临时空间PrivateTmptrue〔Install〕服务安装的用户模式,一般使用multiuser即可WantedBymultiuser。target复制代码 其中Service部分的Type包括如下几种类型:simple:表示ExecStart启动的进程是该服务的主进程。如果它需要为其他进程提供服务,那么必须在该服务启动之前先建立好通信渠道,比如套接字,以加快后续单元的启动速度。forking:表示ExecStart进程将会在启动时使用fork()函数,这是传统Unix系统的做法,也就是说这个进程将由systemd进程fork出来,然后当该进程都准备就绪时,systemd进程退出,而fork出来的进程作为服务的主进程继续运行,对于此类型的进程,建议设置PIDFile选项,以帮助systemd准确定位该服务的主进程。oneshot:该进程会在systemd启动后续单元之前退出,适用于仅需要执行一次的程序。比如清理磁盘,你只需要执行一次,不需要一直在后台运行这个程序。notify:与simple类似,不同之处在于该进程会在启动完成之后通过sdnotify之类的接口发送一个通知消息。systemd在启动后续单元之前,必须确保该进程已经成功地发送了一个消息。dbus:该进程需要在DBus上获得一个由BusName指定的名称,systemd将会在启动后续单元之前,首先确保该进程已经成功获取了指定DBus名称。 2、开启开机自启动〔devusertestserver1〕sudosystemctlenablenginx。serviceCreatedsymlinkfrometcsystemdsystemmultiuser。target。wantsnginx。servicetousrlibsystemdsystemnginx。service。复制代码 以上两步,就将nginx服务配置成了在操作系统启动时自动启动。 其它命令参考sudosystemctlstartnginx。service启动sudosystemctlrestartnginx。service重启sudosystemctlreloadnginx。service重载sudosystemctlstopnginx。service停止sudosystemctlstatusnginx。service查看服务状态sudosystemctlcatnginx。service查看服务配置systemctllistunitfilesgrepnginx查看服务enabled状态sudosystemctldisablenginx。service关闭开机自启动sudojournalctlfunginx。service查看日志sudosystemctldaemonreload配置修改后,重新加载复制代码 根据以上配置,通过start启动nginx服务时,报PIDfilevarrunnginx。pidnotreadable(yet?)afterstart。的错误,启动失败,日志如下〔devusertestserver1〕sudojournalctlfunginx。serviceLogsbeginatWed2020032509:14:55CST。Mar2511:02:27testserver1nginx〔14144〕:nginx:configurationfileusrlocalnginxconfnginx。conftestissuccessfulMar2511:02:27testserver1systemd〔1〕:PIDfilerunnginx。pidnotreadable(yet?)afterstart。Mar2511:04:29testserver1systemd〔1〕:nginx。servicestartoperationtimedout。Terminating。Mar2511:04:29testserver1systemd〔1〕:Failedtostartnginx。Mar2511:04:29testserver1systemd〔1〕:Unitnginx。serviceenteredfailedstate。Mar2511:04:29testserver1systemd〔1〕:nginx。servicefailed。复制代码 从字面看是PID文件不可读,查看varrunnginx。pid,该文件也确实不存在,查看nginx。conf配置文件,发现是pidvarrunnginx。pid;这行配置被注释掉了,如果不指定pid文件位置,nginx默认会把pid文件保存在logs目录中。所以出现systemd启动服务时找不到pid文件而报错,将nginx。conf中的pid配置注释去掉,重启nginx。service即可。Ubuntu18。04的开机自启动配置 在Ubuntu18。04中,主要也是以systemd服务来实现开机自启动,systemd默认读取etcsystemdsystem下的配置文件,该目录下的一些文件会链接到libsystemdsystem下的文件。 因此可以在etcsystemdsystem目录下面创建一个自启动服务配置,以内网穿透服务frp客户端为例,如〔Unit〕DescriptionfrpcAfternetwork。targetWantsnetwork。target〔Service〕TimeoutStartSec30ExecStarthomedevuserappsfrpfrpcchomedevuserappsfrpfrpc。iniExecStopbinkillMAINPIDRestart1〔Install〕WantedBymultiuser。target复制代码 各配置项与CentOS类似。然后将服务器加到自启动列表中并启动服务sudosystemctlenablefrpcsudosystemctlstartfrpc复制代码 其它更多systemctl命令与CentOS类似。 也可以使用libsystemdsystemrclocal。service来执行一些开机需要执行的脚本,该文件内容为SPDXLicenseIdentifier:LGPL2。1Thisfileispartofsystemd。systemdisfreesoftware;youcanredistributeitandormodifyitunderthetermsoftheGNULesserGeneralPublicLicenseaspublishedbytheFreeSoftwareFoundation;eitherversion2。1oftheLicense,or(atyouroption)anylaterversion。Thisunitgetspulledautomaticallyintomultiuser。targetbysystemdrclocalgeneratorifetcrc。localisexecutable。〔Unit〕Descriptionetcrc。localCompatibilityDocumentationman:systemdrclocalgenerator(8)ConditionFileIsExecutableetcrc。localAfternetwork。target〔Service〕TypeforkingExecStartetcrc。localstartTimeoutSec0RemainAfterExityesGuessMainPIDno复制代码 从Description看它是为了兼容之前版本的etcrc。local的,该服务启动命名就是etcrc。localstart,将该文件链接到etcsystemdsystem下sudolnslibsystemdsystemrclocal。serviceetcsystemdsystemrclocal。service复制代码 创建etcrc。local文件,并赋予可执行权限vimetcrc。local!binbashechotestrcvartest。logsudochmodxetcrc。local复制代码完 作者:半路雨歌 链接:https:juejin。cnpost6844904104515338248