將自己寫的腳本添加至開機自啓動服務和chkconfig的原理

將自己寫的腳本添加至開機自啓動服務和chkconfig的原理

創建腳本測試

[root@anuo ~]# vim anuo.sh  --創建個腳本
# !/bin/bash
# chkconfig: 35 53 88   --指定3和5級別啓動 53的啓動的順序    88是關閉的順序
# description: is anuo  --可以隨便說點啥, 最好的說明這個腳本的用途啥的。
echo Anuo Come on   --腳本的內容
[root@anuo ~]# mv anuo.sh /etc/init.d/      --必須將腳本放到/etc/init.d/目錄下
[root@anuo ~]# chmod +x /etc/init.d/anuo.sh     --別忘記給腳本加執行權限
[root@anuo ~]# chkconfig --add anuo.sh      --添加開機自啓動
[root@anuo ~]# chkconfig --list | grep anuo.sh      --可以看到開啓級別的啓動
anuo.sh         0:關閉    1:關閉    2:關閉    3:啓用    4:關閉    5:啓用    6:關閉

--這裏的3和5級別的啓動也就對應了上面的第一個35

小測試

[root@anuo ~]# ll /etc/rc.d/rc3.d/|grep anuo.sh     
lrwxrwxrwx  1 root root 17 5月  12 19:10 S53anuo.sh -> ../init.d/anuo.sh

--查看到3級別啓動的文件裏一個S53anuo.sh的鏈接文件(S表示開啓,53也就對應了配置文件裏的53的啓動的順序)

[root@anuo ~]# chkconfig anuo.sh off    --設置開機不啓動
[root@anuo ~]# ll /etc/rc.d/rc3.d/|grep anuo.sh     
lrwxrwxrwx  1 root root 17 5月  12 19:30 K88anuo.sh -> ../init.d/anuo.sh

--再次查看發現S53anuo.sh的鏈接文件沒有了,卻多了個K88anuo.sh的鏈接文件(K表示不開啓 88對應的是配置文件裏的88關閉的順序)

小結:要把腳本放到/etc/init.d/目錄下並給執行權限,當chkconfig設置開機自啓動時候會在相應的啓動級別的文件裏創建S開頭的鏈接文件,同時會刪除以K開頭的對應的鏈接文件,反之也亦然。(也可以手動刪除、創建鏈接文件也是一樣的效果)

精簡開機自啓動

方法1思路:找出需要關閉的的服務將其關閉

[root@anuo ~]# chkconfig --list|grep "3:on"|egrep -v "network|rsyslog|crond|sysstat|sshd"|awk '{print "chkconfig",$1,"off"}'|bash

[root@anuo ~]# chkconfig --list | grep "3:on"   --查看剩下開啓的
crond           0:off   1:off   2:off   3:on    4:off   5:on    6:off
network         0:off   1:off   2:off   3:on    4:off   5:on    6:off
rsyslog         0:off   1:off   2:off   3:on    4:off   5:on    6:off
sshd            0:off   1:off   2:off   3:on    4:off   5:on    6:off
sysstat         0:off   1:on    2:off   3:on    4:off   5:on    6:off

方法2思路:將所有服務全部關閉自啓動,再開啓需要開啓的服務。

[root@anuo ~]# LANG=en_SU.UTF-8     --調整字符集
[root@anuo ~]# echo $LANG
en_SU.UTF-8

[root@anuo ~]# for i in `chkconfig --list |grep "3:on" | awk '{print $1}'`;do chkconfig --level 2345 $i off ;done
[root@anuo ~]# chkconfig --list | grep "3:on"   --查看沒有就說明全部關閉成功
[root@anuo ~]# for i in network rsyslog crond sysstat sshd;do chkconfig --level 35 $i on;done
[root@anuo ~]# chkconfig --list | grep "3:on"   --查看開啓也成功了
crond           0:off   1:off   2:off   3:on    4:off   5:on    6:off
network         0:off   1:off   2:off   3:on    4:off   5:on    6:off
rsyslog         0:off   1:off   2:off   3:on    4:off   5:on    6:off
sshd            0:off   1:off   2:off   3:on    4:off   5:on    6:off
sysstat         0:off   1:on    2:off   3:on    4:off   5:on    6:off
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章