Centos7-开机自启动服务设置
方式一:
可以借助 /etc/rc.d/rc.local 文件做一个开机自启动服务的功能,我们测试一个开启自动同步服务器时间的功能
1、root看一下rc.local是否有执行权限,没有的话 chmod u+x /etc/rc.d/rc.local 添加执行权限
2、新建一个自己要启动命令的脚本文件 vim /usr/local/myscripts/test.sh
#! /bin/bash yum info ntp && ntpdate cn.ntp.org.cn
3、给这个文件添加执行权限 chmod u+x /usr/local/myscripts/test.sh
4、把这个脚本文件的路径添加到 rc.local 中
#!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local /usr/local/myscripts/test.sh
5、重启系统 reboot
方式二:
通过chkconfig来配置开启自启
1、一定要在 /etc/init.d 这个目录下新建一个文件 test2.sh
2、编辑 test2.sh 文件 vim test2.sh
#! /bin/bash #chkconfig 2345 88 89 #description:auto_run #开机自启动同步时间 yum info ntp && ntpdate cn.ntp.org.cn
3、给 test2.sh 添加权限 chmod u+x test2.sh
4、添加到服务 chkconfig --add /etc/init.d/test2.sh
参考: