systemd以及awk等相关用法

2018-10-05  本文已影响0人  佐岸的咖啡

(1)、简述systemd的新特性及unit常见类型分析,能够实现编译安装的如nginx\apache实现通过systemd来管理

Systemd

POST--->Boot sequeue(BIOS)--->Bootloader(MBR)--->kernel(ramdisk)--->rootfs--->/sbin/init

init

Systemd的新特性

核心概念: unit

/usr/lib/systemd/system
/etc/systemd/system
/run/systemd/system

unit的常见类型

unit单元 文件扩展名 解释说明
Service unit .service 定义系统服务
Target unit .Target 用于模拟实现"运行级别"
Device unit .device 定义内核识别的设备
Mount unit .mount 定义文件系统挂载点
Socket unit .socket 标识进程间通信用到的socket文件
Snapshot unit .snapshot 管理系统快照
Swap unit .swap 标识swap设备
Automount unit .automount 文件系统自动挂载点设备
Path unit .path 定义文件系统中的一文件或目录

关键特性:

不兼容:

管理系统服务:

CentOS 7: service类型的unit文件
动作 CentOS 6 CentOS 7
启动 service NAME start systemctl start NAME.service
停止 service NAME stop systemctl stop NAME.service
重启 service NAME restart systemctl restart NAME.service
状态 service NAME status systemctl status NAME.service
条件式重启 service NAME condrestart systemctl try-restart NAME.service
重载或重启 systemctl reload-or-restart NAME.service
重载或条件式重启 systemctl reload-or-try-restart NAME.service
查看某服务当前激活与否的状态 systemctl is-active NAME.service
查看所有已激活的服务 systemctl list-units --t service
查看所有服务(包含未激活) chkconfig --list systemctl list-units -t NAME.service -a
设置服务开机自启 chkconfig NAME on systemctl enable NAME.service
禁止服务开机自启 chkconfig NAME off systemctl disable NAME.service
查看某服务是否能开机自启 chkconfig --list NAME systemctl is-enabled NAME.service
禁止某服务设定为开机自启 systemctl mask NAME.service
取消此禁止 systemctl umask NAME.service
查看服务依赖关系 systemctl list-dependencies NAME.service

管理target units:

运行级别 对应别名 服务名
0 runlevel0.target poweroff.target
1 runlevel1.target rescue.target
2 runlevel2.target multi-user.target
3 runlevel3.target multi-user.target
4 runlevel4.target multi-user.target
5 runlevel5.target graphical.target
6 runlevel6.target reboot.target
动作 命令操作
级别切换 init N ==> systemctl isolate NAME.target
查看级别 runlevel ==> systemctl list-units -t target
查看所有级别 systemctl list-units -t target -a
获取默认运行级别 systemctl get-default
修改默认运行级别 systemctl set-default NAME.target
切换至紧急救援模式 systemctl rescue
切换至emergency模式 systemctl emergency

其他常用命令:

动作 命令操作
关机 systemctl halt, systemctl poweroff
重启 systemctl reboot
挂起 systemctl suspend
快照 systemctl hibernate
快照并挂起 systemctl hybird-sleep

service unit file

注意: 对于新创建的unit文件或修改了的unit文件,要通知systemd重载此配置文件

systemctl daemon-reload

==================================================================================

编译安装nginx\apache,并通过systemd来管理.

(第一步)====创建用户配置环境====

[root@centos7 ~]#yum install -y pcre
[root@centos7 ~]#yum install -y pcre-devel
[root@centos7 ~]#yum install -y openssl-devel
[root@centos7 ~]#useradd nginx
[root@centos7 ~]#echo "nginx"|passwd --stdin nginx

(第二步)====下载tar包并解压配置====

NGINX官网 http://nginx.org/

[root@centos7 ~]#tar -zvxf nginx-1.15.5.tar.gz -C /usr/local/
[root@centos7 ~]#cd /usr/local/nginx-1.15.5/
[root@centos7 nginx-1.15.5]#./configure
[root@centos7 nginx-1.15.5]#make && make install

(第三步)====编辑配置文件====

[root@centos7 system]#cd /usr/lib/systemd/system
[root@centos7 system]#vim nginx.service
[unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

(第四步)====启动nginx====

[root@centos7 system]#systemctl start nginx.service
[root@centos7 system]#systemctl stop nginx.service
[root@centos7 system]#systemctl restart nginx.service


(2)、描述awk命令用法及示例(至少3例)

基本用法:

print

变量

print命令

操作符

示例1: 判断用户UID,若大于等于1000为普通用户,否则,为系统用户或管理用户,且显示出对应的结果;

awk -F: '{$3>1000?usertype="Common user":usertype="Sysadmin or Sysuser";printf "%10s:%s\n",$1,usertype}' /etc/passwd

PATTERN

示例2: 求/etc/passwd中第二行到第十行的用户,并显示出来;

[root@centos7 ~]#awk -F: '(NR>=2&&NR<=10){print $1}' /etc/passwd

使用场景: 对awk取得的整行或某个字段做条件判断;

示例3: 以if语句判断用户UID,若大于等于1000为普通用户,否则,为系统用户或管理用户,且显示出对应的结果

[root@centos7 ~]#awk -F: '{if($3>=1000){printf "Common User: %s\n",$1}else{printf "root or Sysusers: %s\n",$1}}' /etc/passwd

示例4: 如果某个字段个数大于5个,则显示出来

[root@centos7 ~]#awk '{if(NF>=5) print $0}' /etc/fstab 

示例5: 对df里占用比这一字段进行监控,若设备的占用比超过20%则显示出这一设备名称.

[root@centos7 ~]#df -h|awk -F[%] '/^\//{print $1}'|awk '{if($NF>=20)print $1}' 
/dev/sda1

使用场景: 对一行内的多个字段逐一类似处理时使用;对数组中的各元素逐一处理时使用;

示例6: 对/etc/grub2.cfg中以空格或linux16开头的行进行处理,取出该行内每个字段具体的长度个数;

[root@centos7 ~]#awk '/^[[:space:]]*linux16/{i=1;while(i<=NF){print $i,length($i);i++}}' /etc/grub2.cfg

示例7: 对上述结果,取出长度大于等于7的字段

[root@centos7 ~]#awk '/^[[:space:]]*linux16/{i=1;while(i<=NF){if(length($i)>=7){print $i,length($i)};i++}}' /etc/grub2.cfg 

注意: var会遍历array的每个索引;


(3)、描述awk函数示例(至少3例)

函数

示例:
随机数

[root@centos7 ~]#awk '{print rand()}' /etc/passwd

字符串处理

[root@centos7 ~]#awk 'length>80' /etc/passwd
[root@centos7 ~]#netstat -tan|awk '/^tcp\>/{split($5,ip,":");count[ip[1]]++}END{for(i in count){print i,count[i]}}'
上一篇 下一篇

猜你喜欢

热点阅读