#!/bin/bash # chkconfig: - 90 10 # description: asr service #設置啟動文件 startup=/home/test/src/asr_server #設置相應的環境變量 export LD_LIBRARY_PATH=/home/test/lib:$LD_LIBRARY_PATH start(){ echo -n "Starting asr service:" RETVAL=`ps -ef |grep asr_serve[r] |awk -F " " '{print $2}'` if [ -n $RETVAL ];then $startup fi echo "asrserver is succeessfully started up" } stop(){ echo -n "Shutting down tomcat: " RETVAL=`ps -ef |grep asr_serve[r] |awk -F " " '{print $2}'` if [ -z $RETVAL ];then RETVAL=`kill -9 $RETVAL` fi echo "tomcat is succeessfully shut down." } status(){ RETVAL=`ps -ef |grep asr_serve[r] |awk -F " " '{print $2}'` if [ -n $RETVAL ];then echo "asrserver is running..." else echo "asrserver is stopped..." fi } restart(){ echo -n "restart asr service:" RETVAL=`ps -ef |grep asr_serve[r] |awk -F " " '{print $2}'` if [ -z $RETVAL ];then RETVAL=`kill -9 $RETVAL` fi $startup echo "asrserver is succeessfully started up" } case "$1" in start) start ;; stop) stop ;; status) status ;; restart) restart ;; *) echo $"Usage: $0 {start|stop|status|restart}" exit 1 esac
>>service xxxd start
env: /etc/init.d/xxxd: 沒有那個文件或目錄
報錯原因:文件格式可能不正確,有可能是dos文件,也可能文件中有\r等windows轉義字符
解決方案:
1.如果文件格式不正確,那么使用Notepad工具轉碼
2.判斷文件中是否有\r這種不可見轉義字符,可以把xxxd當做shell腳本執行,執行就會報錯"行4: $'\r': 未找到命令",
解決辦法
使用vi打開文本文件
vi xxxd
命令模式下輸入
:set fileformat=unix
:wq
# chkconfig: - 90 10
2345表示系統運行級別是2,3,4或者5時都啟動此服務,該項也可以設置為"-"表示默認
20,是啟動的優先級,80是關閉的優先級,
如果啟動優先級配置的數太小時如0時,則有可能啟動不成功,
因為此時可能其依賴的網絡服務還沒有啟動,從而導致自啟動失敗。
#"#chkconfig: - 90 10" 和 "#description: xxx"是必須的,否則在運行chkconfig --add auto_run時,會報錯,描述文字可以自定義。
Linux注冊系統服務步驟
1.編寫服務腳本
2.拷貝到/etc/init.d目錄下
3.為服務腳本添加可執行權限 >>chmod a+x xxxd 4.添加到系統服務中 >>chkconfig --add xxxd 5.檢測是否添加成功 >>chkconfig --list | grep xxxd
6.設置開機啟動 >>chkconfig xxxd on
刪除系統服務命令 >>chkconfig --del xxxd