Centos 7.x 系统支持两种方式自定义开机启动:
- 第一种:systemd服务(推荐)
- 第二种:init.d启动脚本
方法一、systemd服务
systemd service通过systemctl命令管理。语法:
systemctl [enable disable start stop restart] 服务名称
- 服务脚本所在位置:
/usr/lib/systemd/system
- 服务脚本文件命名格式为:
xxx.service
- 脚本模板为:
[Unit]
Description=服务功能说明
After=syslog.target
[Service]
Type=forking
ExecStart=自定义start脚本
ExecReload=自定义restart脚本
ExecStop=自定义stop脚本
[Install]
WantedBy=multi-user.target
- 编写自定义服务shell脚本
举例:开机自启动tomcat
- 创建service配置文件:
cd /usr/lib/systemd/system
vim mes.service
内容为:
[Unit]
Description=YT mes application
After=syslog.target
After=network.target
[Service]
Type=forking
ExecStart=/opt/mes-application/bin/startup.sh
ExecStop=/opt/mes-application/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
- 加载systemd服务
systemctl daemon-reload
- enable服务
systemctl enable mes
- 启动服务
systemctl start mes
image.png
方法二、init.d启动脚本
使用chkconfig命令管理脚本,语法为:
chkconfig [--add][--del][--list][系统服务]
chkconfig [--level <等级代号>][系统服务][on/off/reset]
- 脚本所在位置:
/etc/rc.d/init.d
- 脚本模板为:
#!/bin/bash
# chkconfig: 2345 90 90
# description: start mes application
自定义脚本内容
2345
表示linux init级别(0-6),第一个90
表示start优先级,数值越小优先级越高,第二个90
表是stop优先级,数值越小优先级越高。
举例:开机自启动tomcat
- 进入 /etc/rc.d/init.d,创建启动脚本
cd /etc/rc.d/init.d
vim start-mes.sh
内容为:
#!/bin/bash
# chkconfig: 2345 90 90
# description: start mes application
systemctl stop firewalld
/opt/mes-application/bin/startup.sh
chmod +x start-mes.sh
- 添加开机服务
chkconfig --add start-mes.sh
chkconfig start-mes.sh on
- 检查一下启动状态
chkconfig --list
image.png