设置开机自启动方法总结
查看命令
- 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 之后采用 https://baike.baidu.com/item/systemd/18473007 作为 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.cnblogs.com/startcentos/p/6147444.html
来源: http://www.bubuko.com/infodetail-3061615.html