CentOS7-systemd雜記

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
[email protected]
basic.target
basic.target.wants
blk-availability.service
bluetooth.target
brandbot.path
brandbot.service
chrony-dn***[email protected]

啓動服務

[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
  [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

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

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

切換圖形界面,並關閉圖形界面進程

[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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章