背景
Linux服务器存在重启的情况,服务器重启后原来在跑的服务就停止了,由于服务器重启是个随机事件,因此需要配置在重启后自动开启一些服务。
本篇文章对于Linux发行版Debian和Red Hat。
实现
1、配置启动脚本
进入目录: /etc/init.d/ ,编写要开机启动的脚本 custom-service.sh
#!/bin/sh
#chkconfig: 345 95 95
#description: custom-service
RDS_HOME=/root/app/rds
sh $RDS_HOME/bin/startup.sh
2、使用chkconfig命令配置添加custom-service.sh至自启动服务
[root@localhost init.d]# chkconfig --add custom-service.sh
[root@localhost init.d]# chkconfig custom-service.sh on
[root@localhost init.d]# chkconfig custom-service.sh --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
custom-service.sh 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@localhost init.d]#
3、修改脚本custom-service.sh文件权限
[root@localhost init.d]# chmod 777 custom-service.sh