CentOS7添加开机启动脚本
方法一、通过rc.local文件(不推荐)
- 编写需要开机启动的脚本,并赋予其可执行权限
vim /root/deploy.sh
chmod +x /root/deploy.sh
- 在
rc.local文件末尾添加下面配置,开机时会加载运行rc.local文件
/root/deploy.sh
- 为
rc.local文件添加可执行权限
chmod +x rc.local
- 注意
rc.local文件在CentOS7中,是为了兼容之前版本才存在的,所以默认不具有执行权限- 与以前的系统版本不同的是,当前版本使用Systemd来启动系统服务,使得在引导期间
rc.local会与其他服务并行执行,也就是说这个脚本不会在所有其他服务之后运行。
方法二、通过systemd注册为系统服务(推荐)
- 在
/etc/systemd/user/目录下新建service文件,以nginx.service为例。(用户Unit文件/etc/systemd/user,系统Unit文件/etc/systemd/system,包管理器Pacman的Unit文件/usr/lib/systemd/system)
cd /etc/systemd/user/
vim nginx.service
- 添加以下内容
[Unit]
Description=nginx service
Documentation=https://www.xxxxx.com/documentation
#Requires=
#Before=your_application.service another_example_application.service
Wants=network.target network-online.target
After=network.target network-online.target
#AssertPathExists=/var/lib/xxx
[Service]
#Type指定服务的启动类型,必须为simple, exec, forking, oneshot, dbus, notify, idle 之一。常用simple和forking。
Type= forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
# 设为 true表示在进程的文件系统名字空间中挂载私有的 /tmp 与 /var/tmp 目录, 也就是不与名字空间外的其他进程共享临时目录。 这样做会增加进程的临时文件安全性,但同时也让进程之间无法通过 /tmp 或 /var/tmp 目录进行通信。
# 适用于web系统服务,不适用于mysql之类的数据库用户服务,数据库用户服务设为false。
PrivateTmp=true
Restart=on-failure
RestartSec=60s
#[Install]部分:指定服务的启用信息,只有在systemctl的enable与disable命令在启用/停用单元时才会使用此部分。
[Install]
# “WantedBy=multi-user.target”表示当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行。
WantedBy=multi-user.target
- 接着可以使用各种服务操作
systemctl daemon-reload # 首先重载systemd配置
systemctl enable nginx.service # 设置开机自启动
systemctl disable nginx.service # 停止开机自启动
systemctl start nginx.service # 启动nginx服务
systemctl status nginx.service # 查看服务当前状态
systemctl stop nginx.service # 停止服务
systemctl restart nginx.service # 重新启动服务
systemctl list-units --type=service # 列出指定类型的配置文件
systemctl list-unit-files # 列出所有配置文件
systemctl list-dependencies nginx.service # 显示 Unit 的所有依赖(不包括Target 类型)
systemctl list-dependencies --all nginx.service # 显示 Unit 的所有依赖
方法三、通过chkconfig注册为系统服务
- 在
init.d目录下创建需要开机启动的脚本,并添加可执行权限
cd /etc/rc.d/init.d
vim deploy
chmod +x deploy
- 使用该方法启动脚本,必须加入下面这样的注释头,否则将提示
service deploy does not support chkconfig
#!/bin/bash
# chkconfig: 2345 90 10
# description: deploy script
- 例子:NGINX Init Script
- 其第二行含义:chkconfig: <Linux运行级别> <启动优先级> <停止优先级>
- 启动脚本中要实现POSIX规范中的函数:
start()、stop()、restart()等,此后即可使用service xxxx(脚本名) start/stop等命令启动或关闭服务
- 操作命令
chkconfig --add deploy
#在chkconfig工具服务列表中增加此服务,此时服务会被在/etc/rc.d/rcN.d中赋予K/S入口(即在相应运行级rcX.d中创建链接);
chkconfig deploy on # 配置系统启动时该脚本默认启动
chkconfig deploy off # 配置系统启动时该脚本默认关闭
chkconfig --list # shows SysV services
mv ./mysql/support-files/mysql.server /etc/init.d/mysql57
chmod +x /etc/init.d/mysql57
chkconfig --add mysql57
chkconfig --level 345 mysql on
chkconfig --list
chkconfig --del mysql57
附:参考链接
chkconfig命令
https://man.linuxde.net/chkconfigLinux系统7个运行级别图文详解
https://www.linuxidc.com/Linux/2018-07/153129.htmSystemd 入门教程:实战篇 –阮一峰
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.htmlSystemd 入门教程:命令篇
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html可能是史上最全面易懂的 Systemd 服务管理教程! https://cloud.tencent.com/developer/article/1516125