博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux启动过程、守护进程以及其他
阅读量:7163 次
发布时间:2019-06-29

本文共 5418 字,大约阅读时间需要 18 分钟。

hot3.png

系统:ubuntu server 12.04

###1、linux启动过程

<hr/> 资料:[linux启动流程](http://www.cnblogs.com/scnutiger/archive/2009/09/30/1576795.html)

###2、关于SysV风格的启动方式

<hr/> **资料:** [init](http://zh.wikipedia.org/zh-cn/Init) [System V init启动与Busybox init启动对比](http://blog.csdn.net/newger/article/details/3292110)

在ubuntu12.04中/etc/rc.local/etc/rcN.d/README,man service也会有一些解释。

###3、运行级别(runlevel)

<hr />

  • 运行级别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如何启动到字符界面

<hr/> ubuntu server 12.04安装桌面后,每次启动默认进入lightDM,之后进入桌面环境。查看的runlevel为2,使用inittab文件将启动的runlevel改为3后仍然会进入桌面(原因请参考:[Ubuntu init启动流程分析](http://www.cnblogs.com/cassvin/archive/2011/12/25/ubuntu_init_analysis.html))。而各个rcN.d文件下也没有关于LightDM的文件。 **方法一:修改内核启动参数** 参考`http://hi.baidu.com/y365y/item/d5ecc803934ac3dcdde5b094`。

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体系

参考http://hi.baidu.com/y365y/item/d5ecc803934ac3dcdde5b094
/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的守护进程

<hr/> **如何软连接:**

bash >> ln -s tree.txt treebash >> 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/bashecho "start" >> /home/my.logdate >> /home/my.log

文件mylog-start内容如下:

#!/bin/bashecho "halt" >> /home/my.logdate >> /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/pythonfrom os import curdir,sepfrom BaseHTTPServer import BaseHTTPRequestHandler,HTTPServerclass MyHandler(BaseHTTPRequestHandler):    def do_GET(self):        content='''        

hello,kugou!

''' 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的软链接。待续。

apache2脚本分析

待续。

###6、如何穿创建基于upstart的守护进程

<hr/> [ubuntu-upstart官方资料](http://upstart.ubuntu.com/getting-started.html) 待续。 相关命令:service、initctl、update-rc.d、invoke-rc.d

###7、关于at

<hr/> `echo touch ~/touch_file | at now + 2 minutes` -->两分钟后建立/更新文件~/touch_file `echo shutdown -h now | at now + 4 hours` -->4小时后关机。

###8、关于crontab

<hr/> 查看命令cron、crontab的man文档,其中`man 5 crontab`获得的内容比`man crontab`更加详细。 资料:[Linux Crontab 定时任务 命令详解](http://blog.csdn.net/tianlesoftware/article/details/5315039)

###9、关于nohup

<hr/> 资料:[linux的nohup命令的用法](http://www.cnblogs.com/allenblogs/archive/2011/05/19/2051136.html)

###10、其他方式的开机启动

<hr/> **桌面程序** 进入 ~/.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

###11、相关资料

转载于:https://my.oschina.net/letiantian/blog/115881

你可能感兴趣的文章