歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux管理 >> Linux服務

Linux服務自動啟動功能

以下是我整理的一些有關"Linux服務自動啟動功能"方法,供大家參考。
setup 、rc.local 和chkconfig三種方式都可以設置
第一種)
輸入#setup指令進入系統服務菜單,選擇你想啟動的服務比如httpd,然後重起機器或者 /etc/rc.d./init.d/httpd   start
第二種)
把啟動命令放到/etc/rc.d/rc.local文件裡這樣就可以每次啟動的時候自動啟動服務了,例如對於 apache,編譯好apache後會在安裝目錄的bin下生成apachectl文件,這是個啟動教本,我們只需要把這個命令加到rc.local裡就可以了
echo /usr/local/apache/bin/apachectl >> /etc/rc.d/rc.local,
設置服務自動啟動的方式是在rc.local裡還可以加入類似以下的一些腳本:
#sshd
/usr/local/sbin/sshd
#proftpd
/usr/local/sbin/proftpd
#apache
/home/apache/bin/apachectl start
#mysql
/home/mysql/bin/safe_mysqld --port=3306 &
#start Oracle8i listener first
su - oracle -c 'lsnrctl start'
#start oracle8i
su - oracle -c 'dbstart'
第三種)
通過chkconfig指令.
使用chkconfig命令來把某項服務加到系統的各項運行級別中,步驟如下,
1 創建啟動腳本.
   對於apache,mysql,ssh這樣的軟件都是自己帶的,我們只要稍微修改一下使之支持chkconfig就可以了
2 修改腳本
我們需要在腳本的前面加上一下2行,才能支持chkconfig命令
# chkconfig: 2345 08 92
#
# description: Automates a packet filtering firewall with ipchains.
#
chkconfig:後面定義的使啟動服務的運行級別(例子中使2345啟動改服務),以及關閉和啟動服務的順序,(上例中關閉服務的順序使8,啟動的順序使92)
descriptions:對改服務的描述(上例中是ipchains包過濾),你可以換成自己想要的
修改好之後執行
cp 你的腳本 /etc/rc.d/init.d/腳本名
chmod 700 /etc/rc.d/init.d/腳本名
chkconfig --add 腳本名
例如:
將其加入Linux啟動過程,僅在level 3, level 5級別下運行
[root@Tester init.d]/sbin/chkconfig --add apache-httpd
[root@Tester init.d]/sbin/chkconfig --level 35 apache-httpd on
之後就可以了,以後每次重新啟動服務器都會自動啟動和關閉我們的服務了
下面是網路上一些創建的APACHE啟動腳本案例,可以參考一下:
Linux下設置apache httpd服務為自動啟動的實現:
系統環境:          Red Hat ENTERPRISE AS 4
Apache Httpd 版本: 2.0.61
Apache 安裝路徑:   /usr/local/apache-httpd-2.0.61
Step 1:
[buck@Tester local]ln -s /usr/local/apache-httpd-2.0.61 apache-httpd-default
Step 2:
切換到超級用戶,進入/etc/init.d/,創建啟動腳本
[root@Tester init.d]vi appache-httpd
#!/bin/bash
#
# created by Buck Zhang, 2007/12/02
#
# apache-httpd    Startup script. for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
#           HTML files and CGI.
# processname: httpd
# config: /usr/local/apache-httpd-default/conf/httpd.conf
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Path to the apachectl script, server binary, and short-form. for messages.
apahectl=/usr/local/apache-httpd-default/bin/apachectl
httpd=${HTTPD-/usr/local/apache-httpd-default/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure. So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}
# When stopping httpd a delay of >10 second is required before SIGKILLing the
# httpd parent; this gives enough time for the httpd parent to SIGKILL any
# errant children.
stop() {
    echo -n $"Stopping $prog: "
    killproc -d 10 $httpd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=$?
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        killproc $httpd -HUP
        RETVAL=$?
    fi
    echo
}
# See how we were called.
case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
status)
        status $httpd
    RETVAL=$?
    ;;
restart)
    stop
    start
    ;;
condrestart)
    if [ -f ${pidfile} ] ; then
        stop
        start
    fi
    ;;
reload)
        reload
    ;;
graceful|help|configtest|fullstatus)
    $apachectl $@
    RETVAL=$?
    ;;
*)
    echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
    exit 1
esac
exit $RETVAL
Copyright © Linux教程網 All Rights Reserved