ubuntu添加開機啓動腳本(服務,app)



1、方法一,編輯rc.loacl腳本 

Ubuntu開機之後會執行/etc/rc.local文件中的腳本,

所以我們可以直接在/etc/rc.local中添加啓動腳本。

當然要添加到語句:exit 0 前面才行。

如:

sudo vi /etc/rc.local

/home/neo/test.sh

exit 0

然後在 exit 0 前面添加好腳本代碼。

 2、方法二,添加一個Ubuntu的開機啓動服務。

如果要添加爲開機啓動執行的腳本文件,

可先將腳本複製或者軟連接/etc/init.d/目錄下,

然後用:update-rc.d xxx defaults NN命令(NN爲啓動順序),

將腳本添加到初始化執行的隊列中去。

注意如果腳本需要用到網絡,則NN需設置一個比較大的數字,如99。

1) 將你的啓動腳本複製到 /etc/init.d目錄下
 以下假設你的腳本文件名爲 test。
2) 設置腳本文件的權限
 $ sudo chmod 755 /etc/init.d/test
3) 執行如下命令將腳本放到啓動腳本中去:
 $ cd /etc/init.d
 $ sudo update-rc.d test defaults 95
 注:其中數字95是腳本啓動的順序號,按照自己的需要相應修改即可。在你有多個啓動腳本,而它們之間又有先後啓動的依賴關係時你就知道這個數字的具體作用了。該命令的輸出信息參考如下:
update-rc.d: warning: /etc/init.d/test missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
  Adding system startup for /etc/init.d/test ...
    /etc/rc0.d/K95test -> ../init.d/test
    /etc/rc1.d/K95test -> ../init.d/test
    /etc/rc6.d/K95test -> ../init.d/test
    /etc/rc2.d/S95test -> ../init.d/test
    /etc/rc3.d/S95test -> ../init.d/test
    /etc/rc4.d/S95test -> ../init.d/test
    /etc/rc5.d/S95test -> ../init.d/test
卸載啓動腳本的方法:
 $ cd /etc/init.d
$ sudo update-rc.d -f test remove
命令輸出的信息參考如下:
Removing any system startup links for /etc/init.d/test ...
    /etc/rc0.d/K95test
    /etc/rc1.d/K95test
    /etc/rc2.d/S95test
    /etc/rc3.d/S95test
    /etc/rc4.d/S95test
    /etc/rc5.d/S95test
    /etc/rc6.d/K95test


-----------------------------------------------------------------------------------------------------------

下面是桌面版開機子啓動程序(app)。需先安裝app之後

下面以firefox爲例:

第一步:安裝:sudo apt-get install firefox

第二步:設置,完整流程如下:

mkdir -p ~/.config/autostart

sudo cp /usr/share/applications/firefox.desktop  ~/.config/autostart/
sudo chown username:group firefox.desktop
chmod 0755 firefox.desktop

sudo reboot -f

重啓之後會自動運行firefox


備註: rm  ~/.config/autostart/firefox.desktop 即可取消開機自啓動。









發佈了62 篇原創文章 · 獲贊 9 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章