CentOS 開機自啟動指令碼

2022-07-29 12:36:11 字數 1270 閱讀 9478

開機時執行自己的指令碼.

1.編寫自己的服務指令碼

進入系統服務指令碼目錄:

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

vi test

內容如下:

#!/bin/bash

## chkconfig: - 57 75

# description: test service

start()

stop()

# see how we were called.

case "$1" in

start)

start

;; stop)

stop

;; restart|force-reload)

stop

start

;; *)

echo $"usage: $0 "

exit 2

esac

要注意的是, 檔案的格式要是 unix. 可以通過 vi 命令 :set ff? 檢視.如果不是, 執行的時候會報錯:

/bin/bash^m: bad interpreter

:set fileformat=unix --> 設定編碼為unix(還可以設定為 doc)

vi 中儲存

將該指令碼設定為可執行:

chmod +x test

可以看到. 啟動服務的時候會輸出一行字. 然後往 /tmp/sunyu.txt 中寫入乙個 3. 關閉時會往檔案中寫入乙個 2 如果該檔案不存在, 請先行自己在 /tmp 下新建: 

cd /tmp/

touch sunyu.txt

然後將檔案設定為可寫:

chmod 777 sunyu.txt

2.新增系統服務

chkconfig --add test

3.系統服務自啟動

chkconfig --level 345 test on

4.測試

service test start

然後檢視 /tmp/sunyu.txt 中的內容: cat /tmp/sunyu.txt

service test stop

然後再檢視上面檔案中的內容

重啟:reboot

再檢視 /tmp/sunyu.txt 檔案中的內容.如果看到變化表示服務新增成功.

此例通過後, 可在指令碼中的 start stop 中做自己想做的其它事了.

CentOS 開機自啟動指令碼

開機時執行自己的指令碼.1.編寫自己的服務指令碼 進入系統服務指令碼目錄 cd etc rc.d init.d vi test 內容如下 bin bash chkconfig 57 75 description test service start stop see how we were call...

CentOS 開機自啟動指令碼

開機時執行自己的指令碼.1.編寫自己的服務指令碼 進入系統服務指令碼目錄 cd etc rc.d init.d vi test 內容如下 bin bash chkconfig 57 75 description test service start stop see how we were call...

centos設定開機自啟動指令碼

以前直接在 etc rc.local中寫入命令可執行,現在發現不起作用。mount中內容 bin bash chkconfig 2345 80 90 description mount mount t cifs o username administrator password uid 1002,g...