轉載自CSDN博客:https://blog.csdn.net/u013401853/article/details/71079595
在ubuntu下要把一個程序加入開機啟動,一般可以通過修改rc.local來完成,但ubuntu下有兩個rc.local文件。分別是/etc/rc.local和/etc/init.d/rc.local。可以看一下兩個文件的內容找到他倆的關系:
/etc/init.d/rc.local
#! /bin/sh
### BEGIN INIT INFO
# Provides: rc.local
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: Run /etc/rc.local if it exist
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
. /lib/init/vars.sh
. /lib/lsb/init-functions
do_start() {
if [ -x /etc/rc.local ]; then
[ "$VERBOSE" != no ] && log_begin_msg "Running local boot scripts (/etc/rc.local)"
/etc/rc.local
ES=$?
[ "$VERBOSE" != no ] && log_end_msg $ES
return $ES
fi
}
case "$1" in
start)
do_start
;;
restart|reload|force-reload)
echo "Error: argument '$1' not supported" >&2
exit 3
;;
stop)
;;
*)
echo "Usage: $0 start|stop" >&2
exit 3
;;
esac
從注釋可以看出該腳本運行在2 3 4 5的啟動級別,只能處理start的參數,然后執行start,如果有/etc/rc.local文件的話則執行/etc/rc.local。如果要把開機啟動的程序放/etc/init.d/rc.local文件里,記住千萬別一股腦寫文件最后面就行了,因為在case語句塊里腳本就會退出。
/etc/rc.local
#!/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.
exit 0
這個腳本里面基本沒有內容,就是寫個模板讓你放開機自啟動程序的。把你的程序寫在exit 0行的前面就行了。
所以要添加開機啟動項,只需在/etc/rc.local文件中添加就行了。
ubuntu的啟動級別:
0 關機
1 單用戶
2-5 多用戶圖形界面
6 重啟
對應每個啟動級別,/etc/目錄下都對應一個像/etc/rc5.d/這樣的目錄,下面是一些腳本,這些腳本基本都是對應/etc/init.d/目錄下的軟鏈接,命名里面的數字代表優先級,啟動時這些腳本都會執行一遍。
我的系統為ubuntu 15.04
---------------------
作者:普朗克常量
來源:CSDN
原文:https://blog.csdn.net/u013401853/article/details/71079595
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
