linux 設置開機啟動項兩種方式


有時候我們需要Linux系統在開機的時候自動加載某些腳本或系統服務。

在解問題之前先來看看Linux的啟動流程

Linux的啟動流程

Linux啟動順序

主要順序就是: 
1. 加載內核 
2. 啟動初始化進程 
3. 確定運行級別 
4. 加載開機啟動程序 
5. 用戶登錄

啟動流程的具體細節可以看看Linux 的啟動流程 

第4步加載啟動程序其實是兩步:

  1. init進程逐一加載開機啟動程序,其實就是運行指定目錄里的啟動腳本。
  2. 在運行完指定目錄里面的程序后init進程還會去執行/etc/rc.local 這個腳本。

ps:“指定目錄”是指在第3步中設置的運行級別對應的目錄。

要完成我們的需求,我們使用第4步中的任意一種方式都可以。

下面分別就是這兩種方式的具體實現:

1.chkconfig

以supervisord服務腳本為例:

#!/bin/sh
##
## /etc/rc.d/init.d/supervisord
##
#supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord

# Source init functions
. /etc/rc.d/init.d/functions

prog="supervisord"
prefix="/usr/"
exec_prefix="${prefix}"
PIDFILE="/var/run/supervisord.pid"
CONFIG="/etc/supervisord.conf"
prog_bin="${exec_prefix}bin/supervisord -c $CONFIG "

function log_success_msg() {
        echo "$@" "[ OK ]"
}

function log_failure_msg() {
        echo "$@" "[ OK ]"
}

start()
{
       #echo -n $"Starting $prog: "
       #daemon $prog_bin --pidfile $PIDFILE
       #[ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog failed"
       #echo
        if [ ! -r $CONFIG ]; then
                log_failure_msg "config file doesn't exist (or you don't have permission to view)"
                exit 4
        fi

        if [ -e $PIDFILE ]; then
                PID="$(pgrep -f $PIDFILE)"
                if test -n "$PID" && kill -0 "$PID" &>/dev/null; then
                        # If the status is SUCCESS then don't need to start again.
                        log_failure_msg "$NAME process is running"
                        exit 0
                fi
        fi

        log_success_msg "Starting the process" "$prog"
        daemon $prog_bin --pidfile $PIDFILE
        log_success_msg "$prog process was started"

}
stop()
{
       echo -n $"Shutting down $prog: "
       [ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown"
       echo
}

case "$1" in

 start)
   start
 ;;

 stop)
   stop
 ;;

 status)
       status $prog
 ;;

 restart)
   stop
   start
 ;;

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

esac

  第1步:把上面的腳本放在/etc/init.d/文件夾下。

ln -s ./supervisord  /etc/init.d/supervisord

  第2步:將啟動腳本權限改為可執行。

chmod a+x /etc/init.d/supervisord

  第3步:添加啟動項。

chkconfig --add supervisord
chkconfig supervisord on

  第4步:檢查是否設置成功。

chkconfig --list | grep supervisord
supervisord     0:關閉    1:關閉    2:啟用    3:啟用    4:啟用    5:啟用    6:關閉

成功~

2.修改/etc/rc.local腳本

/etc/rc.local 腳本內容如下

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

#touch /var/lock/subsys/local
echo "hello linux" >> /tmp/hello2.log

influxd > /tmp/influxd.log 2>&1 &

echo "hello linux" >> /tmp/hello3.log

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM