1.獨立腳本
1).編寫要執行腳本的sh文件mysetup.sh
#!/bin/sh
### BEGIN INIT INFO
# Provides: land.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
#任務腳本
#進入要執行腳本目錄
cd /home/cbuav/working/opencv/target_land
#取得root權限,'123456'爲密碼,不用加引號,'ls'無實際作用
echo 123456|sudo -S ls
#執行腳本./bin/mywork,sudo -S需要加上
sudo -S ./bin/mywork
#任務腳本
- 註釋部分是必須內容,ubuntu 16.04中一定要加上該LSB信息,不然放入啓動腳本的時候會報錯無法開機啓動。
- 任務腳本中,一般需要用到root權限,取得root權限和實際任務語句最好分開寫,有些情況下寫在一起不會成功。
2).將該sh文件移動到/etc/init.d/
目錄下,並修改權限
cp mysetup.sh /etc/init.d
sudo chmod 755 /etc/init.d/mysetup.sh
3).設置啓動順序
cd /etc/init.d
sudo update-rc.d mysetup.sh defaults 95
- 其中數字95是腳本啓動的順序號,按照自己的需要相應修改即可。在你有多個啓動腳本,而它們之間又有先後啓動的依賴關係時你就知道這個數字的具體作用了。
4).卸載啓動腳本
cd /etc/init.d
sudo update-rc.d -f mysetup.sh remove
2.在rc.local文件中添加啓動腳本
rc.local腳本是一個ubuntu開機後會自動執行的腳本,我們可以在該腳本內添加命令行指令。該腳本位於/etc/路徑下,需要root權限才能修改。
該腳本具體格式如下:
#!/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.
#任務腳本
#打開mate終端,並在其中運行腳本
mate-terminal -x /home/myname/mysetup.sh
#任務腳本
exit 0
- 一定要將命令添加在 exit 0之前
參考文獻: