欢迎您来到雷竞技raybet官方网站、祝您体验愉快! 咨询热线:039-64156952

明白话 linux 教程-11-系统服务治理

本文摘要:当你浏览 linux 的目录的时候有没有注意到有这样一个目录 /etc/init.d/,下面有许多剧本文件,好比 /etc/init.d/ufw,它是 ubuntu 自带的一个防火墙软件,我们可以通过 sudo service ufw stop 来停止,用 sudo service ufw start 来启动,cat /etc/init.d/ufw 发现这个剧本处置惩罚了 start|status|stop|restart 等参数,换言之它是一个包装的剧本,用来对接 ser

雷竞技raybet官方网站入口

当你浏览 linux 的目录的时候有没有注意到有这样一个目录 /etc/init.d/,下面有许多剧本文件,好比 /etc/init.d/ufw,它是 ubuntu 自带的一个防火墙软件,我们可以通过 sudo service ufw stop 来停止,用 sudo service ufw start 来启动,cat /etc/init.d/ufw 发现这个剧本处置惩罚了 start|status|stop|restart 等参数,换言之它是一个包装的剧本,用来对接 service 下令,这些服务剧本都是 sevice 的实例,通常是在后台运行,好比 ssh network-manager x11-common。许多软件包在安装的时候,都市对应发生一个包装服务,提供 start|stop|restart 参数来利便治理,用户可以自己编写一些剧本放在这个目录下面,用 service 来托管。也可以用系统工具 systemctl 来治理服务,好比 sudo systemctl stop ufw,systemd 的设计很是庞大,有一些人认为它违反了 Unix 的 keep simple keep stupid 的哲学,可是实际事情中,service 和 systemctl 都市用到。除了直接检察目录浏览服务,还可以输入 sudo service --status-all 检察,有一些服务是自动启动的,它们在 /etc/rcX.d/ 中被软毗连,其中 X 是一个数字,表现系统的启动级别,这些目录内里的服务是互斥的,每次启动只会运行一个目录里的服务,可是相同目录下的服务是有先后顺序的,你会发现这些文件中都以 K 或者 S + 数字来命名,S 表现随服务自动启动,K 表现杀死历程,数字表现启动的优先级,好比一个服务要用网络,它的优先级应该在网络服务启动以后。

雷竞技raybet官方网站

上面说到系统的运行级别,你可以输入 runlevel 来确定当前系统的运行级别。wangbo@wangbo-VirtualBox:~$ runlevelN 5 // 运行在图形界面运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动运行级别1:单用户事情状态,root权限,用于系统维护,克制远程登陆运行级别2:多用户状态(没有NFS)运行级别3:完全的多用户状态(有NFS),登陆后进入控制台下令行模式运行级别4:系统未使用,保留运行级别5:X11控制台,登陆后进入图形GUI模式运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动可以使用 init 下令来改变运行级别,好比 init 0 关机,init 6 重启,下面来写一个自己的服务,这个服务很简朴就是打印 sevice 下令后面的参数和当前时间,剧本路径 /etc/init.d/hello,现在还没有学习 shell 剧本的知识,不用在意细节。#!/bin/bashcase "$1" in start) echo start date ;; stop) echo stop date ;; restart) echo restart date ;;esacsudo chmod a+x /etc/init.d/hello 加上执行权限,由于它自己就是个 shell 剧本,可以直接运行,输入 /etc/init.d/hello start 会打印 start 和系统当前时间,输入 sudo service hello start,系统提示找不到 hello.service,还需要注册一下,输入 sudo update-rc.d hello defaults 注册一下,使用 sudo service status 获取状态发现输出了 start 和 时间,由于我们的服务不是常驻的服务,运行完后就退出了,所以系统显示 active (exited)。

雷竞技raybet官方网站

wangbo@wangbo-VirtualBox:~$ sudo chmod a+x /etc/init.d/hellowangbo@wangbo-VirtualBox:~$ sudo service hello startFailed to start hello.service: Unit hello.service not found.wangbo@wangbo-VirtualBox:~$ sudo service hello startwangbo@wangbo-VirtualBox:~$ sudo service hello status● hello.service Loaded: loaded (/etc/init.d/hello; generated) Active: active (exited) since Fri 2020-12-11 11:48:15 CST; 16s ago Docs: man:systemd-sysv-generator(8) Process: 12245 ExecStart=/etc/init.d/hello start (code=exited, status=0/SUCCESS)12月 11 11:48:15 wangbo-VirtualBox systemd[1]: Starting hello.service...12月 11 11:48:15 wangbo-VirtualBox hello[12245]: start12月 11 11:48:15 wangbo-VirtualBox hello[12254]: 2020年 12月 11日 星期五 11:48:15 CST12月 11 11:48:15 wangbo-VirtualBox systemd[1]: Started hello.service.wangbo@wangbo-VirtualBox:~$ sudo service hello restartwangbo@wangbo-VirtualBox:~$ sudo service hello status● hello.service Loaded: loaded (/etc/init.d/hello; generated) Active: active (exited) since Fri 2020-12-11 11:49:07 CST; 1s ago Docs: man:systemd-sysv-generator(8) Process: 12280 ExecStart=/etc/init.d/hello start (code=exited, status=0/SUCCESS)12月 11 11:49:07 wangbo-VirtualBox systemd[1]: Starting hello.service...12月 11 11:49:07 wangbo-VirtualBox hello[12280]: start12月 11 11:49:07 wangbo-VirtualBox hello[12281]: 2020年 12月 11日 星期五 11:49:07 CST12月 11 11:49:07 wangbo-VirtualBox systemd[1]: Started hello.service.再次运行 sudo service hello stop,获取状态显示服务处于 inactive (dead) 表现服务当前没有运行。好了,这些是系统服务的基本知识,更深入的知识还需要不停的去实践,好比如何让服务开机启动,因为 linux 刊行版的区别,可能实现上稍有差异或者有一些坑,都是履历问题,掌握它的设计机制,通过下令去验证自己的想法是最重要的。


本文关键词:明,白话,linux,教程,-11-,系统,服务,雷竞技raybet官方网站入口,治理,当你

本文来源:雷竞技raybet官方网站-www.fasaija.com