【2018.05.11学习笔记】【linux基础知识10.23-10.27】

10.23 linux任务计划cron

我们可能有这样的需求:在凌晨的时候做一些操作,例如备份数据、重启服务等。这样就需要借助任务计划去实现。

linux的任务计划服务:cron。cron的配置文件定义了执行的SHELL,执行命令的PATH等,还定义了时间的格式。

[root@lgs ~]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

编辑任务计划:crontab -e,像vim一样的操作进行编辑。

[root@lgs ~]# crontab -e
no crontab for root - using an empty one

0 3 * * * /bin/bash /usr/123.sh >>/tmp/123.log 2>>/tmp/123.log

crontab: installing new crontab
[root@lgs ~]# cat /var/spool/cron/root
0 3 * * * /bin/bash /usr/123.sh >>/tmp/123.log 2>>/tmp/123.log

每月的1-10号执行:

0 3 1-10 * * /bin/bash /usr/123.sh >>/tmp/123.log 2>>/tmp/123.log

双月份执行:

0 3 * */2 * /bin/bash /usr/123.sh >>/tmp/123.log 2>>/tmp/123.log

双月份的周2、周5执行,用逗号分割:

0 3 * */2 2,5 /bin/bash /usr/123.sh >>/tmp/123.log 2>>/tmp/123.log

确保cron会执行,要启动crond服务:

[root@lgs ~]# systemctl start crond
[root@lgs ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 日 2018-05-13 11:47:51 CST; 10min ago
 Main PID: 572 (crond)
   CGroup: /system.slice/crond.service
           └─572 /usr/sbin/crond -n

5月 13 11:47:51 lgs systemd[1]: Started Command Scheduler.
5月 13 11:47:51 lgs systemd[1]: Starting Command Scheduler...
5月 13 11:47:51 lgs crond[572]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 74% if used.)
5月 13 11:47:51 lgs crond[572]: (CRON) INFO (running with inotify support)

为什么有些时候,写了计划,就是没有按计划执行:

可能是脚本里的命令,没有用到绝对路径。这是因为crontab的配置文件里定义的PATH,没有你执行的命令的路径。解决办法就是执行的脚本命令要写绝对路径,要么把命令的绝对路径加入到crontab的配置文件里的PATH中去。

一般尽量别修改配置文件,而是命令用绝对路径编写比较好。

任务计划要追加执行日志,方便以后可查,养成好的习惯。

列出系统的任务计划:

[root@lgs ~]# crontab -l
0 3 * * * /bin/bash /usr/123.sh >>/tmp/123.log 2>>/tmp/123.log

备份任务计划:备份文件即可,一般以用户的名字命名的cron文件。

[root@lgs ~]# ls -l /var/spool/cron/
总用量 4
-rw------- 1 root root 63 5月  13 11:54 root

-u选项:指定用户。不指定默认是root。

[root@lgs ~]# crontab -u root -l
0 3 * * * /bin/bash /usr/123.sh >>/tmp/123.log 2>>/tmp/123.log
[root@lgs ~]# crontab -u lgs -l
no crontab for lgs

-r选项:删除计划

[root@lgs ~]# crontab -r 
[root@lgs ~]# crontab -u root -l
no crontab for root

10.24 chkconfig工具

系统里像crond、iptables、nginx、httpd、mysql都是服务的形式运行。系统怎么去控制管理这些服务的启动、开机启动、指定级别开机启动等。就要用到chkconfig。

linux的系统服务管理:CentOS 5、6的时候使用chkconfig。

CentOS7也可以用chkconfig,但是比较少用,主要用systemd来管理服务。

列出服务:用chkconfig只列出用chkconfig管理的服务,即sysV机制的,不含systemd管理的
[root@lgs ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关

CentOS 5、6:进程id为1 的是init(sysV机制),在/etc/init.d/ 下

CentOS7:进程id为1 的是 systemd。

某服务关闭开机启动:(针对2345级别,因为016级别开机启动没意义)

[root@lgs ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
[root@lgs ~]# chkconfig mysqld off
[root@lgs ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:关	3:关	4:关	5:关	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关

某服务开机启动:(针对2345级别,因为016级别开机启动没意义)

[root@lgs ~]# chkconfig mysqld on
[root@lgs ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关

指定级别开机启动或关闭:

[root@lgs ~]# chkconfig --level 45 mysqld off
[root@lgs ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:关	5:关	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关

要把用户自定义(自己安装的)服务加入chkconfig列表去管理:chkconfig --add

(服务的文件必须在/etc/init.d/ 下)

[root@lgs ~]# chkconfig --add 123
服务 123 信息读取出错:没有那个文件或目录
[root@lgs ~]# cd /etc/init.d/
[root@lgs init.d]# ls
functions  httpd  mysqld  netconsole  network  README
[root@lgs init.d]# cp network 123
[root@lgs init.d]# chkconfig --add 123
[root@lgs init.d]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

123            	0:关	1:关	2:开	3:开	4:开	5:开	6:关
mysqld         	0:关	1:关	2:开	3:开	4:关	5:关	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关

123脚本或者服务要被chkconfig识别,必须在脚本里编写以下参数

chkconfig: 2345 10 90:2345运行级别启动、10位启动、90位关闭。

[root@lgs init.d]# cat 123

# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
#              start at boot time.

删除列表中的服务:

[root@lgs init.d]# chkconfig --del 123
[root@lgs init.d]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:关	5:关	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关

10.25 systemd管理服务

在CentOS7中, 主要用systemd管理服务,chkconfig已经很少使用。

查看所有服务:service、target、socket等类型。

[root@lgs ~]# systemctl list-unit-files
UNIT FILE                                     STATE   
proc-sys-fs-binfmt_misc.automount             static  
dev-hugepages.mount                           static  
dev-mqueue.mount                              static  
proc-sys-fs-binfmt_misc.mount                 static  
sys-fs-fuse-connections.mount                 static  
sys-kernel-config.mount                       static  
sys-kernel-debug.mount                        static  
tmp.mount                                     disabled
brandbot.path                                 disabled
systemd-ask-password-console.path             static  
systemd-ask-password-plymouth.path            static  
systemd-ask-password-wall.path                static  
session-2.scope                               static  
session-4.scope                               static  
accounts-daemon.service                       enabled 
arp-ethers.service                            disabled
auditd.service                                enabled 
autovt@.service                               enabled 
blk-availability.service                      disabled
bluetooth.service                             enabled 
brandbot.service                              static  
canberra-system-bootup.service                disabled
canberra-system-shutdown-reboot.service       disabled
canberra-system-shutdown.service              disabled
chrony-dnssrv@.service                        static  
chrony-wait.service                           disabled
chronyd.service                               enabled 
colord.service                                static  
console-getty.service                         disabled
console-shell.service                         disabled
container-getty@.service                      static  
cpupower.service                              disabled
crond.service                                 enabled 
dbus-org.bluez.service                        enabled 
dbus-org.freedesktop.hostname1.service        static  
dbus-org.freedesktop.import1.service          static  
dbus-org.freedesktop.locale1.service          static  
dbus-org.freedesktop.login1.service           static  
dbus-org.freedesktop.machine1.service         static  
dbus-org.freedesktop.NetworkManager.service   enabled 
dbus-org.freedesktop.nm-dispatcher.service    enabled 
dbus-org.freedesktop.timedate1.service        static  
dbus.service                                  static  
debug-shell.service                           disabled
display-manager.service                       enabled 
dm-event.service                              disabled
dracut-cmdline.service                        static  
dracut-initqueue.service                      static  
dracut-mount.service                          static  
dracut-pre-mount.service                      static  
dracut-pre-pivot.service                      static  
dracut-pre-trigger.service                    static  
dracut-pre-udev.service                       static  
dracut-shutdown.service                       static  
ebtables.service                              disabled
emergency.service                             static  
firewalld.service                             disabled
fstrim.service                                static  
gdm.service                                   enabled 
geoclue.service                               static  
getty@.service                                enabled 
halt-local.service                            static  
initrd-cleanup.service                        static  
initrd-parse-etc.service                      static  
initrd-switch-root.service                    static  
initrd-udevadm-cleanup-db.service             static  
ip6tables.service                             disabled
iprdump.service                               disabled
iprinit.service                               disabled
iprupdate.service                             disabled
iptables.service                              enabled 
irqbalance.service                            enabled 
kdump.service                                 enabled 
kmod-static-nodes.service                     static  
lvm2-lvmetad.service                          disabled
lvm2-lvmpolld.service                         disabled
lvm2-monitor.service                          enabled 
lvm2-pvscan@.service                          static  
messagebus.service                            static  
microcode.service                             enabled 
NetworkManager-dispatcher.service             enabled 
NetworkManager-wait-online.service            disabled
NetworkManager.service                        enabled 
oddjobd.service                               disabled
plymouth-halt.service                         disabled
plymouth-kexec.service                        disabled
plymouth-poweroff.service                     disabled
plymouth-quit-wait.service                    disabled
plymouth-quit.service                         disabled
plymouth-read-write.service                   disabled
plymouth-reboot.service                       disabled
plymouth-start.service                        disabled
plymouth-switch-root.service                  static  
polkit.service                                static  
postfix.service                               enabled 
quotaon.service                               static  
rc-local.service                              static  
rdisc.service                                 disabled
realmd.service                                static  
rescue.service                                static  
rhel-autorelabel-mark.service                 static  
rhel-autorelabel.service                      static  
rhel-configure.service                        static  
rhel-dmesg.service                            disabled
rhel-domainname.service                       disabled
rhel-import-state.service                     static  
rhel-loadmodules.service                      static  
rhel-readonly.service                         static  
rsyslog.service                               enabled 
rtkit-daemon.service                          enabled 
selinux-policy-migrate-local-changes@.service static  
serial-getty@.service                         disabled
sshd-keygen.service                           static  
sshd.service                                  enabled 
sshd@.service                                 static  
sysstat.service                               enabled 
systemd-ask-password-console.service          static  
systemd-ask-password-plymouth.service         static  
systemd-ask-password-wall.service             static  
systemd-backlight@.service                    static  
systemd-binfmt.service                        static  
systemd-bootchart.service                     disabled
systemd-firstboot.service                     static  
systemd-fsck-root.service                     static  
systemd-fsck@.service                         static  
systemd-halt.service                          static  
systemd-hibernate-resume@.service             static  
systemd-hibernate.service                     static  
systemd-hostnamed.service                     static  
systemd-hwdb-update.service                   static  
systemd-hybrid-sleep.service                  static  
systemd-importd.service                       static  
systemd-initctl.service                       static  
systemd-journal-catalog-update.service        static  
systemd-journal-flush.service                 static  
systemd-journald.service                      static  
systemd-kexec.service                         static  
systemd-localed.service                       static  
systemd-logind.service                        static  
systemd-machine-id-commit.service             static  
systemd-machined.service                      static  
systemd-modules-load.service                  static  
systemd-nspawn@.service                       disabled
systemd-poweroff.service                      static  
systemd-quotacheck.service                    static  
systemd-random-seed.service                   static  
systemd-readahead-collect.service             enabled 
systemd-readahead-done.service                indirect
systemd-readahead-drop.service                enabled 
systemd-readahead-replay.service              enabled 
systemd-reboot.service                        static  
systemd-remount-fs.service                    static  
systemd-rfkill@.service                       static  
systemd-shutdownd.service                     static  
systemd-suspend.service                       static  
systemd-sysctl.service                        static  
systemd-timedated.service                     static  
systemd-tmpfiles-clean.service                static  
systemd-tmpfiles-setup-dev.service            static  
systemd-tmpfiles-setup.service                static  
systemd-udev-settle.service                   static  
systemd-udev-trigger.service                  static  
systemd-udevd.service                         static  
systemd-update-done.service                   static  
systemd-update-utmp-runlevel.service          static  
systemd-update-utmp.service                   static  
systemd-user-sessions.service                 static  
systemd-vconsole-setup.service                static  
tcsd.service                                  disabled
teamd@.service                                static  
tuned.service                                 enabled 
upower.service                                disabled
vgauthd.service                               enabled 
vmtoolsd.service                              enabled 
wpa_supplicant.service                        disabled
-.slice                                       static  
machine.slice                                 static  
system.slice                                  static  
user-0.slice                                  static  
user.slice                                    static  
dbus.socket                                   static  
dm-event.socket                               enabled 
lvm2-lvmetad.socket                           enabled 
lvm2-lvmpolld.socket                          enabled 
sshd.socket                                   disabled
syslog.socket                                 static  
systemd-initctl.socket                        static  
systemd-journald.socket                       static  
systemd-shutdownd.socket                      static  
systemd-udevd-control.socket                  static  
systemd-udevd-kernel.socket                   static  
basic.target                                  static  
bluetooth.target                              static  
cryptsetup-pre.target                         static  
cryptsetup.target                             static  
ctrl-alt-del.target                           disabled
default.target                                enabled 
emergency.target                              static  
final.target                                  static  
getty.target                                  static  
graphical.target                              static  
halt.target                                   disabled
hibernate.target                              static  
hybrid-sleep.target                           static  
initrd-fs.target                              static  
initrd-root-fs.target                         static  
initrd-switch-root.target                     static  
initrd.target                                 static  
iprutils.target                               disabled
kexec.target                                  disabled
local-fs-pre.target                           static  
local-fs.target                               static  
machines.target                               disabled
multi-user.target                             enabled 
network-online.target                         static  
network-pre.target                            static  
network.target                                static  
nss-lookup.target                             static  
nss-user-lookup.target                        static  
paths.target                                  static  
poweroff.target                               disabled
printer.target                                static  
reboot.target                                 disabled
remote-fs-pre.target                          static  
remote-fs.target                              enabled 
rescue.target                                 disabled
rpcbind.target                                static  
runlevel0.target                              disabled
runlevel1.target                              disabled
runlevel2.target                              enabled 
runlevel3.target                              enabled 
runlevel4.target                              enabled 
runlevel5.target                              static  
runlevel6.target                              disabled
shutdown.target                               static  
sigpwr.target                                 static  
sleep.target                                  static  
slices.target                                 static  
smartcard.target                              static  
sockets.target                                static  
sound.target                                  static  
suspend.target                                static  
swap.target                                   static  
sysinit.target                                static  
system-update.target                          static  
time-sync.target                              static  
timers.target                                 static  
umount.target                                 static  
chrony-dnssrv@.timer                          disabled
fstrim.timer                                  disabled
systemd-readahead-done.timer                  indirect
systemd-tmpfiles-clean.timer                  static  

252 unit files listed.
lines 210-255/255 (END)

指定类型list:

[root@lgs ~]# systemctl list-units --type=service
  UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
  auditd.service                     loaded active running Security Auditing Service
  chronyd.service                    loaded active running NTP client/server
  crond.service                      loaded active running Command Scheduler
  dbus.service                       loaded active running D-Bus System Message Bus
  getty@tty1.service                 loaded active running Getty on tty1
  iptables.service                   loaded active exited  IPv4 firewall with iptables
  irqbalance.service                 loaded active running irqbalance daemon
  kdump.service                      loaded active exited  Crash recovery kernel arming
  kmod-static-nodes.service          loaded active exited  Create list of required static device nodes for the current kernel
  lvm2-lvmetad.service               loaded active running LVM2 metadata daemon
  lvm2-monitor.service               loaded active exited  Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling
  lvm2-pvscan@8:17.service           loaded active exited  LVM2 PV scan on device 8:17
  lvm2-pvscan@8:18.service           loaded active exited  LVM2 PV scan on device 8:18
  lvm2-pvscan@8:19.service           loaded active exited  LVM2 PV scan on device 8:19
  mysqld.service                     loaded active running LSB: start and stop MySQL
  network.service                    loaded active exited  LSB: Bring up/down networking
  NetworkManager-wait-online.service loaded active exited  Network Manager Wait Online
  NetworkManager.service             loaded active running Network Manager
  polkit.service                     loaded active running Authorization Manager
  postfix.service                    loaded active running Postfix Mail Transport Agent
● rc-local.service                   loaded failed failed  /etc/rc.d/rc.local Compatibility
  rhel-autorelabel-mark.service      loaded active exited  Mark the need to relabel after reboot
  rhel-dmesg.service                 loaded active exited  Dump dmesg to /var/log/dmesg

--all选项:显示所有的service(包括active激活的和inactive未激活的)

[root@lgs ~]# systemctl list-units --all --type=service
  UNIT                                                  LOAD      ACTIVE   SUB     DESCRIPTION
  accounts-daemon.service                               loaded    inactive dead    Accounts Service
  auditd.service                                        loaded    active   running Security Auditing Service
  brandbot.service                                      loaded    inactive dead    Flexible Branding Service
  chronyd.service                                       loaded    active   running NTP client/server
  cpupower.service                                      loaded    inactive dead    Configure CPU power related settings
  crond.service                                         loaded    active   running Command Scheduler
  dbus.service                                          loaded    active   running D-Bus System Message Bus
  dm-event.service                                      loaded    inactive dead    Device-mapper event daemon
  dracut-shutdown.service                               loaded    inactive dead    Restore /run/initramfs
  emergency.service                                     loaded    inactive dead    Emergency Shell
● exim.service                                          not-found inactive dead    exim.service
  gdm.service                                           loaded    inactive dead    GNOME Display Manager
  getty@tty1.service                                    loaded    active   running Getty on tty1
  ip6tables.service                                     loaded    inactive dead    IPv6 firewall with ip6tables
  iptables.service                                      loaded    active   exited  IPv4 firewall with iptables
  irqbalance.service                                    loaded    active   running irqbalance daemon
  kdump.service                                         loaded    active   exited  Crash recovery kernel arming
  kmod-static-nodes.service                             loaded    active   exited  Create list of required static device nodes for the current kernel
● ldap.service                                          not-found inactive dead    ldap.service
● lvm2-activation.service                               not-found inactive dead    lvm2-activation.service

开机启动:.service可以不写。

[root@lgs ~]# systemctl enable crond.service

检查是否开机启动:

[root@lgs ~]# systemctl is-enabled crond
enabled

开机不启动:

[root@lgs ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@lgs ~]# systemctl is-enabled crond
disabled

查看服务状态:

[root@lgs ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
   Active: active (running) since 日 2018-05-13 11:47:51 CST; 49min ago
 Main PID: 572 (crond)
   CGroup: /system.slice/crond.service
           └─572 /usr/sbin/crond -n

5月 13 11:47:51 lgs systemd[1]: Started Command Scheduler.
5月 13 11:47:51 lgs systemd[1]: Starting Command Scheduler...
5月 13 11:47:51 lgs crond[572]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 74% if used.)
5月 13 11:47:51 lgs crond[572]: (CRON) INFO (running with inotify support)

单独开启、停止、重启服务:

[root@lgs ~]# systemctl stop crond
[root@lgs ~]# systemctl start crond
[root@lgs ~]# systemctl restart crond

如果一个服务设置开机启动后,会生成一个软链接文件:

如果设置开机不启动后,软链接文件就会被删除。

[root@lgs ~]# systemctl enable crond
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

[root@lgs ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service 
lrwxrwxrwx 1 root root 37 5月  13 12:40 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service

[root@lgs ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@lgs ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service 
ls: 无法访问/etc/systemd/system/multi-user.target.wants/crond.service: 没有那个文件或目录

10.26 unit介绍

systemd开启和监督整个系统是基于unit的概念。unit是由一个与配置文件名同名的名字和类型组成的一个封装单元。

unit有很多类型: service:系统服务; target:多个unit组成的组; device:硬件设备;

mount:文件系统的挂载点; automount:自动挂载点; path:文件或者路径

scope:不是由systemd启动的外部进程; slice:进程组; snapshot:systemd 快照

socket:进程间通信的套接字; swap:swap文件; timer:定时器。

列出正在运行的unit:

[root@lgs ~]# systemctl list-units
  UNIT                                                              LOAD   ACTIVE SUB       DESCRIPTION
  proc-sys-fs-binfmt_misc.automount                                 loaded active waiting   Arbitrary Executable File Formats File System Automount Point
  sys-devices-pci0000:00-0000:00:07.1-ata2-host1-target1:0:0-1:0:0:0-block-sr0.device loaded active plugged   VMware_Virtual_IDE_CDROM_Drive
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged   VMware_Virtual_S 1
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda2.device loaded active plugged   VMware_Virtual_S 2
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda3.device loaded active plugged   VMware_Virtual_S 3
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda.device loaded active plugged   VMware_Virtual_S
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb-sdb1.device loaded active plugged   LVM PV m8V6a8-xkMn-W5tX-YAIQ-30Xz-dqNz-TvZ6yS on
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb-sdb2.device loaded active plugged   LVM PV EoP8W8-B3x1-V2FW-kyY9-dHhy-6eIh-StZvVB on
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb-sdb3.device loaded active plugged   LVM PV DrJCcW-NoZ3-1zUk-u3OE-PdET-rLAb-eyXCyd on
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb.device loaded active plugged   VMware_Virtual_S
  sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device loaded active plugged   82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT Single P
  sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device loaded active plugged   ES1371/ES1373 / Creative Labs CT2518 (Audio PCI 64V/128/5200 / C
  sys-devices-platform-serial8250-tty-ttyS1.device                  loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS1
  sys-devices-platform-serial8250-tty-ttyS2.device                  loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS2
  sys-devices-platform-serial8250-tty-ttyS3.device                  loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS3
  sys-devices-pnp0-00:05-tty-ttyS0.device                           loaded active plugged   /sys/devices/pnp0/00:05/tty/ttyS0
  sys-devices-virtual-block-dm\x2d0.device                          loaded active plugged   /sys/devices/virtual/block/dm-0
  sys-module-configfs.device                                        loaded active plugged   /sys/module/configfs
  sys-subsystem-net-devices-ens33.device                            loaded active plugged   82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT Single P
  -.mount                                                           loaded active mounted   /
  boot.mount                                                        loaded active mounted   /boot
  dev-hugepages.mount                                               loaded active mounted   Huge Pages File System
  dev-mqueue.mount                                                  loaded active mounted   POSIX Message Queue File System
  run-user-0.mount                                                  loaded active mounted   /run/user/0
  sys-kernel-config.mount                                           loaded active mounted   Configuration File System
  sys-kernel-debug.mount                                            loaded active mounted   Debug File System
  brandbot.path                                                     loaded active waiting   Flexible branding
  systemd-ask-password-plymouth.path                                loaded active waiting   Forward Password Requests to Plymouth Directory Watch
  systemd-ask-password-wall.path                                    loaded active waiting   Forward Password Requests to Wall Directory Watch
  session-2.scope                                                   loaded active running   Session 2 of user root
  auditd.service                                                    loaded active running   Security Auditing Service
  chronyd.service                                                   loaded active running   NTP client/server
  crond.service                                                     loaded active running   Command Scheduler
  dbus.service                                                      loaded active running   D-Bus System Message Bus
  getty@tty1.service                                                loaded active running   Getty on tty1
  iptables.service                                                  loaded active exited    IPv4 firewall with iptables
  irqbalance.service                                                loaded active running   irqbalance daemon
  kdump.service                                                     loaded active exited    Crash recovery kernel arming
  kmod-static-nodes.service                                         loaded active exited    Create list of required static device nodes for the current kernel
  lvm2-lvmetad.service                                              loaded active running   LVM2 metadata daemon
  lvm2-monitor.service                                              loaded active exited    Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progr
  lvm2-pvscan@8:17.service                                          loaded active exited    LVM2 PV scan on device 8:17
  lvm2-pvscan@8:18.service                                          loaded active exited    LVM2 PV scan on device 8:18
  lvm2-pvscan@8:19.service                                          loaded active exited    LVM2 PV scan on device 8:19
  mysqld.service                                                    loaded active running   LSB: start and stop MySQL
  network.service                                                   loaded active exited    LSB: Bring up/down networking
  NetworkManager-wait-online.service                                loaded active exited    Network Manager Wait Online
  NetworkManager.service                                            loaded active running   Network Manager
  polkit.service                                                    loaded active running   Authorization Manager
  postfix.service                                                   loaded active running   Postfix Mail Transport Agent
● rc-local.service                                                  loaded failed failed    /etc/rc.d/rc.local Compatibility
  rhel-autorelabel-mark.service                                     loaded active exited    Mark the need to relabel after reboot
  rhel-dmesg.service                                                loaded active exited    Dump dmesg to /var/log/dmesg
  rhel-import-state.service                                         loaded active exited    Import network configuration from initramfs
  rhel-readonly.service                                             loaded active exited    Configure read-only root support
  rsyslog.service                                                   loaded active running   System Logging Service
  sshd.service                                                      loaded active running   OpenSSH server daemon
  sysstat.service                                                   loaded active exited    Resets System Activity Logs
  systemd-journal-flush.service                                     loaded active exited    Flush Journal to Persistent Storage
  systemd-journald.service                                          loaded active running   Journal Service
  systemd-logind.service                                            loaded active running   Login Service
  systemd-random-seed.service                                       loaded active exited    Load/Save Random Seed
  systemd-remount-fs.service                                        loaded active exited    Remount Root and Kernel File Systems
  systemd-sysctl.service                                            loaded active exited    Apply Kernel Variables
  systemd-tmpfiles-setup-dev.service                                loaded active exited    Create Static Device Nodes in /dev
  systemd-tmpfiles-setup.service                                    loaded active exited    Create Volatile Files and Directories
  systemd-udev-trigger.service                                      loaded active exited    udev Coldplug all Devices
  systemd-udevd.service                                             loaded active running   udev Kernel Device Manager
  systemd-update-utmp.service                                       loaded active exited    Update UTMP about System Boot/Shutdown
  systemd-user-sessions.service                                     loaded active exited    Permit User Sessions
  systemd-vconsole-setup.service                                    loaded active exited    Setup Virtual Console
  tuned.service                                                     loaded active running   Dynamic System Tuning Daemon
  vgauthd.service                                                   loaded active running   VGAuth Service for open-vm-tools
  vmtoolsd.service                                                  loaded active running   Service for virtual machines hosted on VMware
  -.slice                                                           loaded active active    Root Slice
  system-getty.slice                                                loaded active active    system-getty.slice
  system-lvm2\x2dpvscan.slice                                       loaded active active    system-lvm2\x2dpvscan.slice
  system-selinux\x2dpolicy\x2dmigrate\x2dlocal\x2dchanges.slice     loaded active active    system-selinux\x2dpolicy\x2dmigrate\x2dlocal\x2dchanges.slice
  system.slice                                                      loaded active active    System Slice
  user-0.slice                                                      loaded active active    User Slice of root
  user.slice                                                        loaded active active    User and Session Slice
  dbus.socket                                                       loaded active running   D-Bus System Message Bus Socket
  dm-event.socket                                                   loaded active listening Device-mapper event daemon FIFOs
  lvm2-lvmetad.socket                                               loaded active running   LVM2 metadata daemon socket
  lvm2-lvmpolld.socket                                              loaded active listening LVM2 poll daemon socket
  systemd-initctl.socket                                            loaded active listening /dev/initctl Compatibility Named Pipe
  systemd-journald.socket                                           loaded active running   Journal Socket
  systemd-shutdownd.socket                                          loaded active listening Delayed Shutdown Socket
  systemd-udevd-control.socket                                      loaded active running   udev Control Socket
  systemd-udevd-kernel.socket                                       loaded active running   udev Kernel Socket
  dev-disk-by\x2duuid-deb5a799\x2d817e\x2d41a2\x2d8ab5\x2d25893d3247b2.swap loaded active active    /dev/disk/by-uuid/deb5a799-817e-41a2-8ab5-25893d3247b2
  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
  paths.target                                                      loaded active active    Paths
  remote-fs.target                                                  loaded active active    Remote File Systems
  slices.target                                                     loaded active active    Slices
  sockets.target                                                    loaded active active    Sockets
  sound.target                                                      loaded active active    Sound Card
  swap.target                                                       loaded active active    Swap
  sysinit.target                                                    loaded active active    System Initialization
  timers.target                                                     loaded active active    Timers
  systemd-tmpfiles-clean.timer                                      loaded active waiting   Daily Cleanup of Temporary Directories

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

108 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
lines 71-116/116 (END)

--all选项:列出所有的units,包括inactive、和失败的。

[root@lgs ~]# systemctl list-units --all
  UNIT                                                             LOAD      ACTIVE   SUB       DESCRIPTION
  proc-sys-fs-binfmt_misc.automount                                loaded    active   waiting   Arbitrary Executable File Formats File System Automount Point
  dev-block-8:17.device                                            loaded    active   plugged   LVM PV m8V6a8-xkMn-W5tX-YAIQ-30Xz-dqNz-TvZ6yS on /dev/sdb1 1
  dev-block-8:18.device                                            loaded    active   plugged   LVM PV EoP8W8-B3x1-V2FW-kyY9-dHhy-6eIh-StZvVB on /dev/sdb2 2
  dev-block-8:19.device                                            loaded    active   plugged   LVM PV DrJCcW-NoZ3-1zUk-u3OE-PdET-rLAb-eyXCyd on /dev/sdb3 3
  dev-cdrom.device                                                 loaded    active   plugged   VMware_Virtual_IDE_CDROM_Drive
  dev-disk-by\x2did-ata\x2dVMware_Virtual_IDE_CDROM_Drive_10000000000000000001.device loaded    active   plugged   VMware_Virtual_IDE_CDROM_Drive
  dev-disk-by\x2did-dm\x2dname\x2dvg1\x2dlv1.device                loaded    active   plugged   /dev/disk/by-id/dm-name-vg1-lv1
  dev-disk-by\x2did-dm\x2duuid\x2dLVM\x2d52fM6w9Z59cxCGS41hhRqVi76fnUmE3jJmFXe58h09mT5xXh1oFbU3dmL7OH1mU9.device loaded    active   plugged   /dev/disk/by-id/
  dev-disk-by\x2did-lvm\x2dpv\x2duuid\x2dDrJCcW\x2dNoZ3\x2d1zUk\x2du3OE\x2dPdET\x2drLAb\x2deyXCyd.device loaded    active   plugged   LVM PV DrJCcW-NoZ3-1zUk-
  dev-disk-by\x2did-lvm\x2dpv\x2duuid\x2dEoP8W8\x2dB3x1\x2dV2FW\x2dkyY9\x2ddHhy\x2d6eIh\x2dStZvVB.device loaded    active   plugged   LVM PV EoP8W8-B3x1-V2FW-
  dev-disk-by\x2did-lvm\x2dpv\x2duuid\x2dm8V6a8\x2dxkMn\x2dW5tX\x2dYAIQ\x2d30Xz\x2ddqNz\x2dTvZ6yS.device loaded    active   plugged   LVM PV m8V6a8-xkMn-W5tX-
  dev-disk-by\x2dpath-pci\x2d0000:00:07.1\x2data\x2d2.0.device     loaded    active   plugged   VMware_Virtual_IDE_CDROM_Drive
  dev-disk-by\x2dpath-pci\x2d0000:00:10.0\x2dscsi\x2d0:0:0:0.device loaded    active   plugged   VMware_Virtual_S
  dev-disk-by\x2dpath-pci\x2d0000:00:10.0\x2dscsi\x2d0:0:0:0\x2dpart1.device loaded    active   plugged   VMware_Virtual_S 1
  dev-disk-by\x2dpath-pci\x2d0000:00:10.0\x2dscsi\x2d0:0:0:0\x2dpart2.device loaded    active   plugged   VMware_Virtual_S 2
  dev-disk-by\x2dpath-pci\x2d0000:00:10.0\x2dscsi\x2d0:0:0:0\x2dpart3.device loaded    active   plugged   VMware_Virtual_S 3
  dev-disk-by\x2dpath-pci\x2d0000:00:10.0\x2dscsi\x2d0:0:1:0.device loaded    active   plugged   VMware_Virtual_S
  dev-disk-by\x2dpath-pci\x2d0000:00:10.0\x2dscsi\x2d0:0:1:0\x2dpart1.device loaded    active   plugged   LVM PV m8V6a8-xkMn-W5tX-YAIQ-30Xz-dqNz-TvZ6yS on /de
  dev-disk-by\x2dpath-pci\x2d0000:00:10.0\x2dscsi\x2d0:0:1:0\x2dpart2.device loaded    active   plugged   LVM PV EoP8W8-B3x1-V2FW-kyY9-dHhy-6eIh-StZvVB on /de
  dev-disk-by\x2dpath-pci\x2d0000:00:10.0\x2dscsi\x2d0:0:1:0\x2dpart3.device loaded    active   plugged   LVM PV DrJCcW-NoZ3-1zUk-u3OE-PdET-rLAb-eyXCyd on /de
  dev-disk-by\x2duuid-02635225\x2ddce3\x2d410a\x2d932d\x2d9e6d38fc22f3.device loaded    active   plugged   /dev/disk/by-uuid/02635225-dce3-410a-932d-9e6d38fc2
  dev-disk-by\x2duuid-9fa2450c\x2d5b1c\x2d4cd6\x2d99a0\x2d8c6baa0dc6f5.device loaded    active   plugged   VMware_Virtual_S 1
  dev-disk-by\x2duuid-deb5a799\x2d817e\x2d41a2\x2d8ab5\x2d25893d3247b2.device loaded    active   plugged   VMware_Virtual_S 2
  dev-disk-by\x2duuid-fefaab3f\x2d8726\x2d4a25\x2d9e55\x2dcea60b974233.device loaded    active   plugged   VMware_Virtual_S 3
  dev-dm\x2d0.device                                               loaded    active   plugged   /dev/dm-0
  dev-mapper-vg1\x2dlv1.device                                     loaded    active   plugged   /dev/mapper/vg1-lv1
  dev-sda.device                                                   loaded    active   plugged   VMware_Virtual_S
  dev-sda1.device                                                  loaded    active   plugged   VMware_Virtual_S 1
  dev-sda2.device                                                  loaded    active   plugged   VMware_Virtual_S 2
  dev-sda3.device                                                  loaded    active   plugged   VMware_Virtual_S 3
  dev-sdb.device                                                   loaded    active   plugged   VMware_Virtual_S
  dev-sdb1.device                                                  loaded    active   plugged   LVM PV m8V6a8-xkMn-W5tX-YAIQ-30Xz-dqNz-TvZ6yS on /dev/sdb1 1
  dev-sdb2.device                                                  loaded    active   plugged   LVM PV EoP8W8-B3x1-V2FW-kyY9-dHhy-6eIh-StZvVB on /dev/sdb2 2
  dev-sdb3.device                                                  loaded    active   plugged   LVM PV DrJCcW-NoZ3-1zUk-u3OE-PdET-rLAb-eyXCyd on /dev/sdb3 3
  dev-sr0.device                                                   loaded    active   plugged   VMware_Virtual_IDE_CDROM_Drive
  dev-ttyS0.device                                                 loaded    active   plugged   /dev/ttyS0
  dev-ttyS1.device                                                 loaded    active   plugged   /dev/ttyS1
  dev-ttyS2.device                                                 loaded    active   plugged   /dev/ttyS2
  dev-ttyS3.device                                                 loaded    active   plugged   /dev/ttyS3
  dev-vg1-lv1.device                                               loaded    active   plugged   /dev/vg1/lv1
  sys-devices-pci0000:00-0000:00:07.1-ata2-host1-target1:0:0-1:0:0:0-block-sr0.device loaded    active   plugged   VMware_Virtual_IDE_CDROM_Drive
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded    active   plugged   VMware_Virtual_S 1
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda2.device loaded    active   plugged   VMware_Virtual_S 2
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda3.device loaded    active   plugged   VMware_Virtual_S 3
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda.device loaded    active   plugged   VMware_Virtual_S
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb-sdb1.device loaded    active   plugged   LVM PV m8V6a8-xkMn-W5tX-YAIQ-30Xz-dqNz-TvZ6
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb-sdb2.device loaded    active   plugged   LVM PV EoP8W8-B3x1-V2FW-kyY9-dHhy-6eIh-StZv
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb-sdb3.device loaded    active   plugged   LVM PV DrJCcW-NoZ3-1zUk-u3OE-PdET-rLAb-eyXC
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb.device loaded    active   plugged   VMware_Virtual_S
  sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device loaded    active   plugged   82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT Sin
  sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device loaded    active   plugged   ES1371/ES1373 / Creative Labs CT2518 (Audio PCI 64V/128/520
  sys-devices-platform-serial8250-tty-ttyS1.device                 loaded    active   plugged   /sys/devices/platform/serial8250/tty/ttyS1
  sys-devices-platform-serial8250-tty-ttyS2.device                 loaded    active   plugged   /sys/devices/platform/serial8250/tty/ttyS2
  sys-devices-platform-serial8250-tty-ttyS3.device                 loaded    active   plugged   /sys/devices/platform/serial8250/tty/ttyS3
  sys-devices-pnp0-00:05-tty-ttyS0.device                          loaded    active   plugged   /sys/devices/pnp0/00:05/tty/ttyS0
  sys-devices-virtual-block-dm\x2d0.device                         loaded    active   plugged   /sys/devices/virtual/block/dm-0
  sys-module-configfs.device                                       loaded    active   plugged   /sys/module/configfs
  sys-subsystem-net-devices-ens33.device                           loaded    active   plugged   82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT Sing
  -.mount                                                          loaded    active   mounted   /
  boot.mount                                                       loaded    active   mounted   /boot
  dev-hugepages.mount                                              loaded    active   mounted   Huge Pages File System
  dev-mqueue.mount                                                 loaded    active   mounted   POSIX Message Queue File System
  proc-sys-fs-binfmt_misc.mount                                    loaded    inactive dead      Arbitrary Executable File Formats File System
  run-user-0.mount                                                 loaded    active   mounted   /run/user/0
  sys-fs-fuse-connections.mount                                    loaded    inactive dead      FUSE Control File System
  sys-kernel-config.mount                                          loaded    active   mounted   Configuration File System
  sys-kernel-debug.mount                                           loaded    active   mounted   Debug File System
  tmp.mount                                                        loaded    inactive dead      Temporary Directory
  brandbot.path                                                    loaded    active   waiting   Flexible branding
  systemd-ask-password-console.path                                loaded    inactive dead      Dispatch Password Requests to Console Directory Watch
  systemd-ask-password-plymouth.path                               loaded    active   waiting   Forward Password Requests to Plymouth Directory Watch
  systemd-ask-password-wall.path                                   loaded    active   waiting   Forward Password Requests to Wall Directory Watch
  session-2.scope                                                  loaded    active   running   Session 2 of user root
  accounts-daemon.service                                          loaded    inactive dead      Accounts Service
  auditd.service                                                   loaded    active   running   Security Auditing Service
  brandbot.service                                                 loaded    inactive dead      Flexible Branding Service
  chronyd.service                                                  loaded    active   running   NTP client/server
  cpupower.service                                                 loaded    inactive dead      Configure CPU power related settings
  crond.service                                                    loaded    active   running   Command Scheduler
  dbus.service                                                     loaded    active   running   D-Bus System Message Bus
  dm-event.service                                                 loaded    inactive dead      Device-mapper event daemon
  dracut-shutdown.service                                          loaded    inactive dead      Restore /run/initramfs
  emergency.service                                                loaded    inactive dead      Emergency Shell
● exim.service                                                     not-found inactive dead      exim.service
  gdm.service                                                      loaded    inactive dead      GNOME Display Manager
  getty@tty1.service                                               loaded    active   running   Getty on tty1
  ip6tables.service                                                loaded    inactive dead      IPv6 firewall with ip6tables
  iptables.service                                                 loaded    active   exited    IPv4 firewall with iptables
  irqbalance.service                                               loaded    active   running   irqbalance daemon
  kdump.service                                                    loaded    active   exited    Crash recovery kernel arming
  kmod-static-nodes.service                                        loaded    active   exited    Create list of required static device nodes for the current ke
● ldap.service                                                     not-found inactive dead      ldap.service
● lvm2-activation.service                                          not-found inactive dead      lvm2-activation.service
  lvm2-lvmetad.service                                             loaded    active   running   LVM2 metadata daemon
  lvm2-lvmpolld.service                                            loaded    inactive dead      LVM2 poll daemon
  lvm2-monitor.service                                             loaded    active   exited    Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or p
  lvm2-pvscan@8:17.service                                         loaded    active   exited    LVM2 PV scan on device 8:17
  lvm2-pvscan@8:18.service                                         loaded    active   exited    LVM2 PV scan on device 8:18
  lvm2-pvscan@8:19.service                                         loaded    active   exited    LVM2 PV scan on device 8:19
  microcode.service                                                loaded    inactive dead      Load CPU microcode update
  mysqld.service                                                   loaded    active   running   LSB: start and stop MySQL
  network.service                                                  loaded    active   exited    LSB: Bring up/down networking
  NetworkManager-wait-online.service                               loaded    active   exited    Network Manager Wait Online
  NetworkManager.service                                           loaded    active   running   Network Manager
● nscd.service                                                     not-found inactive dead      nscd.service
● ntpd.service                                                     not-found inactive dead      ntpd.service
● ntpdate.service                                                  not-found inactive dead      ntpdate.service
  plymouth-quit-wait.service                                       loaded    inactive dead      Wait for Plymouth Boot Screen to Quit
  plymouth-quit.service                                            loaded    inactive dead      Terminate Plymouth Boot Screen
  plymouth-read-write.service                                      loaded    inactive dead      Tell Plymouth To Write Out Runtime Data
  plymouth-start.service                                           loaded    inactive dead      Show Plymouth Boot Screen
  polkit.service                                                   loaded    active   running   Authorization Manager
  postfix.service                                                  loaded    active   running   Postfix Mail Transport Agent
● rc-local.service                                                 loaded    failed   failed    /etc/rc.d/rc.local Compatibility

--state选项:指定状态

[root@lgs ~]# systemctl list-units --all --state=inactive
  UNIT                                                  LOAD      ACTIVE   SUB  DESCRIPTION
  proc-sys-fs-binfmt_misc.mount                         loaded    inactive dead Arbitrary Executable File Formats File System
  sys-fs-fuse-connections.mount                         loaded    inactive dead FUSE Control File System
  tmp.mount                                             loaded    inactive dead Temporary Directory
  systemd-ask-password-console.path                     loaded    inactive dead Dispatch Password Requests to Console Directory Watch
  accounts-daemon.service                               loaded    inactive dead Accounts Service
  brandbot.service                                      loaded    inactive dead Flexible Branding Service
  cpupower.service                                      loaded    inactive dead Configure CPU power related settings
  dm-event.service                                      loaded    inactive dead Device-mapper event daemon
  dracut-shutdown.service                               loaded    inactive dead Restore /run/initramfs
  emergency.service                                     loaded    inactive dead Emergency Shell
● exim.service                                          not-found inactive dead exim.service
  gdm.service                                           loaded    inactive dead GNOME Display Manager
  ip6tables.service                                     loaded    inactive dead IPv6 firewall with ip6tables
● ldap.service                                          not-found inactive dead ldap.service
● lvm2-activation.service                               not-found inactive dead lvm2-activation.service
  lvm2-lvmpolld.service                                 loaded    inactive dead LVM2 poll daemon
  microcode.service                                     loaded    inactive dead Load CPU microcode update

--type选项:指定类型

[root@lgs ~]# systemctl list-units --all --type=service
  UNIT                                                  LOAD      ACTIVE   SUB     DESCRIPTION
  accounts-daemon.service                               loaded    inactive dead    Accounts Service
  auditd.service                                        loaded    active   running Security Auditing Service
  brandbot.service                                      loaded    inactive dead    Flexible Branding Service
  chronyd.service                                       loaded    active   running NTP client/server
  cpupower.service                                      loaded    inactive dead    Configure CPU power related settings
  crond.service                                         loaded    active   running Command Scheduler
  dbus.service                                          loaded    active   running D-Bus System Message Bus
  dm-event.service                                      loaded    inactive dead    Device-mapper event daemon
  dracut-shutdown.service                               loaded    inactive dead    Restore /run/initramfs
  emergency.service                                     loaded    inactive dead    Emergency Shell
● exim.service                                          not-found inactive dead    exim.service
  gdm.service                                           loaded    inactive dead    GNOME Display Manager
  getty@tty1.service                                    loaded    active   running Getty on tty1

查看指定服务是否激活:

[root@lgs ~]# systemctl is-active crond
active

10.27 target介绍

系统为了方便管理,用target来管理units

列出系统的target:

[root@lgs ~]# systemctl list-unit-files --type=target
UNIT FILE                 STATE   
basic.target              static  
bluetooth.target          static  
cryptsetup-pre.target     static  
cryptsetup.target         static  
ctrl-alt-del.target       disabled
default.target            enabled 
emergency.target          static  
final.target              static  
getty.target              static  
graphical.target          static  
halt.target               disabled
hibernate.target          static  
hybrid-sleep.target       static  
initrd-fs.target          static  
initrd-root-fs.target     static  
initrd-switch-root.target static  
initrd.target             static  
iprutils.target           disabled
kexec.target              disabled
local-fs-pre.target       static  
local-fs.target           static  
machines.target           disabled
multi-user.target         enabled 
network-online.target     static  
network-pre.target        static  
network.target            static  
nss-lookup.target         static  
nss-user-lookup.target    static  
paths.target              static  
poweroff.target           disabled
printer.target            static  
reboot.target             disabled
remote-fs-pre.target      static  
remote-fs.target          enabled 
rescue.target             disabled
rpcbind.target            static  
runlevel0.target          disabled
runlevel1.target          disabled
runlevel2.target          enabled 
runlevel3.target          enabled 
runlevel4.target          enabled 
runlevel5.target          static  
runlevel6.target          disabled
shutdown.target           static  
sigpwr.target             static  
sleep.target              static  
slices.target             static  
smartcard.target          static  
sockets.target            static  
sound.target              static  
suspend.target            static  
swap.target               static  
sysinit.target            static  
system-update.target      static  
time-sync.target          static  
timers.target             static  
umount.target             static  

57 unit files listed.
lines 15-60/60 (END)

查看指定target下有哪些units:

[root@lgs ~]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
● ├─dbus.service
● ├─irqbalance.service
● ├─kdump.service
● ├─mysqld.service
● ├─network.service
● ├─NetworkManager.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
● ├─postfix.service
● ├─rc-local.service
● ├─rsyslog.service
● ├─sshd.service
● ├─sysstat.service
● ├─systemd-ask-password-wall.path
● ├─systemd-logind.service
● ├─systemd-readahead-collect.service
● ├─systemd-readahead-replay.service
● ├─systemd-update-utmp-runlevel.service
● ├─systemd-user-sessions.service
● ├─tuned.service
● ├─vmtoolsd.service
● ├─basic.target
● │ ├─iptables.service
● │ ├─microcode.service
● │ ├─rhel-autorelabel-mark.service
● │ ├─rhel-autorelabel.service
● │ ├─rhel-configure.service
● │ ├─rhel-dmesg.service
● │ ├─rhel-loadmodules.service
● │ ├─selinux-policy-migrate-local-changes@targeted.service
● │ ├─paths.target
● │ ├─slices.target
● │ │ ├─-.slice
● │ │ └─system.slice
● │ ├─sockets.target
● │ │ ├─dbus.socket
● │ │ ├─dm-event.socket
● │ │ ├─systemd-initctl.socket
● │ │ ├─systemd-journald.socket
● │ │ ├─systemd-shutdownd.socket
● │ │ ├─systemd-udevd-control.socket
● │ │ └─systemd-udevd-kernel.socket
● │ ├─sysinit.target
● │ │ ├─dev-hugepages.mount
● │ │ ├─dev-mqueue.mount
● │ │ ├─kmod-static-nodes.service
● │ │ ├─lvm2-lvmetad.socket
● │ │ ├─lvm2-lvmpolld.socket
● │ │ ├─lvm2-monitor.service
● │ │ ├─plymouth-read-write.service
● │ │ ├─plymouth-start.service
● │ │ ├─proc-sys-fs-binfmt_misc.automount
● │ │ ├─sys-fs-fuse-connections.mount
● │ │ ├─sys-kernel-config.mount
● │ │ ├─sys-kernel-debug.mount
● │ │ ├─systemd-ask-password-console.path
● │ │ ├─systemd-binfmt.service
● │ │ ├─systemd-firstboot.service
● │ │ ├─systemd-hwdb-update.service
● │ │ ├─systemd-journal-catalog-update.service
● │ │ ├─systemd-journal-flush.service
● │ │ ├─systemd-journald.service
● │ │ ├─systemd-machine-id-commit.service
● │ │ ├─systemd-modules-load.service
● │ │ ├─systemd-random-seed.service
● │ │ ├─systemd-sysctl.service
● │ │ ├─systemd-tmpfiles-setup-dev.service
● │ │ ├─systemd-tmpfiles-setup.service
● │ │ ├─systemd-udev-trigger.service
● │ │ ├─systemd-udevd.service
● │ │ ├─systemd-update-done.service
● │ │ ├─systemd-update-utmp.service
● │ │ ├─systemd-vconsole-setup.service
● │ │ ├─cryptsetup.target
● │ │ ├─local-fs.target
● │ │ │ ├─-.mount
● │ │ │ ├─boot.mount
● │ │ │ ├─rhel-import-state.service
● │ │ │ ├─rhel-readonly.service
● │ │ │ └─systemd-remount-fs.service
● │ │ └─swap.target
● │ │   └─dev-disk-by\x2duuid-deb5a799\x2d817e\x2d41a2\x2d8ab5\x2d25893d3247b2.swap
● │ └─timers.target
● │   └─systemd-tmpfiles-clean.timer
● ├─getty.target
● │ └─getty@tty1.service
● └─remote-fs.target
lines 47-92/92 (END)

target下可能含有其他target和service。

查看系统默认的target:

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

设置系统默认的target:设置默认target,会创建一个软链接。

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

[root@lgs ~]# ls -l  /etc/systemd/system/default.target
lrwxrwxrwx 1 root root 41 5月  13 13:41 /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target

查看一个service是属于哪个target的:[Install]部分的WantedBy

[root@lgs ~]# cat /usr/lib/systemd/system/sshd.service 
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

multi-user.target下的service才能设置开机启动。

转载于:https://my.oschina.net/u/3804114/blog/1811565

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值