1. 開機啓動時自動運行程序 php
Linux加載後, 它將初始化硬件和設備驅動, 而後運行第一個進程init。init根據配置
文件繼續引導過程,啓動其它進程。一般狀況下,修改放置在
/etc/rc或
/etc/rc.d 或
/etc/rc?.d
目錄下的腳本文件,可使init自動啓動其它程序。例如:編輯
/etc/rc.d/rc.local 文件(該文件一般是系統最後啓動的腳本),
在文件最末加上一行「xinit」或「startx」,能夠在開機啓動後直接進入X-Window。html
2. 登陸時自動運行程序
(1) 用戶登陸時,bash先自動執行系統管理員創建的全局登陸script :
/ect/profile
而後bash在用戶起始目錄下按順序查找三個特殊文件中的一個:
/.bash_profile、
/.bash_login、
/.profile,
但只執行最早找到的一個。所以,只需根據實際須要在上述文件中加入命令就能夠實
現用戶登陸時自動運行某些程序(相似於DOS下的Autoexec.bat)。linux
(2)14.04 and later
參考:How do I start applications automatically on login?shell
在Ubuntu14.04版本以後提供了更方便的autostart文件夾,能夠在這個文件夾下設置啓動項,設置方法有三種,以下:ubuntu
方法一:GUI方法bash
-
Open the Dash and search for "Startup Applications"app

-
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.post

方法二:使用其餘程序:Using Main Menu (alacarte
)this
-
Firstly open the program 'Main Menu' (type Menu
in the Dash)spa

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

-
Now note the command for that program .

方法三: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