Linux启动过程、守护进程以及其他


Linux启动过程、守护进程以及其他
 
系统:ubuntu server 12.04
1、linux启动过程
2、关于SysV风格的启动方式
3、运行级别(runlevel)
运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆
运行级别2:多用户状态(没有NFS)(Multi-User Mode Without Networking)
运行级别3:完全的多用户状态(有NFS),登陆后进入控制台命令行模式
运行级别4:多用户状态,系统未使用,保留
运行级别5:X11控制台,登陆后进入图形GUI模式
运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动
运行级别S :单用户恢复模式,运行很少进程以及服务
查看当前runlevel: 使用命令rulevel 
切换运行级别:init [0123456Ss] 
/etc/inittab: ubuntu 12.04中没有该文件,创建一个即可。在里面写入系统启动后进入的运行级别:id:N:initdefault: 。N是runlevel对应的数值。 相关工具: sysv-rc-conf,update-rc.d,rcconf,chkconfig
 
4、ubuntu12.04如何启动到字符界面
ubuntu server 12.04安装桌面后,每次启动默认进入lightDM,之后进入桌面环境。查看的runlevel为2,使用inittab文件将启动的runlevel改为3后仍然会进入桌面。而各个rcN.d文件下也没有关于LightDM的文件。 
方法一:修改内核启动参数 
nano /etc/default/grub
修改
GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash”
为:
GRUB_CMDLINE_LINUX_DEFAULT=” text”
然后运行下sudo update-grub2就可了。
不过在12.04 server中GRUB_CMDLINE_LINUX_DEFAULT的值为空(即"")。而update-grub2命令实际上指向update-grub命令。
 
方法二:upstart体系  
/etc/rcN.d中的文件都是/etc/init.d目录中文件的软链接,/etc/init.d目录里面基本上都是/lib/init/upstart-job文件的软连接(可以找时间分析下这个文件),upstart系统对启动项目的管理全部根据/etc/init目录里面的配置文件。所以可以修改一下/etc/init/lightdm.conf。 在/etc/init/lightdm.conf中可以看到:
 
start on ((filesystem
           and runlevel [!06]
           and started dbus
           and (drm-device-added card0 PRIMARY_DEVICE_FOR_DISPLAY=1
                or stopped udev-fallback-graphics))
          or runlevel PREVLEVEL=S)
stop on runlevel [06]
由于把runlevel设置为了3,所以改为:
 
start on ((filesystem
           and runlevel [!036]
           and started dbus
           and (drm-device-added card0 PRIMARY_DEVICE_FOR_DISPLAY=1
                or stopped udev-fallback-graphics))
          or runlevel PREVLEVEL=S)
stop on runlevel [036]
方法三:/etc/lightdm/users.conf(测试失败,没搞懂hidden-users是什么意思,也可能是该配置文件不起作用) 
找到:hidden-users=nobody nobody4 noaccess,改为:hidden-users=sunlt lotte
 
关于nobody,nobody4,noaccess:
 
Windows系统在安装后会自动建立一些用户帐户,在Linux系统中同样有一些用户帐户是在系统安装后就有的,就像Windows系统中的内置帐户一样。它 们是用来完成特定任务的,比如nobody和ftp等,我们访问LinuxSir.Org的网页程序,就是nobody用户(相当于Windows系统中 的匿名帐户);我们匿名访问ftp时,会用到用户ftp或nobody。
首先,nobody是一个普通用户,非特权用户。 使用nobody用户名的目的是,使任何人都可以登录系统,但是其UID(65534)和GID(65534)不提供任何特权,即该uid和gid只能访问人人皆可读写的文件。
其次,许多系统中都按惯例地默认创建一个nobody,尽量限制它的权限至最小,当服务器向外服务时,可能会让client以nobody的身份登录。
 
nobody -- an anonymous user account ,assigned by an NFS server when an unauthorized root user makes a request. The nobody user account is assigned to software processes that do not need any special permissions.
noaccess --  The account assigned to a user or a process that needs access to a system through some application without actually logging into the system.   
nobody4 -- SunOS4.0 or 4.1version of the nobody account.
5、如何创建基于rcN.d的守护进程
 
如何软连接:
bash >> ln -s tree.txt tree
bash >> ls -l
...
lrwxrwxrwx 1 sunlt sunlt      8 Mar 21 08:19 tree -> tree.txt
-rw-rw-r-- 1 sunlt sunlt 104946 Mar 20 07:19 tree.txt
...
示例1:记录开关机时间 
ubuntu的系统日志一般放在目录/var/log中。现在制作一个简单的记录开关机时间的日志,日志文件为/home/my.log。 
文件mylog-halt内容如下:
 
#!/bin/bash
echo "start" >> /home/my.log
date >> /home/my.log
文件mylog-start内容如下:
 
#!/bin/bash
echo "halt" >> /home/my.log
date >> /home/my.log
将mylog-start和mylog-halt放入/etc/init.d/目录中,添加执行权限。自己把这两个文件软链接到相应的rcN.d目录或者使用sysv-rc-conf之类的工具。
 
示例2:一个web服务器 下面是一个简单的基于python的web server示例py_http.py:
 
#!/usr/bin/python
from os import curdir,sep
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        content='''
        <html><body><h2>hello,kugou!</h2></body></html>
        '''
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        self.wfile.write(content)                
def main():
    server = HTTPServer(('',8080),MyHandler)
    server.serve_forever()
if __name__=='__main__':
    main()
按照示例1配置失败。(继续努力...) 但是将python /path/to/py_http.py &放入/etc/rc.local文件中成功。
 
mysql启动脚本分析 
/etc/init.d/mysql是/lib/init/upstart-job的软链接。
 
6、如何穿创建基于upstart的守护进程
相关命令:service、initctl、update-rc.d、invoke-rc.d
7、关于at
echo touch ~/touch_file | at now + 2 minutes -->两分钟后建立/更新文件~/touch_file 
echo shutdown -h now | at now + 4 hours -->4小时后关机。
8、关于crontab
查看命令cron、crontab的man文档,其中 man 5 crontab获得的内容比 man crontab更加详细。 
9、关于nohup
10、其他方式的开机启动
桌面程序 进入 ~/.config/autostart 目录 ,创建以名字加.desktop的文件(一个可执行的shell脚本什么的应该也可以),类似于application目录中的文件,如果要只在某种桌面环境下启动,可以在相应的desktop目录中添加 OnlyShowIn=<desktop-name>;,例如 OnlyShowIn=KDE;。 
其他autostart目录:
/etc/xdg/autostart
/usr/share/autostart
/usr/share/gdm/autostart
/usr/share/gnome/autostart
~/.config/autostart
~/.kde/share/autostart
~/.local/share/autostart
其他用户配置文件 如/etc/bash.bashrc,~/.bashrc
 

相关内容

    暂无相关文章