Linux-启动和服务(service)

以下基于我对文末中各种资料的理解,如有错误还望指出

查询开启及关闭服务的命令可直接翻到文末。

service和daemon

为了支持某些功能,操作系统需要在后台运行一些服务和应用程序,这些就是service和daemon了。比如说如果要像远程连接到linux主机,需要开启ssh服务。

管理服务

计算机中服务众多,ssh要开启ssh服务,使用apache要开启apache服务,不可能每次要用的时候都自己手动开启,例如我们希望一台服务器开机就可以运行apache服务,所以我们需要一些工具(service manager)来管理各种服务,规定哪些服务在什么时候开启,在什么时候关闭。当然规定服务的启动和关闭一般说的都是计算机启动时,所以这就离不开init system

系统的第一个进程是init ,所有的其它进程都是它的子进程,init system 规定了开机应该如何(何时)启动各种进程,当然其中就包括各种服务。

主要的init system有以下几个

  • System V
    • Debian 6 and earlier
    • Ubuntu 9.04 and earlier
    • CentOS 5 and earlier
  • Upstart
    • Ubuntu 9.10 to 14.10, including Ubuntu 14.04
    • CentOS 6
  • systemd
    • Debian 7 and 8
    • Ubuntu 15.04 and newer
    • CentOS 7

system V

使用system V时,启动是分为好几个runlevel的,通过调用不同时刻对应的文件夹里(下文说到的rc?.d )的脚本文件而达到启动或关闭服务的目的,各个level对应的时刻可以通过man runlevel 查到。

  • 0 停机,关机
  • 1 单用户,无网络连接,不运行守护进程,不允许非超级用户登录
  • 2 多用户,无网络连接,不运行守护进程
  • 3 多用户,正常启动系统
  • 4 用户自定义
  • 5 多用户,带图形界面
  • 6 重启

Ubuntu16.04 下runlevel的manpage中有这样一行:

“Runlevels” are an obsolete way to start and stop groups of services used in SysV init.

确实,SysV已经逐渐淡出历史舞台了,不过在各个init system的发展中还是保持了对SysV的兼容性,因为任何Linux发行版都不可能为了采用新的init system而将所有的服务代码修改一遍,所以你还是可以看到它的影子。

Ubuntu 16.04/etc/rc?.d/ 目录下(其中的?是指0-6的数字或S),有很多链接,用ls -l 命令查看可以看到他们都指向的是/etc/init.d/ 目录下的各个脚本,rc?.d 目录名中的? 对应与各个runlevel ,系统启动到特定的runlevel时会调用相应目录的脚本。

如果仔细看会发现rc?.d/ 目录下的各个链接都是以KS 打头,其中K 表示kill,即此时这个服务应该被关闭,S 表示start,服务应该被启动,在KS 后还有一个数字,代表优先级。

相关命令

chkconfig

$ chkconfig apache on
$ chkconfig apache off

这个命令是Red Hat/CentOS上的,可以用来设置开机启动或关闭某个服务

update-rc.d

$ update-rc.d apache remove

这个是Ubuntu16.04 上的命令,下面是manpage的一部分

update-rc.d updates the System V style init script links /etc/rcrun‐level.d/NNname whose target is the script /etc/init.d/name.

service

暂时启动某个服务,不影响下次开机,调用/etc/init.d/ 下的SysV的脚本以启动服务

invoke-rc.d

启动或暂停某个服务,debian上的命令

Upstart

upstart的脚本文件放在了/etc/init/ 目录下。upstart里面有job的概念,/etc/init/ 目录下的每一个脚本都对应一个job,可以使用initctl 对这些job进行操作

命令

initctl

$ initctl list   #列出所有upstart管理的服务,即/etc/init/目录下的脚本
$ initctl stop ssh-agent #关闭服务
$ initctl status ssh-agetn #查看服务状态

service

启动某个服务可以使用service 命令(对,service可以启动SysV和upstart的脚本),当然这也是暂时的,不影响下次启动

关闭upstart某个服务

echo manual | sudo tee /etc/init/SERVICE.override

SERVICE 是你要操作的服务名称,只要/etc/init/ 目录下某个服务有override结尾的文件,这个服务就会被禁用。

systemd

systemd和upstart是竞争关系,不过现在看来貌似systemd更强,Ubuntu16.04 上面大部分的服务都是有systemd管理的,小部分是由upstart管理的。systemd的

命令

systemctl

$ systemctl stop apache  #关闭apache服务
$ systemctl disable apache #设置开机不启动

命令总结

  • systemctl 管理systemd

  • initctl 管理upstart

  • service 管理upstart和SysV

    管理他们在/etc/init//etc/init.d/ 下的脚本

  • update-rc.d SysV

  • invoke-rc.d SysV

  • chkconfig SysV

常用的有:

$ systemctl disable|enable SERVICE
$ systemctl start|stop|restart|status SERVICE
$ initctl start|stop|restart|status SERVICE
$ service SERVICE start|stop|restart|status
$ update-rc.d SERVICE enable|disable

其余命令查看manpage吧

note:在不同的发行版中可能只有上面命令的某几个,比如我现在用的16.04 除了没有Red Hat特有的chkconfig之外都有,因为从15.04开始ubuntu就开始采用systemd 了。


参考链接:

DigitalOcean

鸟哥的。。。gitbook

DigitalOcean-upstart

IBMdeveloperworks-upstart

AskUbuntu-how to enable and disable service

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章