当前位置: 首页 > news >正文

Shell脚本学习-for循环结构2

案例:通过脚本实现仅sshd、rsyslog、crond、network、sysstat服务在开机时自启动。

Linux系统在开机的服务通常工作在文本模式3级别,因此只需要查找3级别以上的开启的服务即可。查看命令:

chkconfig --list |grep 3:on
[root@vm1 ~]# chkconfig --list |grep 3:onNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd configuration.If you want to list systemd services use 'systemctl list-unit-files'.To see services enabled on particular target use'systemctl list-dependencies [target]'.network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@vm1 ~]#

默认情况下开启的服务比较少,只有network。

[root@vm1 ~]# for name in `chkconfig --list |grep 3:on|awk '{print $1}'`;do chkconfig --level 3 $name off;doneNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd configuration.If you want to list systemd services use 'systemctl list-unit-files'.To see services enabled on particular target use'systemctl list-dependencies [target]'.[root@vm1 ~]# for name in crond network rsyslog sshd sysstat;do chkconfig --level 3 $name on;done
Note: Forwarding request to 'systemctl enable crond.service'.
Note: Forwarding request to 'systemctl enable rsyslog.service'.
Note: Forwarding request to 'systemctl enable sshd.service'.
error reading information on service sysstat: No such file or directory[root@vm1 ~]# chkconfig --list |grep 3:on

说明:将默认开启的服务都关闭,然后开启需要开启的服务。

方法2:

[root@vm1 ~]# for name in `chkconfig --list|grep 3:on|awk '{print $1}'|grep -vE "crond|network|sshd|rsyslog|sysstat"`;do chkconfig $name off;doneNote: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by nativesystemd configuration.If you want to list systemd services use 'systemctl list-unit-files'.To see services enabled on particular target use'systemctl list-dependencies [target]'.

Linux系统管理员,需要遵循的原则是最小化原则,尽量不安装不使用的软件,尽量不开启不需要开启的服务,只要不用,就不开启。

我们再理解下for循环语句的结构。chkconfig命令获取到取值变量列表,然后for循环语句“遍历”这个列表,进行处理。

http://www.lryc.cn/news/111589.html

相关文章:

  • vue 老项目 npm install 报错Python,c++等相关错误
  • 【c语言初级】c++基础
  • idea打开传统eclipse项目
  • 全国各城市-财政收入-一般预算收入-各项税收-个人所得税(1999-2020年)
  • 【动态网页抓取】 :用Python抓取所有内容的指南
  • Spring Boot数据访问基础知识与JDBC简单实现
  • ubuntu添加万能头文件
  • 聊一聊关于前端语法 ?? 的那些事
  • 宝塔Linux面板升级“获取更新包失败”怎么解决?
  • 训练强化学习的经验回放策略:experience replay
  • uniapp学习
  • 机器学习深度学习——数值稳定性和模型化参数(详细数学推导)
  • layui 整合UEditor 百度编辑器
  • 1、sparkStreaming概述
  • 【Spring Boot】Spring Boot 集成 RocketMQ 实现简单的消息发送和消费
  • uniapp:图片验证码检验问题处理
  • 将Visio和Excel导出成没有白边的PDF文件
  • String类及其工具类
  • 踩坑(5)整合kafka 报错 java.net.UnknownHostException: 不知道这样的主机
  • rust持续学习 get_or_insert_with
  • 卡尔曼滤波 | Matlab实现无迹kalman滤波仿真
  • C++---list常用接口和模拟实现
  • [openCV]基于赛道追踪的智能车巡线方案V1
  • SpringIoc-个人学习笔记
  • 【一文搞懂泛型】
  • 概念解析 | 利用MIMO雷达技术实现高性能目标检测的关键技术解析
  • Grafana制作图表-自定义Flink监控图表
  • 【TypeScript】初识TypeScript和变量类型介绍
  • 阿里云瑶池 PolarDB 开源官网焕新升级上线
  • 泡水书为什么不能再出售