溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Linux系統(tǒng)中rc.local自啟動服務(wù)實(shí)例

發(fā)布時間:2021-09-02 19:44:01 來源:億速云 閱讀:288 作者:chen 欄目:系統(tǒng)運(yùn)維

本篇內(nèi)容介紹了“Linux系統(tǒng)中rc.local自啟動服務(wù)實(shí)例”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

linux有自己一套完整的啟動體系,抓住了linux啟動的脈絡(luò),linux的啟動過程將不再神秘。

本文中假設(shè)inittab中設(shè)置的init tree為:

/etc/rc.d/rc0.d
/etc/rc.d/rc1.d
/etc/rc.d/rc2.d
/etc/rc.d/rc3.d
/etc/rc.d/rc4.d
/etc/rc.d/rc5.d
/etc/rc.d/rc6.d
/etc/rc.d/init.d

目錄

1. 關(guān)于linux的啟動
2. 關(guān)于rc.d
3. 啟動腳本示例
4. 關(guān)于rc.local
5. 關(guān)于bash啟動腳本
6. 關(guān)于開機(jī)程序的自動啟動


1. 關(guān)于linux的啟動

init是所有進(jìn)程的頂層
init讀取/etc/inittab,執(zhí)行rc.sysinit腳本
(注意文件名是不一定的,有些unix甚至?xí)⒄Z句直接寫在inittab中)

rc.sysinit腳本作了很多工作:

init $PATH

config network

start swap function

set hostname

check root file system, repair if needed

check root space

....


rc.sysinit根據(jù)inittab執(zhí)行rc?.d腳本
linux是多用戶系統(tǒng),getty是多用戶與單用戶的分水嶺
在getty之前運(yùn)行的是系統(tǒng)腳本


2. 關(guān)于rc.d

所有啟動腳本放置在 /etc/rc.d/init.d下

rc?.d中放置的是init.d中腳本的鏈接,命名格式是:

S{number}{name}

K{number}{name}

S開始的文件向腳本傳遞start參數(shù)

K開始的文件向腳本傳遞stop參數(shù)

number決定執(zhí)行的順序


3. 啟動腳本示例

這是一個用來啟動httpd的 /etc/rc.d/init.d/apache 腳本:

代碼:

#!/bin/bash

......

可以看出他接受start,stop,restart,status參數(shù)

然后可以這樣建立rc?.d的鏈接:

代碼:

cd /etc/rc.d/init.d &&

ln -sf ../init.d/apache ../rc0.d/K28apache &&

ln -sf ../init.d/apache ../rc1.d/K28apache &&

ln -sf ../init.d/apache ../rc2.d/K28apache &&

ln -sf ../init.d/apache ../rc3.d/S32apache &&

ln -sf ../init.d/apache ../rc4.d/S32apache &&

ln -sf ../init.d/apache ../rc5.d/S32apache &&

ln -sf ../init.d/apache ../rc6.d/K28apache


4. 關(guān)于rc.local

經(jīng)常使用的 rc.local 則完全是習(xí)慣問題,不是標(biāo)準(zhǔn)。

各個發(fā)行版有不同的實(shí)現(xiàn)方法,可以這樣實(shí)現(xiàn):

代碼:

touch /etc/rc.d/rc.local

chmod +x /etc/rc.d/rc.local

ln -sf /etc/rc.d/rc.local /etc/rc.d/rc1.d/S999rc.local &&

ln -sf /etc/rc.d/rc.local /etc/rc.d/rc2.d/S999rc.local &&

ln -sf /etc/rc.d/rc.local /etc/rc.d/rc3.d/S999rc.local &&

ln -sf /etc/rc.d/rc.local /etc/rc.d/rc4.d/S999rc.local &&

ln -sf /etc/rc.d/rc.local /etc/rc.d/rc5.d/S999rc.local &&

ln -sf /etc/rc.d/rc.local /etc/rc.d/rc6.d/S999rc.local

5. 關(guān)于bash啟動腳本

/etc/profile

/etc/bashrc

~/.bash_profile

~/.bashrc

是bash的啟動腳本

一般用來設(shè)置單用戶的啟動環(huán)境,也可以實(shí)現(xiàn)開機(jī)單用戶的程序,但要明確他們都是屬于bash范疇而不是系統(tǒng)范疇。

他們的具體作用介紹如下:

/bin/bash這個命令解釋程序(后面簡稱shell)使用了一系列啟動文件來建立一個運(yùn)行環(huán)境:
/etc/profile

/etc/bashrc

~/.bash_profile

~/.bashrc

~/.bash_logout

每一個文件都有特殊的功用并對登陸和交互環(huán)境有不同的影響。

/etc/profile 和 ~/.bash_profile 是在啟動一個交互登陸shell的時候被調(diào)用。

/etc/bashrc 和 ~/.bashrc 是在一個交互的非登陸shell啟動的時候被調(diào)用。

