Linux设置服务自启

设置开机自启动方法总结


查看命令

chkconfig --list #列出所有的系统服务。CentOS6之前

systemctl list-units --all --type=service #查看所有服务。CentOS6之后
systemctl list-units --type=service #查看所有已经启动的服务。CentOS6之后

设置自启动服务

systemctl enable servicename
systemctl start servicename

设置自启动脚本

/etc/rc.d/rc.local


方法

/etc/rc.d/rc.local 或者其链接文件 /etc/rc.local 文件中添加启动命令


问题

在CentOS7之后可能无法自启动


原因和解决方案

CentOS7之后采用systemd作为init,rc.local也是通过systemd的service启动的。

输入

cat /usr/lib/systemd/system/rc-local.service

查看其启动配置

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.d/rc.local is executable.
[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes

其中发现启动rc.local是通过命令 ExecStart=/etc/rc.d/rc.local start 实现的,因此 /etc/rc.d/rc.local 必须得有执行权限。

若其没有执行权限,应给予。

chmod +x /etc/rc.d/rc.local

之后则需要启动rc-local.service服务

systemctl enable rc-local.service

systemctl start rc-local.service

若未能成功启动

systemctl enable rc-local.service

The unit files have no [Install] section. They are not meant to be enabled

using systemctl.

Possible reasons for having this kind of units are:

1) A unit may be statically enabled by being symlinked from another unit's

.wants/ or .requires/ directory.

1) A unit's purpose may be to act as a helper for some other unit which has

a requirement dependency on it.

1) A unit may be started when needed via activation (socket, path, timer,

D-Bus, udev, scripted systemctl call, ...).

提示启动service里面没有install这节的内容。那就给它通过多用户的target启动就可以了。

vim /usr/lib/system/system/rc-local.service

[Install]

WantedBy=multi-user.target

然后再次enable启动服务:

systemctl enable rc-local.service

ln -s '/lib/systemd/system/rc-local.service' '/etc/systemd/system/multi-user.target.wants/rc-local.service'

以上解决方法摘录自网络,systemd的原理我并不了解


扩展

启动脚本可以写成服务然后用systemd添加到自启动,不一定非要添加到rc.local


/etc/init.d/


方法

此方法貌似是sysv的init使用,systemd行不行有待试验

将脚本拷贝至 /etc/init.d/ 路径下,或者创建需要的脚本文件即可

之后增加可执行权限

chmod +x /etc/init.d/scriptname.sh

最后添加自启动

chkconfig --add scriptname.sh
chkconfig scriptname.sh on

最后一步是不是可以用systemctl enable scripname.sh我没试过

查到还有一种方法是先在/etc/profile.d/创建脚本之后添加可执行权限再拷贝至/etc/init.d/,之后的流程相同。


参数

chkconfig方法的参数设置

#!/bin/sh
表明此脚本使用/bin/sh来解释执行

#chkconfig: 2345 80 90
2345表示系统运行级别
0——关机,
1——单用户,就是我们之前修改root账户密码的模式,
2——多用户模式,但比3模式少了一个nfs服务
3——多用户命令行模式,最常用
4——保留级别暂时没用,
5——图形模式,
6——重启
80表示启动优先级
90表示关闭优先级

#description:xxxxxxxxx
脚本的描述信息

systemctl方法的参数设置(以nginx为例)

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

[Unit]服务的说明

Description:描述服务

After:描述服务类别

[Service]服务运行参数的设置

Type=forking是后台运行的形式

ExecStart为服务的具体运行命令

ExecReload为重启命令

ExecStop为停止命令

PrivateTmp=True表示给服务分配独立的临时空间

注意:[Service]的启动、重启、停止命令全部要求使用绝对路径。[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3。


参考资料

https://www.liangzl.com/get-article-detail-15933.html

https://baijiahao.baidu.com/s?id=1593743216505421871&wfr=spider&for=pc

https://www.cnblogs.com/startcentos/p/6147444.html

https://blog.csdn.net/abcwanglinyong/article/details/84638125

猜你喜欢

转载自www.cnblogs.com/tajangbay-zkr-NLP/p/10878966.html