CentOS7 设置自启动 – 记忆角落

CentOS7 设置自启动

/ 0评 / 0

CentOS7 设置自启动

在CentOS 6中,可以通过在/etc/init.d/添加自启动脚本来设置自启动;

在CentOS 7中,设置自启动的方法发生了改变:虽然我们仍然可以使用脚本来添加自启动,但是官方更建议使用服务来设置自启动

通过配置系统服务来设置自启动

以Docker为例:

安装完二进制的Docker之后,把Docker服务设置成自启动:

进入/etc/systemd/system/目录,并创建docker.service文件

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
# ExecStart=/usr/bin/dockerd --selinux-enabled=false --insecure-registry=ip
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

其中:

给docker.service文件添加执行权限

chmod +x /etc/systemd/system/docker.service

重新加载配置文件:systemctl daemon-reload

通过自启动脚本来设置

echo "source /etc/profile && cd /usr/local/src/emqx/bin && sudo ./emqx start"  >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

在CentOS 7中/etc/rc.d/rc.local文件的权限被降低了,开机的时候执行的脚本是不能启动一些服务的,执行下面的命令将文件标记为可执行的文件:

虽然两种方法都可以设置自启动,但是不建议通过脚本来设置,建议通过设置服务来启动;

发表评论

您的电子邮箱地址不会被公开。