~/.bash_logout 在用戶注銷登陸的時候被讀取

一個交互的登陸shell會在 /bin/login 成功登陸之后運(yùn)行。一個交互的非登陸shell是通過命令行來運(yùn)行的,如[prompt]$/bin/bash。一般一個非交互的shell出現(xiàn)在運(yùn)行 shell腳本的時候。之所以叫非交互的shell,是因?yàn)樗辉诿钚猩系却斎攵皇菆?zhí)行腳本程序。

6. 關(guān)于開機(jī)程序的自動啟動

系統(tǒng)腳本可以放置在/etc/rc.d/init.d中并建立/etc/rc.d/rc?.d鏈接,也可以直接放置在/etc/rc.d/rc.local中。

init.d腳本包含完整的start,stop,status,reload等參數(shù),是標(biāo)準(zhǔn)做法,推薦使用。

為特定用戶使用的程序(如有的用戶需要使用中文輸入法而有的不需要)放置在~/中的bash啟動腳本中。

========================================================================
設(shè)置系統(tǒng)自動啟動
在/etc/init.d/下創(chuàng)建smsafe文件

內(nèi)容:
#!/bin/bash
# chkconfig: 35 95 1
# description: script to start/stop smsafe
case $1 in
start)
sh /opt/startsms.sh
;;
stop)
sh /opt/stopsms.sh
;;
*)
echo "Usage: $0 (start|stop)"
;;
esac
更改權(quán)限
# chmod 775 smsafe

加入自動啟動
# chkconfig –add smsafe

查看自動啟動設(shè)置
# chkconfig –list smsafe

smsafe 0:off 1:off 2:off 3:on 4:off 5:on 6:off

以后可以用以下命令啟動和停止腳本
# service smsafe start 啟動

# service smsafe stop 停止

=======================================================================
jira 的啟動主要依靠的是bin目錄下的catalina.sh腳本,提供了如init腳本的start,stop等參數(shù)

#!/bin/bash

#

# chkconfig: 2345 85 15

# description: jira

# processname: jira

# source function library

. /etc/init.d/functions

#下面一行比較重要,為jira的安裝路徑,沒有的話,將會提示找不到文件

CATALINA_HOME="/var/www/jira"

 

RETVAL=0

start() {

echo -n $"Starting jira services: "

 

. /var/www/jira/bin/catalina.sh start

RETVAL=$?

echo

}

stop() {

echo -n $"Shutting down jira services: "

. /var/www/jira/bin/catalina.sh stop

RETVAL=$?

echo

}

case "$1" in

start)

start

;;

stop)

stop

;;

restart|reload)

stop

start

;;

status)

status jira

RETVAL=$?

;;

*)

echo $"Usage: $0 {start|stop|restart|status}"

exit 1

esac

exit $RETVAL

 

-------------------------------

保存為/etc/init.d/jira

然后利用chkconfig --add jira

OK

啟動/etc/init.d/jira start

停止/etc/init.d/jira stop

=======================================================================

(以Websphere為例子)

1. 在/etc/rc.d/init.d目錄下新建啟動腳本startWebsphere,鍵入以下內(nèi)容:
#!/bin/sh
/opt/WebSphere/AppServer/bin/startServer.sh server1

修改該文件的權(quán)限:
chmod 755 startWebsphere

2. 在對應(yīng)的目錄下建立軟連接(假設(shè)系統(tǒng)默認(rèn)進(jìn)入X11)
cd /etc/rc.d/rc5.d
ln -s ../init.d/startWebsphere S99startWebsphere

3. 重啟系統(tǒng)即可

=======================================================================
linux下oracle的自啟動腳本

1.寫一個StartOracle.sql,假設(shè)放在/目錄下
vi /StartOracle.sql加入如下兩行保存
startup
exit

2.配置/etc/rc.local
vi /etc/rc.local加入如下內(nèi)容,保存
su - oracle -c '$ORACLE_HOME/bin/lsnrctl start'
su - oracle -c '$ORACLE_HOME/bin/sqlplus "/as sysdba" @/StartOracle.sql'

3. 如果還要自動啟動oracle enterprise manager(em)和isqlplus可以如下配置
vi /etc/rc.local 加入:
su - oracle -c '$ORACLE_HOME/bin/emctl start dbconsole'
su - oracle -c '$ORACLE_HOME/bin/isqlplusctl start'

要知道em和isqlplus等使用的端口可以查詢文件:
$ORACLE_HOME/install/portlist.ini(以oracle 10.1.0.3為例)

=======================================================================
#root命令行下直接綁定演示:
arp -s 192.x.x.x. 00:ea:se綁定.
arp -d 刪除
arp -f 批量導(dǎo)入

“Linux系統(tǒng)中rc.local自啟動服務(wù)實(shí)例”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI