码迷,mamicode.com
首页 > 其他好文 > 详细

CentOS7-systemd杂记

时间:2018-03-27 02:00:41      阅读:439      评论:0      收藏:0      [点我收藏+]

标签:systemd systemctl 杂记

systemd

基础

systemd文件

[root@qq ~]# cd /usr/lib/systemd/system
[root@qq system]# ls
abrt-ccpp.service
abrtd.service
abrt-oops.service
abrt-pstoreoops.service
abrt-vmcore.service
abrt-xorg.service
arp-ethers.service
atd.service
auditd.service
autovt@.service
basic.target
basic.target.wants
blk-availability.service
bluetooth.target
brandbot.path
brandbot.service
chrony-dnssrv@.service

启动服务

[root@qq system]# systemctl start httpd.service

查看服务状态

[root@qq system]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)        #是否允许开机自启动
   Active: active (running) since Mon 2016-07-25 16:31:14 CST; 21h ago      #当前状态
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 15654 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
 Main PID: 15673 (httpd)        #主进程号
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─15673 /usr/sbin/httpd -DFOREGROUND
           ├─15695 /usr/sbin/httpd -DFOREGROUND
           ├─15696 /usr/sbin/httpd -DFOREGROUND
           ├─15697 /usr/sbin/httpd -DFOREGROUND
           ├─15698 /usr/sbin/httpd -DFOREGROUND
           └─15699 /usr/sbin/httpd -DFOREGROUND

Jul 25 16:30:44 qq systemd[1]: Starting The Apache HTTP Server...
Jul 25 16:30:54 qq httpd[15673]: AH00557: httpd: apr_sockaddr_info_...q
Jul 25 16:30:54 qq httpd[15673]: AH00558: httpd: Could not reliably...e
Jul 25 16:31:14 qq systemd[1]: Started The Apache HTTP Server.
Jul 26 14:09:15 qq systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

停止服务

[root@qq system]# systemctl stop httpd.service 
[root@qq system]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)

查看服务当前状态

[root@qq system]# systemctl is-active httpd
active

查看所有服务哪些处于活动状态

[root@qq system]# systemctl list-units --type service
  UNIT                      LOAD   ACTIVE SUB     DESCRIPTION
  abrt-ccpp.service         loaded active exited  Install ABRT coredump
  abrt-oops.service         loaded active running ABRT kernel log watch
  abrtd.service             loaded active running ABRT Automated Bug Re
  atd.service               loaded active running Job spooling tools
  auditd.service            loaded active running Security Auditing Ser
  crond.service             loaded active running Command Scheduler
  dbus.service              loaded active running D-Bus System Message 
  firewalld.service         loaded active running firewalld - dynamic f
  getty@tty1.service        loaded active running Getty on tty1
  httpd.service             loaded active running The Apache HTTP Serve
● kdump.service             loaded failed failed  Crash recovery kernel
  kmod-static-nodes.service loaded active exited  Create list of requir
  libstoragemgmt.service    loaded active running libstoragemgmt plug-i
  lvm2-lvmetad.service      loaded active running LVM2 metadata daemon
  lvm2-monitor.service      loaded active exited  Monitoring of LVM2 mi
  network.service           loaded active exited  LSB: Bring up/down ne
  NetworkManager.service    loaded active running Network Manager

查看所有服务包括未激活的

[root@qq system]# systemctl list-units --type service --all
  UNIT                      LOAD      ACTIVE   SUB     DESCRIPTION
  abrt-ccpp.service         loaded    active   exited  Install ABRT cor
  abrt-oops.service         loaded    active   running ABRT kernel log 
  abrt-vmcore.service       loaded    inactive dead    Harvest vmcores 
  abrt-xorg.service         loaded    inactive dead    ABRT Xorg log wa
  abrtd.service             loaded    active   running ABRT Automated B
  atd.service               loaded    active   running Job spooling too
  auditd.service            loaded    active   running Security Auditin
  brandbot.service          loaded    inactive dead    Flexible Brandin
  cpupower.service          loaded    inactive dead    Configure CPU po
  crond.service             loaded    active   running Command Schedule
  dbus.service              loaded    active   running D-Bus System Mes
● display-manager.service   not-found inactive dead    display-manager.
  dm-event.service          loaded    inactive dead    Device-mapper ev
  dmraid-activation.service loaded    inactive dead    Activation of DM
  dracut-shutdown.service   loaded    inactive dead    Restore /r

设置服务开机自启动

Hint: Some lines were ellipsized, use -l to show in full.
[root@qq ~]# systemctl enable httpd.service
[root@qq ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled;

查看所有服务哪些是开机自启的

[root@qq ~]# systemctl list-unit-files --type service
UNIT FILE                                   STATE   
abrt-ccpp.service                           enabled 
abrt-oops.service                           enabled 
abrt-pstoreoops.service                     disabled
abrt-vmcore.service                         enabled 
abrt-xorg.service                           enabled 
abrtd.service                               enabled 
arp-ethers.service                          disabled
atd.service                                 enabled 
auditd.service                              enabled 
autovt@.service                             disabled
blk-availability.service                    disabled
brandbot.service                            static  
chrony-dnssrv@.service                      static  
chrony-wait.service                         disabled

查看所有服务当前状态

[root@qq ~]# systemctl list-units --type service
  UNIT                      LOAD   ACTIVE SUB     DESCRIPTION
  abrt-ccpp.service         loaded active exited  Install ABRT coredump
  abrt-oops.service         loaded active running ABRT kernel log watch
  abrtd.service             loaded active running ABRT Automated Bug Re
  atd.service               loaded active running Job spooling tools
  auditd.service            loaded active running Security Auditing Ser
  crond.service             loaded active running Command Scheduler
  dbus.service              loaded active running D-Bus System Message 
  firewalld.service         loaded active running firewalld - dynamic f
  getty@tty1.service        loaded active running Getty on tty1
  httpd.service             loaded active running The Apache HTTP Serve
● kdump.service             loaded failed failed  Crash recovery kernel
  kmod-static-nodes.service loaded active exited  Create list of requir
  libstoragemgmt.service    loaded active running libstoragemgmt plug-i
  lvm2-lvmetad.service      loaded active running LVM2 metadata daemon
  lvm2-monitor.service      loaded active exited  Monitoring of LVM2 mi
  network.service           loaded active exited  LSB: Bring up/down ne
  NetworkManager.service    loaded active running Network Manager

切换图形界面,并关闭图形界面进程

[root@qq ~]# systemctl isolate graphical.target 
[root@qq ~]# systemctl stop graphical.target

显示所有已装载级别

[root@qq ~]# systemctl list-units --type target
UNIT                   LOAD   ACTIVE SUB    DESCRIPTION
basic.target           loaded active active Basic System
cryptsetup.target      loaded active active Encrypted Volumes
getty.target           loaded active active Login Prompts
local-fs-pre.target    loaded active active Local File Systems (Pre)
local-fs.target        loaded active active Local File Systems
multi-user.target      loaded active active Multi-User System
network-online.target  loaded active active Network is Online
network.target         loaded active active Network
nfs-client.target      loaded active active NFS client services
nss-user-lookup.target loaded active active User and Group Name Lookups
paths.target           loaded active active Paths
remote-fs-pre.target   loaded active active Remote File Systems (Pre)
remote-fs.target       loaded active active Remote File Systems
slices.target          loaded active active Slices
sockets.target         loaded active active Sockets
swap.target            loaded active active Swap
sysinit.target         loaded active active System Initialization
timers.target          loaded active active Timers

查看所有target,包括未装载的

[root@qq ~]# systemctl list-units --type target --all
  UNIT                   LOAD      ACTIVE   SUB    DESCRIPTION
  basic.target           loaded    active   active Basic System
  bluetooth.target       loaded    inactive dead   Bluetooth
  cryptsetup.target      loaded    active   active Encrypted Volumes
  emergency.target       loaded    inactive dead   Emergency Mode
  final.target           loaded    inactive dead   Final Step
  getty.target           loaded    active   active Login Prompts

获取当前系统启动默认级别

[root@qq ~]# systemctl get-default 
multi-user.target

修改默认启动级别为图形界面

[root@qq ~]# systemctl set-default graphical.target 
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.

手动修改启动级别

[root@qq ~]# rm /etc/systemd/system/default.target
[root@qq ~]# ln -sv /usr/lib/systemd/system/multi-user.target /etc/systemd/system/default.target
‘/etc/systemd/system/default.target’ -> ‘/usr/lib/systemd/system/multi-user.target’
[root@qq ~]# systemctl get-default 
multi-user.target

查看服务是否开机自启(简洁版)

[root@qq ~]# systemctl is-enabled httpd.service
enabled

查看服务的依赖关系

[root@qq ~]# systemctl list-dependencies httpd.service
httpd.service
● ├─-.mount
● ├─system.slice
● ├─tmp.mount
● ├─var.mount
● └─basic.target
●   ├─alsa-restore.service
●   ├─alsa-state.service
●   ├─firewalld.service
●   ├─microcode.service
●   ├─rhel-autorelabel-mark.service
●   ├─rhel-autorelabel.service
●   ├─rhel-configure.service
●   ├─rhel-dmesg.service
●   ├─rhel-loadmodules.service
●   ├─paths.target
●   ├─slices.target
●   │ ├─-.slice
●   │ └─system.slice
●   ├─sockets.target
●   │ ├─avahi-daemon.socket
●   │ ├─cups.socket
●   │ ├─dbus.socket
●   │ ├─dm-event.socket
●   │ ├─iscsid.socket
●   │ ├─iscsiuio.socket
●   │ ├─rpcbind.socket
●   │ ├─systemd-initctl.socket
●   │ ├─systemd-journald.socket
●   │ ├─systemd-shutdownd.socket
●   │ ├─systemd-udevd-control.socke
……

定义一个数组,数组中的元素/var/log目录下所有以.log结尾的文件;要统计其下标为偶数的文件中的行数之和;

[root@qq bin]# cat array2.sh 
#!/bin/bash
#
declare -a files
files=(/var/log/*.log)
declare -i lines=0

for i in $(seq 0 $[${#files[*]}-1]); do
   if [ $[$i%2] -eq 0 ]; then
    let lines+=$(wc -l ${files[$i]} | cut -d‘ ‘ -f1) 
    fi
done

echo "Lines= $lines"

显示数组中所有元素

[root@qq bin]# weekday[0]=Sun
[root@qq bin]# weekday[1]=Mon
[root@qq bin]# weekday[8]=Sat
[root@qq bin]# echo ${weekdays}

[root@qq bin]# echo ${weekday}
Sun
[root@qq bin]# echo ${weekday[@]}
Sun Mon Sat

通过偏移和取数数量取得想要的元素、又称数组切片

[root@qq bin]# echo ${weekday[@]:1:2}
Mon Sat
[root@qq bin]# 
[root@qq bin]# 
[root@qq bin]# echo ${weekday[@]:2:2}
Sat

字符串处理

字符串切片

[root@qq bin]# name="Obama"
[root@qq bin]# echo ${name:1:3}
bam
[root@qq bin]# echo ${name:1}
bama
[root@qq bin]# 

取右侧字符串

[root@qq bin]# echo ${name: -3}
ama

基于模式取字符串

[root@qq bin]# file="/var/log/message"
[root@qq bin]# echo ${file#*/}
var/log/message
[root@qq bin]# file="var/log/message"
[root@qq bin]# echo ${file#*/}
log/message
[root@qq bin]# echo ${file##*/}
message
[root@qq bin]# file="/var/log/message"
[root@qq bin]# 

[root@qq bin]# file="/var/log/message/1/2/3"
[root@qq bin]# echo $file
/var/log/message/1/2/3
[root@qq bin]# echo ${file##*/}
3

CentOS7-systemd杂记

标签:systemd systemctl 杂记

原文地址:http://blog.51cto.com/zhongle21/2091434

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!