service命令示例

状态检查

查看sshd守护程序的当前状态

[root@centos etc]# service sshd status
openssh-daemon (pid  1599) is running...

所有进程的状态检查

[root@centos ~]# service --status-all
abrt-ccpp hook is installed
abrtd (pid  1708) is running...
abrt-dump-oops (pid 1716) is running...
acpid (pid  1487) is running...
atd (pid  1740) is running...

停止某个服务

停止sshd守护程序

[root@centos etc]# service sshd stop
Stopping sshd:                                             [  OK  ]

[root@centos etc]# service sshd status
openssh-daemon is stopped

启动某个服务

[root@centos etc]# service sshd start
Starting sshd:                                             [  OK  ]

重启服务

[root@centos etc]# service sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]

重新加载配置

[root@centos etc]# service sshd reload
Reloading sshd:                                            [  OK  ]

初始化过程

Linux系统上的init进程通常被称为所有进程的父进程。init进程将始终具有1(pid为1)的进程ID,它是系统上的内核启动的第一个进程。init是初始化的缩写,用于启动所有其他进程和默认运行级别。随着时间的推移,许多发行版已采用System V标准来管理服务,但是,其他发行版已移至SystemdUpstart

Upstart

Upstart是基于事件的init守护程序的替代。Upstart由一家知名公司的前雇员撰写,该公司为我们提供了Ubuntu(Canonical)。Upstart的想法是摆脱传统的启动过程,在传统的启动过程中,必须先完成已启动的任务,然后才能开始下一个任务。Upstart是一个事件驱动的系统,允许它异步响应系统事件。Upstart负责在启动和关闭时启动和停止服务和任务。它还积极监视这些服务和任务。Upstart还能够运行sysvinit脚本而无需进行修改。各种发行版包括Upstart来替代SystemV。这些发行版包括RHEL,CentOS和Fedora。但是,许多这样的系统现在已移至系统化

如前所述,我们可以在System V下使用service命令控制服务/守护程序,但是在Upstart下,我们使用了另一个命令。与Upstart一起使用的命令是initctl。该命令允许您与init守护程序进行通信。

以下是使用中的initctl命令的一些基本示例。

initctl命令示例

状态检查

root@john-desktop:~# initctl status cups
cups start/running, process 672

停止服务

root@john-desktop:~# initctl stop cups
cups stop/waiting

启动服务

root@john-desktop:~# initctl start cups
cups start/running, process 3574

重启服务

root@john-desktop:~# initctl restart cups
cups start/running, process 3606

重新加载配置

root@john-desktop:~# initctl reload cups

列出所有服务

root@john-desktop:~# initctl list
avahi-daemon start/running, process 611
mountall-net stop/waiting
nmbd stop/waiting
passwd stop/waiting
rc stop/waiting
rsyslog start/running, process 601
tty4 start/running, process 1080

重新加载配置

此选项请求init守护程序重新加载其配置文件。此命令不会重新启动任何作业。

root@john-desktop:~# initctl reload-configuration cups

SysV-系统V

如果您的Linux发行版使用SysV标准,则init将检查一个名为/etc/inittab的文件。该文件包含系统应在其下启动的默认运行级别。例如,id:3:initdefault:将自动启动运行级别3目录结构中的所有脚本。

运行级别1-单用户模式
运行级别2-与运行级别3相同,但没有NFS
运行级别3-多用户和联网模式
运行级别4-未使用的
运行级别5-与运行级别3,但带有图形桌面(X窗口系统)

运行System V初始化脚本

作为管理员,您经常需要停止,启动,重新启动或重新加载特定的服务/守护程序。为此,我们使用一个名为service的命令。该命令允许您执行通常位于/etc/init.d目录中的System V脚本。除了能够启动和停止服务之外,我们还可以查看当前状态。

管理服务和守护程序

了解SysV-System V,Upstart和Systemd。使用initctl命令。使用service命令。使用systemctl命令来启动,停止,列出服务。

systemd

systemd是System V的另一个替代。systemd代表系统守护程序。它的名称故意是小写!systemd旨在更好地处理依赖关系,并具有在启动时并行处理更多工作的能力。systemd支持系统快照和系统状态还原,跟踪存储在所谓的cgroup(与传统的PID方法)不同的进程。现在,许多流行的Linux发行版都采用了systemd。Fedora,Mandriva,Mageia,Arch Linux。还计划将systemd包含在较新的RHEL(Red hat)版本中。

正如我们在System VUpstart中所看到的,这两种类型都有自己独特的命令来控制服务。同样适用于systemd。在systemd下使用的命令是systemctl。以下是一些systemctl命令的一些基本功能的示例。

系统命令描述
systemctl start mytest.service启动指定服务
systemctl stop mytest.service停止指定的服务
systemctl status mytest.service查看指定服务的状态
systemctl list-unit-files --type = service列出可以启动或停止的已知服务
systemctl restart mytest.service重启指定的服务
systemctl reload mytest.service如果支持,将重新加载配置文件
systemctl enable mytest.service启用服务,相当于chkconfig mytest on
systemctl disable mytest.service禁用服务,相当于chkconfig mytest off
systemctl is-enabled mytest.service检查当前运行级别服务是否启用
日期:2019-04-29 03:17:31 来源:oir作者:oir