Ubuntu添加開機自動啟動程序方法


1. 開機啟動時自動運行程序   

Linux加載后, 它將初始化硬件和設備驅動, 然后運行第一個進程init。init根據配置 
   文件繼續引導過程,啟動其它進程。通常情況下,修改放置在 
     /etc/rc或 
     /etc/rc.d 或 
     /etc/rc?.d 
   目錄下的腳本文件,可以使init自動啟動其它程序。例如:編輯 
     /etc/rc.d/rc.local 文件(該文件通常是系統最后啟動的腳本), 
   在文件最末加上一行“xinit”或“startx”,可以在開機啟動后直接進入X-Window。

2. 登錄時自動運行程序

  (1) 用戶登錄時,bash先自動執行系統管理員建立的全局登錄script : 
     /ect/profile 
   然后bash在用戶起始目錄下按順序查找三個特殊文件中的一個: 
     /.bash_profile、 
     /.bash_login、 
     /.profile, 
   但只執行最先找到的一個。因此,只需根據實際需要在上述文件中加入命令就可以實 
   現用戶登錄時自動運行某些程序(類似於DOS下的Autoexec.bat)。

 

(2)14.04 and later

參考:How do I start applications automatically on login?

在Ubuntu14.04版本之后提供了更方便的autostart文件夾,可以在這個文件夾下設置啟動項,設置方法有三種,如下:

方法一:GUI方法

  • Open the Dash and search for "Startup Applications"

    enter image description here

  • Now click on Add and give in the command to run the application. This can be found in Main Menu if installed (see below) or as shown in this question.

    enter image description here


方法二:使用其他程序:Using Main Menu (alacarte Install alacarte)

  • Firstly open the program 'Main Menu' (type Menu in the Dash)

    enter image description here

  • Now select the program which you want to add to startup and click on properties .

    enter image description here

  • Now note the command for that program .

    enter image description here


方法三:Non GUI approach

Advanced users may want to put a .desktop file in ~/.config/autostart to run applications after a user login. This may have following content:

[Desktop Entry]
Type=Application
Name=<Name of application as displayed>
Exec=<command to execute>
Icon=<full path to icon>
Comment=<optinal comments>
X-GNOME-Autostart-enabled=true

 

 

 

 

 

3. 退出登錄時自動運行程序 
   退出登錄時,bash自動執行個人的退出登錄腳本 
     /.bash_logout。 
   例如,在/.bash_logout中加入命令“tar -cvzf c.source.tgz *.c”,則在每次退出 
   登錄時自動執行 “tar” 命令備份 *.c 文件。

4. 定期自動運行程序 
   Linux有一個稱為crond的守護程序,主要功能是周期性地檢查 /var/spool/cron目錄 
   下的一組命令文件的內容,並在設定的時間執行這些文件中的命令。用戶可以通過 
   crontab 命令來建立、修改、刪除這些命令文件。

   例如,建立文件crondFile,內容為“00 9 23 Jan * HappyBirthday”,運行“crontab 
   cronFile”命令后,每當元月23日上午9:00系統自動執行“HappyBirthday”的程序(“* 
   ”表示不管當天是星期幾)。

5. 定時自動運行程序一次 
   定時執行命令at 與crond 類似(但它只執行一次):命令在給定的時間執行,但不自 
   動重復。at命令的一般格式為:at [ -f file ] time ,在指定的時間執行file文件 
   中所給出的所有命令。也可直接從鍵盤輸入命令: 
     $ at 12:00 
     at>mailto Roger -s ″Have a lunch″ < plan.txt 
     at>Ctr-D 
     Job 1 at 2000-11-09 12:00 
   2000-11-09 12:00時候自動發一標題為“Have a lunch”,內容為plan.txt文件內容 
   的郵件給Roger.

ubuntu 自添加開機啟動程序
ubuntu (我的是 9.10)的開機啟動會和 redhat suse 這些發行版會稍有差別,比如默認情況下沒有 /etc/inittab 的配置文件,redhat 發行版在啟動級別 3 上是文本模式登錄,而 ubuntu 的啟動級別2~5 都是一樣的啟動。現在,添加一個自定義的可執行文件或腳本,使其在開機啟動時執行。

以一個腳本為例,腳本的內容很簡單 :

引用 
#! /bin/sh

echo "hello start up script!" > /home/beyes/mystart.txt

exit 0


這個腳本的作用只是在我的家目錄里建立一個文本文件,里面的內容就是 echo 后的內容hello start up script!。

編輯好這個腳本后,給其賦予相應的可執行文件,為了方便,就 chmod 777 /etc/init.d/mystart

接着在 /etc/rc5.d 這個目錄下做一個軟鏈接:  ln -s /etc/init.d/mystart /etc/rc5.d/S99mystart

那么,這個腳本開機啟動生效了么?經過重啟后,並沒有發現在 /home/beyes 目錄下生成 mystart.txt 文件。

使用 sysv-rc-conf 配置一下啟動服務:
 
上圖,在第 2 運行級別也配置了讓 mystart 啟動。這個 sysv-rc-conf 會讀取 /etc/init.d 里的文件以及 rcx.d (x為運行級別)下的軟連接等信息。關於更多管理啟動項的更多信息見:
http://www.groad.net/bbs/read.php?tid-1392.html

配置完后,重啟。再到 /home/beyes 里查看,生成了 mystart.txt 文件,里面也有相應的內容。從這里,也看到了 ubuntu 默認的啟動運行級別為 2 。另外,在 /etc/rc2.d 目錄下,也發現了由 sysv-rc-conf 生成的軟連接: S99mystart

ubuntu開機自動運行程序
1.編寫shell腳本
   gedit /etc/init.d/aa
   #!/bin/bash
   mplayer /home/aa.avi -fs -vo fbdev -vf scale=800:600 (fs全屏,vo進入桌面前使用,        scale設置畫面大小)
2. chmod 755 /etc/init.d/aa    
3. ubuntu默認啟動級別為2加載的腳本在/etc/rc2.d/
   ln -s /etc/init.d/aa /etc/rc2.d/s99aa (s為開始執行99為執行順序aa為文件名)
本貼來自天極網群樂社區--http://q.yesky.com/group/review-17826808.html

 

linux啟動過程綜述
http://www.ibm.com/developerworks/cn/linux/kernel/startup/index.html

Upstart與ubuntu啟動過程,簡單原理
http://www.linuxdiyf.com/viewarticle.php?id=102927

Upstart: Ubuntu 的基於事件的啟動進程
http://www2.oklinux.cn/html/Basic/azpz/20080504/52808.htm

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/wbgeorge/archive/2010/05/19/5607425.aspx


免責聲明!

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



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