前言
ln :創(chuàng)建連接文件
- 默認(rèn)創(chuàng)建的是硬連接,好比復(fù)制 ,但是兩個文件會同步
命令:ln ./java/android/aa.txt aaa- s :創(chuàng)建的是軟連接變?yōu)橹赶?類似于windows的快捷方式:In -s
軟鏈接:
1.軟鏈接,以路徑的形式存在。類似于Windows操作系統(tǒng)中的快捷方式
2.軟鏈接可以 跨文件系統(tǒng) ,硬鏈接不可以
3.軟鏈接可以對一個不存在的文件名進(jìn)行鏈接
4.軟鏈接可以對目錄進(jìn)行鏈接
硬鏈接:
1.硬鏈接,以文件副本的形式存在。但不占用實(shí)際空間。
2.不允許給目錄創(chuàng)建硬鏈接
3.硬鏈接只有在同一個文件系統(tǒng)中才能創(chuàng)建
腳本模板
#!/bin/bash
# The next lines are for chkconfig on RedHat systems.
# chkconfig: 35 98 02
# description: Starts and stops xxx Server
# The next lines are for chkconfig on SuSE systems.
# /etc/init.d/xxx
#
### BEGIN INIT INFO
# Provides: xxx
# Required-Start: $network $syslog
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 6
# Short-Description: Starts and stops xxx Server
# Description: Starts and stops xxx Server
### END INIT INFO
case $1 in
start) # 服務(wù)啟動需要做的步驟
...
;;
stop) # 服務(wù)停止需要做的步驟
...
;;
restart) # 重啟服務(wù)需要做的步驟
...
;;
status) # 查看狀態(tài)需要做的步驟
...
;;
*) echo "$0 {start|stop|restart|status}"
exit 4
;;
esac
參考示例,其實(shí)寫一個服務(wù)腳本很簡單,只要滿足參數(shù)1為start|stop|restart|status的腳本都可以,注意上面以#開頭的那些行,分別滿足redhat和suse的要求,按照格式寫就行了
寫完后用chkconfig 服務(wù)名 on即可加入開機(jī)自啟動服務(wù)
例如:可運(yùn)行文件: qq.sh 內(nèi)容如下
num=3
#!/bin/bash
case $num in
1)
echo "num=1";;
2)
echo "num=2";;
3)
echo "num=3";;
4)
echo "num=4";;
*)
echo "defaul";;
esac
賦予可運(yùn)行權(quán)限:
chmod +x qq.sh
注意:運(yùn)行sh文件的方式
第一種: ./qq.sh 需要加./
第二種 sh qq.sh 不需要加./
一創(chuàng)建硬鏈接并運(yùn)行
1.創(chuàng)建一個目錄ww,并在ww目錄創(chuàng)建一個可運(yùn)行文件qq.sh的硬連接并運(yùn)行:
[rwwwt@iz2ze46xi6pjjj ~]# mkdir ww
[rwwwt@iz2ze46xi6pjjj ~]# cd ww
[rwwwt@iz2ze46xi6pjjj ww]# ls
[rwwwt@iz2ze46xi6pjjj ww]# ln ../qq.sh qq
[rwwwt@iz2ze46xi6pjjj ww]# ls
qq
[rwwwt@iz2ze46xi6pjjj69ailg9lz ww]# ./qq
num=3
二創(chuàng)建一個軟連接并運(yùn)行
創(chuàng)建軟連接 ln -s ../qq.sh qq.sh.link
運(yùn)行軟連接 ./qq.sh.link
[rwwwt@iz2ze46xi6pjjj ~]# mkdir ww
[rwwwt@iz2ze46xi6pjjj ~]# cd ww
[rwwwt@iz2ze46xi6pjjj ww]# ls
[rwwwt@iz2ze46xi6pjjj ww]# ln -s ../qq.sh qq.sh.link
[rwwwt@iz2ze46xi6pjjj ww]# ls -l
total 0
lrwxrwxrwx 1 root root 8 Oct 28 10:41 qq.sh.link -> ../qq.sh
[rwwwt@iz2ze46xi6pjjj ww]# ./qq.sh.link
num=3
[rwwwt@iz2ze46xi6pjjj ww]#
三. 將上面qq.sh進(jìn)行改造后可提取輸入?yún)?shù):
bash shell可根據(jù)參數(shù)位置獲取參數(shù)。通過 $1 到 $9 獲取第1到第9個的命令行參數(shù)。$0為shell名。如果參數(shù)超過9個,那么就只能通過${}來獲取了, 例如獲取第10個參數(shù),那么可以寫為${10}。
num=$1
#!/bin/bash
case $num in
1)
echo "num=1";;
2)
echo "num=2";;
3)
echo "num=3";;
4)
echo "num=4";;
*)
echo "defaul";;
esac
運(yùn)行原來的qq.sh
[rwwwt@iz2ze46xi6pjjj ww]# ./qq.sh 4
num=4
運(yùn)行軟連接
[rwwwt@iz2ze46xi6pjjj ww]# ./qq.sh.link 2
num=2
____linux通過ln與chkconfig設(shè)置開機(jī)啟動(第一個例子)____
第一步:我們在/etc/init.d或者/etc/rc.d/init.d下新建一個文件test(需要在root權(quán)限下操作)(/etc/init.d是/etc/rc.d/init.d的一個軟連接)
[rwwwt@iz2ze46xi6pjjj ww]# vim /etc/init.d/test //在init.d目錄下創(chuàng)建文件,輸入模式
復(fù)制上面的qq.sh文件內(nèi)容粘貼進(jìn)來
#!/bin/bash
num=$1
case $num in
1)
echo "num=1";;
2)
echo "num=2";;
3)
echo "num=3";;
4)
echo "num=4";;
*)
echo "defaul";;
esac
第二步:保存退出之后,給其增加可執(zhí)行權(quán)限
[rwwwt@iz2ze46xi6pjjj ww]# chmod +x /etc/init.d/test //保存
第三步:掛載(創(chuàng)建一個軟連接)
將 這個shell文件的link連到/etc/rc2.d/目錄下。linux的/etc/rcX.d/目錄中的數(shù)字代表開機(jī)啟動時不同的run level,也就是啟動的順序,Ubuntu9.10下有0-5六個level,不能隨便連到其他目錄下,可能在那個目錄中的程序啟動時Tomcat所需 要的一些庫尚未被加載,用ln命令將tomcat的鏈接鏈過去:sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S16Tomcat。rcX.d目錄下的命名規(guī)則是很有講究的,更具不同需要可能是S開頭,也可能是K開頭,之后的數(shù)字代表他們的 啟動順序,詳細(xì)看各自目錄下的Readme文件。
[rwwwt@iz2ze46xi6pjjj ww]# ln -s /etc/init.d/test /etc/rc2.d/S17Test
第四步:設(shè)置腳本開機(jī)自啟動
把這個腳本設(shè)置成系統(tǒng)啟動時自動執(zhí)行,系統(tǒng)關(guān)閉時自動停止,使用如下命令:
[rwwwt@iz2ze46xi6pjjj ww]# chkconfig --add test
第五步:驗(yàn)證:chkconfig命令:
參考:http://www.cnblogs.com/qlqwjy/p/7746419.html
chkconfig 選項(xiàng) | 說明 |
---|---|
--level levels | 指定一個運(yùn)行級別適合的操作。范圍為0-7。 |
--add name | 增加一個新的服務(wù)。 |
--del name | 刪除一個服務(wù) |
--list name | 顯示服務(wù)的情況 |
查看全部服務(wù):
[rwwwt@iz2ze46xi6pjjj ww]# chkconfig --list //輸入查看全部命令 顯示如下
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by nat
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
aegis 0:off 1:off 2:on 3:on 4:on 5:on 6:off
agentwatch 0:off 1:off 2:on 3:on 4:on 5:on 6:off
cloudmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
test 0:off 1:off 2:off 3:on 4:on 5:on 6:off
tomcat 0:off 1:off 2:off 3:on 4:on 5:on 6:off
啟動test服務(wù)
[rwwwt@iz2ze46xiz ~]# service test
num=3
[rwwwt@iz2ze46xi6pjjj69ailg9lz ~]# service test start
num=3
刪除test服務(wù):
[rwwwt@iz2ze46xiz ~]# chkconfig --del test
刪除后再次查看發(fā)現(xiàn)不存在service服務(wù)
----------------------------設(shè)置開機(jī)啟動服務(wù)(第二個例子)------------------------
參考:http://www.cnblogs.com/qlqwjy/p/7746419.html
第一步:
創(chuàng)建shell腳本: test2
vim /etc/init.d/test2</pre>
編輯腳本內(nèi)容如下:
#!/bin/sh
#chkconfig: 2345 80 90
#description:test2
case "$1" in
start) echo "start test2 service";;
stop) echo "stop test2 service";;
esac
** 第二步:**
賦予運(yùn)行權(quán)限:
chmod +x /etc/init.d/test2
測試腳本:
/etc/init.d/test2 start
start test2 service
也可以:(service自動尋找/etc/init.d目錄下)
service test2 start
start test2 service
解析:service是一個shell腳本,service test2 相當(dāng)于 /etc/init.d/test2 start
****第三步:(兩種方式:手動添加服務(wù)+chkconfig創(chuàng)建服務(wù) 原理都是創(chuàng)建軟連接)****
1.chkconfig創(chuàng)建服務(wù)
chkconfig --add test2
腳本的前三行如下:(腳本前面三行格式固定)
#!/bin/sh
#chkconfig: 2345 80 90
#description:test2
第一行,告訴系統(tǒng)使用的shell,所有的shell腳本都是這樣。
第 二行,chkconfig后面有三個參數(shù)2345,80和90告訴chkconfig程序,需要在rc2.d~rc5.d目錄下,創(chuàng)建名字為 S80test2的文件連接,連接到/etc/rc.d/init.d目錄下的的test2腳本。第一個字符是S,系統(tǒng)在啟動的時候,運(yùn)行腳 本test2,就會添加一個start參數(shù),告訴腳本,現(xiàn)在是啟動模式。同時在rc0.d和rc6.d目錄下,創(chuàng)建名字為K90test2的 文件連接,第一個字符為K,系統(tǒng)在關(guān)閉系統(tǒng)的時候,會運(yùn)行test2,添加一個stop,告訴腳本,現(xiàn)在是關(guān)閉模式。
注意上面的三行中,第二,第三行是必須的,否則在運(yùn)行chkconfig --add test2時,會報錯。
查看 /etc/rc2.d目錄下結(jié)構(gòu):(S80test2)****
S:代表啟動服務(wù)(運(yùn)行腳本傳入start參數(shù))
K:關(guān)閉服務(wù)(運(yùn)行腳本傳入stop參數(shù))
80; 運(yùn)行級別,越小越優(yōu)先運(yùn)行,優(yōu)先級相同的時候按創(chuàng)建時間啟動。
查看 /etc/rc3.d目錄下結(jié)構(gòu):(S80test2)
查看 /etc/rc0.d目錄下結(jié)構(gòu):(K90test2)
2.手動添加服務(wù):
在rc0.d-rc6.d目錄下分別創(chuàng)建文件連接。
ln -s /etc/rc.d/init.d/test2 /etc/rc.d/rc2.d/S99test2
ln -s /etc/rc.d/init.d/test2 /etc/rc.d/rc3.d/S99test2
ln -s /etc/rc.d/init.d/test2 /etc/rc.d/rc5.d/S99test2
ln -s /etc/rc.d/init.d/test2 /etc/rc.d/rc0.d/K01test2
ln -s /etc/rc.d/init.d/test2 /etc/rc.d/rc6.d/K01test2
Tips:/etc/rc[0~6].d其實(shí)是/etc/rc.d/rc[0~6].d的軟連接,主要是為了保持和Unix的兼容性才做此策
/etc/init.d/其實(shí)是/etc/rc.d/init.d/的軟連接。
第四步:驗(yàn)證
chkconfig --list
第五步:刪除服務(wù)
chkconfig --del test2
上述刪除服務(wù)的命令會刪除/etc/rc.d/rc[0-6].d下面的軟連接。
查看/etc/rc.d/rc3.d目錄:(test2的軟連接已刪除)
查看/etc/init.d目錄結(jié)構(gòu):(原來test2文件仍然存在)
【當(dāng)你用心寫完每一篇博客之后,你會發(fā)現(xiàn)它比你用代碼實(shí)現(xiàn)功能更有成就感!】