1、简述systemd的新特性及unit常见类型分析,能够实现编译安装的如nginx\apache实现通过systemd来管理
系统启动流程
内核空间 –> 用户空间
- 内核空间
- 初始化
- ro挂载rootfs
- 启动init进程
- 用户空间
- 默认运行级别
- 初始化
- 启动或停止服务
- 打印登陆提示符
用户空间初始化程序
- c5: init: 串行启动程序
- c6: upstart:基于dbus信号完成并行启动
- c7: systemd: 并行启动进程
systemd进程特性
1)并行启动进程
2)按需启动进程
3)快照
4)基于依赖关系定义服务控制逻辑
unit类型
- .service: 定义系统服务,扮演/etc/rc.d/init.d/* 各服务脚本。
- .target:模拟“运行级别”。
- .device:定义内核识别的设备。
- .mount:定义文件系统挂载点。
- .socket:标识进程间通信的socket文件。
- .snapshot:管理系统快照。
- .swap:标识swap设备。
- .automount:文件系统自动挂载点设置。
- .path:定义文件系统中的一个文件或目录。
unit文件路径
/usr/lib/systemd/system /run/systemd/system /etc/systemd/system
编译安装的如nginx\apache实现通过systemd来管理
~]# yum -y groupinstall "Development Tools" "Server Platform Development" apache ~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.2.34.tar.gz ~]# tar xf httpd-2.2.34.tar.gz ~]# cd httpd-2.2.34 [root@localhost httpd-2.2.29]# ./configure --prefix=/usr/local/apache2 --sysconfdir=/etc/apache2 [root@localhost httpd-2.2.29]# make -j 4 [root@localhost httpd-2.2.29]# make install ~]# vim /etc/rc.d/init.d/httpd#!/bin/bash # # httpd Startup script for the Apache HTTP Server # # chkconfig: - 85 15 # description: The Apache HTTP Server is an efficient and extensible \ # server implementing the current HTTP standards. if [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpd fi prog=${HTTPD-/usr/local/apache2/bin/httpd} [ -L /usr/sbin/httpd ] || ln -sv $prog /usr/sbin lockfile=${LOCKFILE-/var/lock/subsys/httpd} prog=$(basename $prog) check_prog() { [ -f $lockfile ] && RETVAL=0 || RETVAL=1 return $RETVAL } lcc_start() { check_prog if [ $RETVAL -eq 0 ]; then echo "$prog is started." else #$prog $OPTIONS -DFOREGROUND $prog $OPTIONS if [ $? -eq 0 ]; then touch $lockfile echo "$prog start ok" else echo "$prog start fail" fi fi } lcc_stop() { check_prog if [ $RETVAL -eq 0 ]; then kill -9 `pgrep $prog` &> /dev/null rm -f $lockfile [ $? -eq 0 ] && echo "$prog stop finished." || echo "$prog stop fail." else echo "$prog is stopped." fi } lcc_restart() { lcc_stop lcc_start } lcc_status() { check_prog [ $? -eq 0 ] && echo "$prog is running." || echo "$prog is stopping" } case "$1" in start|stop|restart|status) lcc_$1 ;; *) echo $"Usage: $prog {start|stop|restart|status}" RETVAL=2 ;; esac exit $RETVAL~]# chkconfig --add httpd
nginx wget https://nginx.org/download/nginx-1.12.2.tar.gz yum -y groupinstall "Development Tools" "Server Platform Development" yum -y install pcre-devel openssl-devel yum -y install zlib-devel libxml2-devel libxslt-devel gd-devel perl-devel GeoIP-devel perl-ExtUtils-Embed tar xf nginx-1.12.2.tar.gz cd nginx-1.12.2 ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio make && make install cat > /usr/lib/systemd/system/nginx.service << EOF [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid # Nginx will fail to start if /run/nginx.pid already exists but has the wrong # SELinux context. This might happen when running `nginx -t` from the cmdline. # https://bugzilla.redhat.com/show_bug.cgi?id=1268621 ExecStartPre=/usr/bin/rm -f /run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target EOF systemctl daemon-reload useradd -r nginx install -d -o nginx -g nginx /var/lib/nginx/tmp/client_body systemctl start nginx.service systemctl status nginx.service
2、描述awk命令用法及示例(至少3例)
示例:
- 显示所有用户默认是/bin/bash的详细信息
~]# awk -F: '$NF=="/bin/bash"{print}' /etc/passwd
- 显示第3至5行的所有/etc/passwd中的信息
~]# awk -F: 'NR>=3&&NR<=5{print}' /etc/passwd
- 匹配以bash结尾的行
~]# awk -F: '$NF~/bash$/{print}' /etc/passwd
- ID号为偶数就显示
~]# awk ‘$3%2==0{print}’ /etc/passwd
例如:
- 以UUID开头的行,显示
~]# awk '/^UUID/{print}' /etc/fstab
- 非UUID开头的行,显示
~]# awk '!/^UUID/{print}' /etc/fstab
- ID大于30,显示
~]# awk '$3>30{print}' /etc/passwd
- 显示表头:
~]# awk 'BEGIN{print " username uid \n----------"}'
- 显示结尾:
~]# awk 'END{print "=======================\n END "}'
- 显示用户名及默认shell
~]# awk -F: 'BEGIN{print "----START---"}{printf "%25s %s\n",$1,$NF}END{print "---END---"}' /etc/passwd
- id大于等于1000普通用户
~]# awk -F: ‘{if($3>=1000){print $1,”Common User”} else {print $1,”root or Sysuser”}}’ /etc/passwd
- 显示每行第字段长度
~]# awk ‘{i=1;while(i<=NF) {print $i,length($i);i++}}’ /etc/fstab
~]# awk ‘{for(i=1;i<=NF;i++) {print $i,length($i)}}’ /etc/fstab
- 循环嵌套, 字段长度大于7时显示
~]# awk ‘{i=1;while(i<=NF) {if(length($i)>7){print $i,length($i)};i++}}’ /etc/fstab
~]# awk ‘{for(i=1;i<=NF;i++) {if(length($i)>7){print $i,length($i)}}}’ /etc/fstab
- 统计状态:
~]# netstat -tan | awk '/^tcp\>/{state[$NF]++} END{for(i in state) {print i,state[i]}}'
- 统计IP
~]# awk '{ip[$1]++} END{for(i in ip) {print i,ip[i]}}' /var/log/httpd/access_log
- 统计流量
~]# awk '{a+=1} END{print a}' /var/log/httpd/access_log 假如每个访问1kb消耗,那么最终消耗...
3、描述awk函数示例(至少3例)
- 随机数
~]# awk 'BEGIN{print rand()}'
- 返回字段长度
~]# awk -F: '{print $1,length($1)}' /etc/passwd
- 将/etc/passwd中$1的o替换为O,第一次出现替换.
~]# awk -F: '{sub("o","O",$1);print $1}' /etc/passwd
- 将/etc/passwd中$1的o替换为O,所有出现替换.
~]# awk -F: '{gsub("o","O",$1);print $1}' /etc/passwd
- df -h中Use%大于等于80就显示
~]# df -lhP | awk '/^\/dev/{split($5,state,"%"); if (state[1]>=80)print }'
- 统计ip
~]# netstat -tnl | grep "^tcp\>" | awk '{split($4,ip,":");count[ip[1]]++} END{for(i in count) {print i,count[i]}}'
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/91256
评论列表(1条)
系统启动过程写的简单了,可以再深入一下,systemd的实现和awk的例子非常不错,继续加油~