linux 自启动服务 默认是以root启动的, 但是我们很多服务都是以普通用户启动的, 如果以root启动 , 这样会到来一些权限问题,例如应用中保存的文件等等.
以普通用户启动的核心指令如下:
su - user -f /path/shell
demo:
/etc/rc.local
[wxianfeng@li390-146 ~]$ cat /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local /bin/sh /usr/local/system/www/shell/linode/init_by_root.sh >> /usr/local/system/logs/init.log 2>&1 su - wxianfeng -f /usr/local/system/www/shell/linode/init_by_wxianfeng.sh >> /usr/local/system/logs/init.log 2>&1
init_by_root.sh 脚本
#!/bin/bash # nginx /usr/local/system/nginx/sbin/nginx # mysql /etc/init.d/mysqld start # svn svnserve -d -r /usr/local/system/vcs
init_by_wxianfeng.sh 脚本
#!/bin/bash source /usr/local/rvm/environments/ruby-1.9.2-p180 cd /usr/local/system/www/project_manager thin start -e production -d -p 5000 source /usr/local/rvm/environments/ruby-1.8.7-p334 thin start -C /etc/thin/thin.yml
最终环境 : ubuntu 10.10 + ruby 1.8.7 + rails 2.3.5 + mysql 5.1
换了新工作,重新搭建开发环境,鉴于之前mysql遇到问题,总是不好解决,束手无策,甚至找不到 mysql 安装哪去了,所以这次主要想编译安装mysql,这样有利于了解mysql
1,下载ruby1.8.7 (http://www.ruby-lang.org/en/news/2010/08/16/ruby-1-8-7-p302-is-released/)
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p302.tar.bz2
2,安装依赖的库
sudo apt-get install build-essential sudo apt-get install autoconf sudo apt-get install zlib1g-dev sudo apt-get install openssl libssl-dev
3,编译安装ruby
tar -jxvf ruby-1.8.7-p302.tar.bz2 cd ruby-1.8.7-p302/ autoconf ./configure --prefix=/usr/local/system/ruby make sudo make install
4,ruby 加到PATH 中
vim ~/.bashrc export PATH="$PATH:/usr/local/system/ruby/bin"
5,ruby -v
安装rubygems(http://rubyforge.org/frs/?group_id=126&release_id=43601)
1,下载
wget http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz
2,解压
tar -zvxf rubygems-1.3.7.tgz
3,安装
cd rubygems-1.3.7/ wxianfeng@ubuntu:~/Desktop/rubygems-1.3.7$ ruby setup.rb ERROR: While executing gem ... (Errno::EACCES) Permission denied - /usr/local/system/ruby/lib/ruby/site_ruby/1.8/rubygems wxianfeng@ubuntu:~/Desktop/rubygems-1.3.7$ sudo ruby setup.rb [sudo] password for wxianfeng: sudo: ruby: command not found 改变权限 cd /usr/local sudo chmod -R 777 system/ 再次安装就ok了,不然你以后还会遇到这个问题
4,gem -v
安装rails 2.3.5
>gem install rails -v 2.3.5 --no-ri --no-rdoc
编译安装 mysql
1,下载并且编译安装
download (http://downloads.mysql.com/archives.php?p=mysql-5.1) 从archives 找到 ,注意下载的是source包
sudo apt-get install g++ libncurses5-dev # 安装依赖的库 sudo groupadd mysql # 建立mysql用户组 sudo useradd -g mysql mysql # 添加用户mysql tar -zvxf mysql-5.1.51.tar.gz cd mysql-5.1.51/ ./configure --prefix=/usr/local/system/mysql --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=latin1(all) --with-plugins=innobase(all) # config 很重要,括号里是或者 make sudo make install sudo cp support-files/my-medium.cnf /etc/my.cnf # 配置文件
注意mysql sock文件安默认装在tmp路径下 /tmp/mysql.sock,编译的时候 —with-plugins=all 建议加上 不然你会发现编译后不支持innodb引擎,那就麻烦了!
2, 初始化数据库并修改目录权限
cd /usr/local/system/mysql sudo bin/mysql_install_db --user=mysql sudo chown -R root . sudo chown -R mysql /usr/local/system/mysql/var sudo chgrp -R mysql .
3,启动mysql
bin/mysqld_safe --user=mysql & netstat -antup | grep 3306 sudo cp /usr/local/system/mysql/share/mysql/mysql.server /etc/init.d/mysql sudo killall mysqld # kill 刚刚启动的mysql sudo /etc/init.d/mysql start|stop|restart|status # 重启mysql
4,添加PATH
>vim ~/.bashrc export PATH="$PATH:/usr/local/system/mysql/bin" wxianfeng@ubuntu:/usr/local/system/mysql/bin$ mysql --version mysql Ver 14.14 Distrib 5.1.51, for pc-linux-gnu (i686) using EditLine wrapper
5, 修改mysql root 密码
>sudo /etc/init.d/mysql start --skip-grant-tables >mysql -uroot -p mysql >update user set password=passsword('root') where user='root'; 重启mysql 就ok了 root 账户的密码为root
6,怎么判断我的mysql是编译安装的
wxianfeng@ubuntu:~$ mysql --version mysql Ver 14.14 Distrib 5.1.51, for pc-linux-gnu (i686) using EditLine wrapper
如果你是apt-get 安装的话就显示 for ubuntu-linux-gnu
yum 安装的话就显示 for redhat-linux-gnu 了
遇到的问题:
1,
>mysql -uroot -p
Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)
>mysql -uroot -p --socket=/tmp/mysql.sock 可以启动,所以是sock问题 后来发现 my.cnf 有两个配置文件,/etc/mysql/my.cnf 多了一个这个,里面sock路径指定不一样所致,rename 掉 >mv /etc/mysql/my.cnf /etc/mysql/my_cnf.bak
2,
Starting MySQL. * Manager of pid-file quit without updating file. 错误
wxianfeng@ubuntu:/usr/local/system/shell$ /etc/init.d/mysql start Starting MySQL. * Manager of pid-file quit without updating file.
需要加sudo
3,查看mysql data目录
root@ubuntu:/usr/local/system/mysql/var# ps auxf | grep mysql root 25756 0.0 0.0 4012 764 pts/3 S+ 15:32 0:00 | \_ grep --color=auto mysql root 25000 0.0 0.0 4904 1400 ? S 14:22 0:00 /bin/sh /usr/local/system/mysql/bin/mysqld_safe --datadir=/usr/local/system/mysql/var --pid-file=/usr/local/system/mysql/var/ubuntu.pid mysql 25106 0.0 0.8 119652 16568 ? Sl 14:22 0:00 \_ /usr/local/system/mysql/libexec/mysqld --basedir=/usr/local/system/mysql --datadir=/usr/local/system/mysql/var --user=mysql --log-error=/usr/local/system/mysql/var/ubuntu.err --pid-file=/usr/local/system/mysql/var/ubuntu.pid --socket=/tmp/mysql.sock --port=3306
可以看出在var下 ,注意 var目录的用户是 mysql ,必须root用户才能cd进入
sudo su # 切换到root用户
4,启动rails project报错:因为mysql gem的版本过高的原因
uninitialized constant MysqlCompat::MysqlRes (NameError) >sudo apt-get install libmysqlclient-dev >gem install mysql -v=2.7 --no-ri --no-rdoc
SEE:
http://blog.sitearth.com/ubuntu%E4%B8%8A%E6%90%AD%E5%BB%BAlamp%E7%8E%AF%E5%A2%83%E7%AC%94%E8%AE%B0%E2%80%94%E2%80%94mysql%E6%BA%90%E7%A0%81%E5%AE%89%E8%A3%85%E5%8F%8A%E9%85%8D%E7%BD%AE/
http://51jsp.cn/html/ror/2010/0327/14859.html
see:http://qichunren.javaeye.com/blog/577556
环境:ubuntu 10.10
每天上班的第一件事就是开电脑 , 开一些工作上必须用的软件 或者一些服务 , 每天 都要动你的鼠标 去打开 应用程序 或者 在terminal 上敲命令启动 服务,这样 未免 太麻烦了 ,一点也不 DRY , 那就 把你每天必备的 服务 , 都放在 rc.local中 或者使用 update-rc.d , GUI 软件放到 startup applications 中 随着 开机一起启动吧
开机 自启动服务
使用 rc.local
1,把你需要 启动的 程序都放到 /etc/rc.local 中,注意写到 exit 0 前面
2,默认 ubuntu 的 shell 是 dash ,据说 是为了 提高 开机速度
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
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 账户
修改前:
wxianfeng@ubuntu:~$ ll /etc/rc.local -rwxr-xr-x 1 root root 736 2010-12-24 09:35 /etc/rc.local*
修改:
wxianfeng@ubuntu:~$ sudo chgrp -R wxianfeng /etc/rc.local wxianfeng@ubuntu:~$ sudo chown -R wxianfeng /etc/rc.local
修改后:
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 参数:
>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:
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
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 也就是 从这里启动的:
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 开机自启动
>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 即可:
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 blog.wxianfeng.com nohup /opt/google/chrome/google-chrome & # 启动 chrome nohup guake & # 启动guake nohup emesene & # 启动 emesene (ubuntu MSN客户端) nohup empathy & # 启动 empathy (登录gtalk之用)
添加 过后 其实 是在 ~/.config/autostart 目录下 建立了一个新的.desktop启动文件
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.html http://blog.163.com/lgh_2002/blog/static/44017526201032803748503 http://www.ihacklog.com/linux/tips-linux/fcitx-autostart-via-rc-local.htm
上次写了安装所有gems,如果系统已经安装了 就跳过安装的 shell脚本,其中最主要的使用了 字符串的截取,发现bash里截取字符串非常之麻烦,请教了 一个 搞嵌入式的 家伙(同学) @liuqun,给了我 几个指点,发现原来也有很多办法,只是不是那么顺手.
上次的那个 shell 脚本:
#!/bin/bash # 安装所有的gems,如果已经安装了就不安装 cd /usr/local/system/ruby/lib/ruby/gems/1.8/cache for i in `ls`;do gem=`echo $i | awk -F'-' '{print $1}'` version=`echo $i | grep -o "\-[0-9].*" | sed 's/^-//;s/.gem//'` is_gem_exist=`gem list $gem -v=$version` if [ -z "$is_gem_exist" ]; then # 注意[] 内部两边留空格 `gem install $i` else echo "$i have installed" fi done
1:
wxianfeng@ubuntu:~$ echo "action-i18n-0.4.1.gem" | grep -o "\-[0-9].*" | sed 's/^-//;s/.gem//' 0.4.1
sed 's/^-//' 把开头的 - 变为空
sed 's/.gem//' 是把.gem 变为空
2,
wxianfeng@ubuntu:~$ STR="actionmailer-2.3.5.gem"; STR=${STR##*-}; echo ${STR%\.*} 2.3.5
${STR%\.*} 从右向左截取第一个 . 后的字符串
3,
wxianfeng@ubuntu:~$ STR="actionmailer-2.3.5.gem"; echo ${STR:13:5} 2.3.5
4,
wxianfeng@ubuntu:~$ echo actionmailer-2.3.5.gem | sed 's/[^0-9]*\(.*\).gem/\1/' 2.3.5
5,
wxianfeng@ubuntu:~$ STR="actionmailer-2.3.5.gem" ; echo ${STR:13:5} 2.3.5
6,
wxianfeng@ubuntu:~$ echo "action-i18n-0.4.1.gem" | awk '{print substr($0,13,5)}' 0.4.1
13 是偏移量
5 是 长度
7,
wxianfeng@ubuntu:~$ echo "action-i18n-0.4.1.gem" | cut -d- -f 3 | cut -d . -f 1-3 0.4.1
-f 3 是第三断
-f 1-3 第一到第三断
see: http://tech.foolpig.com/2008/07/09/linux-shell-char/
http://linux.sheup.com/linux/linux5426.htm