Ubuntu下設置開機后自動運行命令


從道理上來講,Ubuntu開機應該是能夠設置執行一些腳本的,事實上確實如此,網上給出了很多解決的方案,基本上是分為兩種,

第一種是編輯/etc/下的rc.local腳本,

然后把對應的需要執行的腳本寫在exit 0前面,在ubuntu16.06上親測無效

第二種則是編輯一個shell腳本,然后在shell腳本中寫入自己開機之后需要執行的命令,然后把該腳本拷貝進/etc/init.d/文件夾下,賦予可執行的權限,然后一行命令sudo update-rc.d 你的腳本 defaults 90

比如我這里先寫一個開機自動掛載兩個盤以及frpc內網穿透的服務,

然后命名為mount_and_frpc.sh,從桌面拷貝到文件夾/etc/init.d/下:sudo cp ./mount_and_frpc.sh /etc/init.d/,並且賦予可執行權限sudo chmod 775 ./mount_and_frpc.sh

然后執行該命令:sudo update-rc.d mount_and_frpc.sh defaults 90,然后會報如下錯誤:missing LSB tags and overrides,這參考下面這倆個鏈接

http://blog.bbzhh.com/index.php/archives/134.html, http://www.linuxdiyf.com/linux/26896.html

個時候需要在執行的命令前面加上這樣一段話:

 

### BEGIN INIT INFO
# Provides:          svnd.sh
# Required-start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the svnd.sh daemon
# Description:       starts svnd.sh using start-stop-daemon
### END INIT INFO

 

然后執行:sudo update-rc.d mount_and_frpc.sh defaults 90,重啟reboot即可

同樣的,如果想取消該開機自啟項,需要執行如下命令

cd /etc/init.d

sudo update-rc.d -f mount_and_frpc.sh remove

更多Ubuntu相關信息見Ubuntu 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=2

本文永久更新鏈接地址http://www.linuxidc.com/Linux/2017-09/147178.htm

 

 

 

1.復制或軟連接腳本到/etc/init.d/目錄下
 
2.將腳本添加到初始化執行的隊列中去
注意如果腳本需要用到網絡,則NN需設置一個比較大的數字,如99。
命令:update-rc.d xxx defaults NN命令 #(NN為啟動順序)
ubuntu 16.04中一定要加上以下LSB信息,不然放入啟動腳本的時候會報錯無法開機啟動。
#!/bin/sh
### BEGIN INIT INFO
# Provides:          svnd.sh
# Required-start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the svnd.sh daemon
# Description:       starts svnd.sh using start-stop-daemon
### END INIT INFO
 
3.設置腳本文件的權限
$ sudo chmod 755 /etc/init.d/svnd.sh (注意一定要設置權限,不然開機不會啟動)
 
4.執行如下命令將腳本放到啟動腳本中去:
$ cd /etc/init.d
$ sudo update-rc.d svnd.sh defaults 95
注:其中數字95是腳本啟動的順序號,按照自己的需要相應修改即可。在你有多個啟動腳本,而它們之間又有先后啟動的依賴關系時你就知道這個數字的具體作用了。
 
5.卸載啟動腳本的方法:
$ cd /etc/init.d
$ sudo update-rc.d -f svnd.sh remove
 
本文永久更新地址: http://www.linuxdiyf.com/linux/26896.html


免責聲明!

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



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