ubuntu 开机自启动服务 和 GUI 软件

每天上班的第一件事就是开电脑 , 开一些工作上必须用的软件 或者一些服务 , 每天 都要动你的鼠标 去打开 应用程序 或者 在terminal 上敲命令启动 服务,这样 未免 太麻烦了 ,一点也不 DRY , 那就 把你每天必备的 服务 , 都放在 rc.local中 或者使用 update-rc.d , GUI 软件放到 startup applications 中 随着 开机一起启动吧

开机 自启动服务

使用 rc.local

1,把你需要 启动的 程序都放到 /etc/rc.local 中,注意写到 exit 0 前面

2,默认 ubuntu 的 shell 是 dash ,据说 是为了 提高 开机速度

1
2
wxianfeng@ubuntu:~$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 2010-11-12 19:12 /bin/sh -> dash

但是 dash 是一个 对脚本 兼容性 和 容错性 非常差的 shell,修改 rc.local 用 bash 来执行不要使用 dash

#!/bin/sh
改为

#!/bin/bash
或者直接修改 ubuntu 默认shell

1
2
3
4
5
wxianfeng@ubuntu:~$ ll /bin/sh
lrwxrwxrwx 1 root root 4 2010-11-12 19:12 /bin/sh -> dash*
wxianfeng@ubuntu:~$ sudo dpkg-reconfigure dash # 选择 NO
wxianfeng@ubuntu:~$ ll /bin/sh
lrwxrwxrwx 1 root root 4 2010-12-24 10:37 /bin/sh -> bash*

3,服务 无法 开启 daemon(守护进程) 的 用 nohup 启动 ,例如 mongodb

4,确保 文件的执行权限 登录的普通用户可以使用 , 默认 ubuntu 登录 是非 root 账户

修改前:

1
2
wxianfeng@ubuntu:~$ ll /etc/rc.local
-rwxr-xr-x 1 root root 736 2010-12-24 09:35 /etc/rc.local*

修改:

1
2
wxianfeng@ubuntu:~$ sudo chgrp -R wxianfeng /etc/rc.local
wxianfeng@ubuntu:~$ sudo chown -R wxianfeng /etc/rc.local

修改后:

1
2
wxianfeng@ubuntu:~$ ll /etc/rc.local
-rwxr-xr-x 1 wxianfeng wxianfeng 736 2010-12-24 09:35 /etc/rc.local*

5,把 rc.local 中的 第一行 -e 给去掉

-e 参数 是 脚本遇到 错误 就退出 , 这个 很不好 , 可以 man sh 看下 -e 参数:

1
2
3
>man sh
-e errexit If not interactive, exit immediately if any untested command fails. The exit status of a command is considered to be explicitly tested if the
command is used to control an if, elif, while, or until; or if the command is the left hand operand of an “&&” or “||” operator.

6,DEMO:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
wxianfeng@ubuntu:~$ cat /etc/rc.local 
#!/bin/bash
#
# 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.

/etc/init.d/mysql start # 启动 mysql
exit 0

说了 那么多 , 说下 rc.local 的 启动原理:

ubuntu 的开机的时候 会根据当前的 运行级别 即 runlevel

wxianfeng@ubuntu:~$ runlevel
N 2
可以看出是2 ,然后 通过 runlevel 执行 /etc/rc*.d 下的 所有文件 ,这里即 rc2.d

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
wxianfeng@ubuntu:~$ ll /etc/rc2.d/
total 20
drwxr-xr-x 2 root root 4096 2010-12-06 10:30 ./
drwxr-xr-x 141 root root 12288 2010-12-24 11:04 ../
-rw-r--r-- 1 root root 677 2010-09-25 02:16 README
lrwxrwxrwx 1 root root 20 2010-11-12 19:12 S20fancontrol -> ../init.d/fancontrol*
lrwxrwxrwx 1 root root 20 2010-11-12 19:12 S20kerneloops -> ../init.d/kerneloops*
lrwxrwxrwx 1 root root 27 2010-11-12 19:12 S20speech-dispatcher -> ../init.d/speech-dispatcher*
lrwxrwxrwx 1 root root 24 2010-12-06 10:30 S20virtualbox-ose -> ../init.d/virtualbox-ose*
lrwxrwxrwx 1 root root 19 2010-11-12 19:12 S25bluetooth -> ../init.d/bluetooth*
lrwxrwxrwx 1 root root 20 2010-11-12 19:12 S50pulseaudio -> ../init.d/pulseaudio*
lrwxrwxrwx 1 root root 15 2010-11-12 19:12 S50rsync -> ../init.d/rsync*
lrwxrwxrwx 1 root root 15 2010-11-12 19:12 S50saned -> ../init.d/saned*
lrwxrwxrwx 1 root root 19 2010-11-12 19:12 S70dns-clean -> ../init.d/dns-clean*
lrwxrwxrwx 1 root root 18 2010-11-12 19:12 S70pppd-dns -> ../init.d/pppd-dns*
lrwxrwxrwx 1 root root 14 2010-11-12 19:12 S75sudo -> ../init.d/sudo*
lrwxrwxrwx 1 root root 24 2010-11-12 19:12 S90binfmt-support -> ../init.d/binfmt-support*
lrwxrwxrwx 1 root root 17 2010-11-15 13:45 S91apache2 -> ../init.d/apache2*
lrwxrwxrwx 1 root root 22 2010-11-12 19:12 S99acpi-support -> ../init.d/acpi-support*
lrwxrwxrwx 1 root root 21 2010-11-12 19:12 S99grub-common -> ../init.d/grub-common*
lrwxrwxrwx 1 root root 18 2010-11-12 19:12 S99ondemand -> ../init.d/ondemand*
lrwxrwxrwx 1 root root 18 2010-11-12 19:12 S99rc.local -> ../init.d/rc.local*

