樹莓派:最好的安排

來源:互聯網
上載者:User

標籤:exec   mil   使用者模式   script   power   option   運行   use   config   

利用cron來開機啟動 

 

利用/etc/init.d開機啟動

在樹莓派下的/etc/init.d檔案夾下有很多指令碼,比如我們已經瞭解了的cron。這個cron指令碼把cron這個守護進程封裝成了一個服務,定義了它在啟動、重啟和終止時的具體行為。這樣,使用者在啟用相應的服務時,就不需要進行太複雜的設定。當服務終止時,作業系統也能根據指令碼的定義,自動地回收相關資源。使用者還能把重要的服務設定成開機啟動,省了手動開啟的麻煩。因此,我們在/etc/init.d中內看到很多默默工作的服務,如ssh、bluetooth、rsync等。

 

服務指令碼遵循特定的格式:

 

#!/bin/sh# Start/stop the cron daemon.#### BEGIN INIT INFO# Provides:          cron# Required-Start:    $remote_fs $syslog $time# Required-Stop:     $remote_fs $syslog $time# Should-Start:      $network $named slapd autofs ypbind nscd nslcd winbind# Should-Stop:       $network $named slapd autofs ypbind nscd nslcd winbind# Default-Start:     2 3 4 5# Default-Stop:# Short-Description: Regular background program processing daemon# Description:       cron is a standard UNIX program that runs user-specified #                    programs at periodic scheduled times. vixie cron adds a #                    number of features to the basic UNIX cron, including better#                    security and more powerful configuration options.### END INIT INFO# 省略部分case "$1" instart)  log_daemon_msg "Starting periodic command scheduler" "cron"        start_daemon -p $PIDFILE $DAEMON $EXTRA_OPTS        log_end_msg $?        ;;stop)   log_daemon_msg "Stopping periodic command scheduler" "cron"        killproc -p $PIDFILE $DAEMON        RETVAL=$?        [ $RETVAL -eq 0 ] && [ -e "$PIDFILE" ] && rm -f $PIDFILE        log_end_msg $RETVAL        ;;restart) log_daemon_msg "Restarting periodic command scheduler" "cron"        $0 stop        $0 start        ;;reload|force-reload) log_daemon_msg "Reloading configuration files for periodic command scheduler" "cron"        # cron reloads automatically        log_end_msg 0        ;;status)        status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?        ;;*)      log_action_msg "Usage: /etc/init.d/cron {start|stop|status|restart|reload|force-reload}"        exit 2        ;;esacexit 0

 

 

/etc/init.d/myscript還不能隨機啟動。Linux在開機啟動時,真正檢查的是/etc/rcN.d檔案夾,執行其中的指令碼。這裡的N代表了運行層級。比如說在運行層級2時,Linux會檢查/etc/rc2.d檔案夾,執行其中的指令碼。Unix系統可以在不同運行模式下工作,如單一使用者模式、多使用者模式,每種模式就稱為一個運行層級。大多數UNIX系統遵照: 

運行層級:0 停機,關機1 單使用者,無網路連接,不運行守護進程,不允許非超級使用者登入2 多使用者,無網路連接,不運行守護進程3 多使用者,正常啟動系統4 使用者自訂5 多使用者,帶圖形介面6 重啟

 

我們需要把/etc/init.d中的服務複製到或者建立軟串連到/etc/rcN.d,才能服務在該運行層級的開機啟動。不過,我們可以利用chkconfig命令更便利地進行:

chkconfig --add myscript            # 建立指令碼軟連結到預設層級chkconfig --level 35 --add myscript # 建立指令碼軟連結到3和5層級chkconfig --del myscript            # 刪除軟連結

 

利用/etc/rc.local開機啟動

樹莓派官網上給出了修改/etc/rc.local的方法,來在樹莓派開機時執行使用者自訂的任務。比如在該檔案中執行date命令:

#!/bin/sh -e## rc.local## This script is executed at the end of each multiuser runlevel.# Make sure that the script will "exit 0" on success or any other# value on error.## In order to enable or disable this script just change the execution# bits.## By default this script does nothing.# timedate > /tmp/rc.local.logexit 0

但這種啟動方式不推薦。/etc/rc.local是在系統初始化的末尾執行的一個指令碼。如果把太多的任務加入到這個指令碼中,不但會拖慢開機的速度,還會造成管理上的混亂。因此,/etc/rc.local往往只用於修改一些在啟動過程需要設定的參數,而不涉及具體的任務啟動。如果想隨開機啟動某些服務,應該避免使用/etc/rc.local。

 

樹莓派:最好的安排

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.