一、通過rc.local該文件實現開機自啟
1:編寫測試腳本
[root@host1 ~]#
vim test.sh
#!/bin/bash
/bin/echo $(/bin/date +%F_%T) >> /tmp/test.log
##開機啟動打印當前時間輸出到test.log文本里
2:測試腳本完成之后,更改rc.local配置文件
[root@host1 ~]#
vim /etc/rc.d/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
/bin/bash /tmp/test.sh >/dev/null 2>/dev/null
##加上這一段配置,讓其開機執行
保存退出
3:在centos7中,/etc/rc.d/rc.local沒有執行權限,需要授權,然后就可以重啟機器驗證就行了
[root@host1 ~]#
chmod +x /etc/rc.d/rc.local
4:重啟之后查看結果
[root@host1 ~]#
cat /tmp/test.log
2019-06-02_16:44:53
二、通過chkcongfig開機啟動服務來實現
1:在/etc/init.d/編輯一個測試腳本
[root@host1 ~]#
vim /etc/init.d/test
#!/bin/bash
# chkconfig: 3 88 88
/bin/bash /tmp/test.sh >/dev/null 2>/dev/null
保存退出
2:賦予執行權限
[root@host1 ~]#
chmod 755 /etc/init.d/test
3:加入開機啟動服務列表
[root@host1 ~]#
chkconfig --add test
4:查看開機啟動服務列表
[root@host1 ~]#
chkconfig --list
注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。
要列出 systemd 服務,請執行 'systemctl list-unit-files'。
查看在具體 target 啟用的服務請執行
'systemctl list-dependencies [target]'。
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
test 0:關 1:關 2:關 3:開 4:關 5:關 6:關
5:重啟系統之后查看結果
[root@host1 ~]#
cat /tmp/test.log
2019-06-02_16:44:53
2019-06-02_16:48:45