可以 看到 S99rc.local 软链接 从 /etc/init.d/rc.local 过来的 ,打开 看看 ,这里有 调用 /etc/rc.local 的文件 ,/etc/rc.local 也就是 从这里启动的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
wxianfeng@ubuntu:~$ cat /etc/init.d/rc.local 
#! /bin/sh
### BEGIN INIT INFO
# Provides: rc.local
# Required-Start: $remote_fs $syslog $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: Run /etc/rc.local if it exist
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start() {
if [ -x /etc/rc.local ]; then
[ "$VERBOSE" != no ] && log_begin_msg "Running local boot scripts (/etc/rc.local)"
/etc/rc.local
ES=$?
[ "$VERBOSE" != no ] && log_end_msg $ES
return $ES
fi
}

case "$1" in
start)
do_start
;;
restart|reload|force-reload)
echo "Error: argument '$1' not supported" >&2
exit 3
;;
stop)
;;
*)
echo "Usage: $0 start|stop" >&2
exit 3
;;
esac

使用 update-rc.d

apache 开机自启动

1
2
3
4
5
>sudo update-rc.d apache2 defaults
mysql 开机自启动

>sudo update-rc.d mysql defaults
开机 启动 GUI 软件

上面 是 开机 启动一些服务 ,例如 mysql ,apache 啥的 ,但是 开机启动一些软件 怎么办,例如 netbeans , eclipse 等

ubuntu 已经 给了我们 gui的配置工具

system -> prefences -> start applications

把所有启动的 GUI 软件 放到一个 shell 里,然后 添加 启动这个shell 即可:

1
2
3
4
5
6
7
8
9
wxianfeng@ubuntu:~$ cat /usr/local/system/shell/autostart.sh 
#!/bin/sh

nohup /usr/local/system/netbeans6_9/bin/netbeans --locale zh_CN.UTF-8 & # 启动netbeans
nohup firefox & # 启动 firefox 可以 默认开启一个网页 firefox wxianfeng.com
nohup /opt/google/chrome/google-chrome & # 启动 chrome
nohup guake & # 启动guake
nohup emesene & # 启动 emesene (ubuntu MSN客户端)
nohup empathy & # 启动 empathy (登录gtalk之用)

添加 过后 其实 是在 ~/.config/autostart 目录下 建立了一个新的.desktop启动文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cd ~/.config/autostart/
wxianfeng@ubuntu:~/.config/autostart$ ll
total 12
drwx------ 2 wxianfeng wxianfeng 4096 2010-12-24 11:03 ./
drwxr-xr-x 18 wxianfeng wxianfeng 4096 2010-12-24 11:03 ../
-rw-r--r-- 1 wxianfeng wxianfeng 266 2010-12-24 11:03 autostart.sh.desktop
wxianfeng@ubuntu:~/.config/autostart$ cat autostart.sh.desktop

[Desktop Entry]
Type=Application
Exec=/usr/local/system/shell/autostart.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=autostart_shell
Name=autostart_shell
Comment[en_US]=start GUI software when boot
Comment=start GUI software when boot

也就是说 这个文件 你可以 手动建立 , 从里面可以看出 我开机启动的 shell 脚本 路径在 /usr/local/system/shell/autostart.sh

注意点:

刚开始 我想把我的 gui 软件 启动也放到rc.local 中 ,这个 是不允许的 ,rc.local 只能启动服务,不能启动 图形界面的 程序

see:

http://www.bettershop.com.cn/bbs/read.php?tid-1029.htmlhttp://blog.163.com/lgh_2002/blog/static/44017526201032803748503http://www.ihacklog.com/linux/tips-linux/fcitx-autostart-via-rc-local.htm