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

posted @ 2016-01-23 11:43  mo_wang  阅读(39804)  评论(0编辑  收藏  举报