方式一:
可以借助 /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
參考: