CentOS7-systemd杂记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7-systemd杂记相关的知识,希望对你有一定的参考价值。

systemd

基础

systemd文件

[[email protected] ~]# cd /usr/lib/systemd/system
[[email protected] 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
[email protected]
basic.target
basic.target.wants
blk-availability.service
bluetooth.target
brandbot.path
brandbot.service
[email protected]

启动服务

[[email protected] system]# systemctl start httpd.service

查看服务状态

[[email protected] 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.

停止服务

[[email protected] system]# systemctl stop httpd.service 
[[email protected] 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)

查看服务当前状态

[[email protected] system]# systemctl is-active httpd
active

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

[[email protected] 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
  [email protected]        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

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

[[email protected] 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.
[[email protected] ~]# systemctl enable httpd.service
[[email protected] ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled;

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

[[email protected] ~]# 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 
[email protected]                             disabled
blk-availability.service                    disabled
brandbot.service                            static  
[email protected]                      static  
chrony-wait.service                         disabled

查看所有服务当前状态

[[email protected] ~]# 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
  [email protected]        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

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

[[email protected] ~]# systemctl isolate graphical.target 
[[email protected] ~]# systemctl stop graphical.target

显示所有已装载级别

[[email protected] ~]# 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,包括未装载的

[[email protected] ~]# 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

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

[[email protected] ~]# systemctl get-default 
multi-user.target

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

[[email protected] ~]# 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.

手动修改启动级别

[[email protected] ~]# rm /etc/systemd/system/default.target
[[email protected] ~]# 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’
[[email protected] ~]# systemctl get-default 
multi-user.target

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

[[email protected] ~]# systemctl is-enabled httpd.service
enabled

查看服务的依赖关系

[[email protected] ~]# 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结尾的文件;要统计其下标为偶数的文件中的行数之和;

[[email protected] 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"

显示数组中所有元素

[[email protected] bin]# weekday[0]=Sun
[[email protected] bin]# weekday[1]=Mon
[[email protected] bin]# weekday[8]=Sat
[[email protected] bin]# echo ${weekdays}

[[email protected] bin]# echo ${weekday}
Sun
[[email protected] bin]# echo ${weekday[@]}
Sun Mon Sat

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

[[email protected] bin]# echo ${weekday[@]:1:2}
Mon Sat
[[email protected] bin]# 
[[email protected] bin]# 
[[email protected] bin]# echo ${weekday[@]:2:2}
Sat

字符串处理

字符串切片

[[email protected] bin]# name="Obama"
[[email protected] bin]# echo ${name:1:3}
bam
[[email protected] bin]# echo ${name:1}
bama
[[email protected] bin]# 

取右侧字符串

[[email protected] bin]# echo ${name: -3}
ama

基于模式取字符串

[[email protected] bin]# file="/var/log/message"
[[email protected] bin]# echo ${file#*/}
var/log/message
[[email protected] bin]# file="var/log/message"
[[email protected] bin]# echo ${file#*/}
log/message
[[email protected] bin]# echo ${file##*/}
message
[[email protected] bin]# file="/var/log/message"
[[email protected] bin]# 

[[email protected] bin]# file="/var/log/message/1/2/3"
[[email protected] bin]# echo $file
/var/log/message/1/2/3
[[email protected] bin]# echo ${file##*/}
3

以上是关于CentOS7-systemd杂记的主要内容,如果未能解决你的问题,请参考以下文章

杂记之--苹果4s手机呼叫转移怎么设置

canvas杂记

python杂记

QT 使用杂记

网站优化杂记

Python学习杂记_10_三元运算符