八周一次课

八周一次课
10.23 linux任务计划cron
10.24 chkconfig工具
10.25 systemd管理服务
10.26 unit介绍
10.27 target介绍
mysql

10.23 linux任务计划cronlinux

  • crontab -u、-e、-l、-r
  • 格式:分 时 日 月 周 user command
  • 文件/var/spool/cron/username
  • 分范围0-59,时范围0-23,日范围1-31,月范围1-12,周1-7
  • 可用格式1-5表示一个范围1到5
  • 可用格式1,2,3表示1或者2或者3
  • 可用格式*/2表示被2整除的数字,好比小时,那就是每隔2小时
  • 要保证服务是启动状态
  • systemctl start crond.service

crontab命令

  • crontab -e //编辑
  • crontab -l //列出 -crontab -r //删除
  • crontab -u username -l //指定用户

任务计划

  • crontab -e //编辑
  • 在linux系统中,系统计划是必不可少的,好比备份数据,重启服务等
    • 操做过程,多是一个脚本,有多是一个单独的命令,在特定的时间去执行它,因此任务计划是不可缺乏的
  • 在windows中都是使用的我的电脑,因此任务计划不多见,几乎用不到

linux中计划的配置文件

  • cat /etc/crontab //查看任务计划的配置文件
    • 文件中会定义几个变量
      • SHELL=/bin/bash
      • PATH环境变量,它命令的路径
      • MAILTO发邮件给谁

[root@tianqi-01 ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=rootnginx

# For details see man 4 crontabssql

# 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    //星期,0或7都                                                                                                            表示周日,也能够写成英文的简写
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed    //用户,不写用户就是root 最后一列,是你要执                                                                                                                                                     行的命令shell

[root@tianqi-01 ~]# vim

crontab -e 进入到crontab的配置文件中,用法和vim同样windows

  • 按 i 进入编辑模式
    • 分钟,小时,日,月,星期,而后后面跟具体的命令    #分、时、日、月、周
  • 凌晨3点去执行,* 表示全部的意思

天天凌晨三点,执行123.sh脚本文件,正确的和错误的日志都输出到123.log文件中centos

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

由于是天天三点执行脚本,因此能够写成追加,天天都去记录日志session

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

若想1-10号,双月去执行该脚本,后面就不在执行了——>只要 被2 整除,就符合条件

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

只要周2和周5执行该文件

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

  • 为何没有年份?
  • 用星期肯定你的惟一性,好比说今年的6月18号和明年的6月18号的星期确定是不一样的,这样就能够肯定某一天的惟一性。

启动crond服务

  • 若想要这个任务正常使用,还须要去启动crond服务
    • systemctl start crond.service //启动crond服务
  • 若想检查服务是否成功启动,
    • 方法一:可以使用ps aux |grep cron 命令查看
      • 如有这个进程,说明这个服务已经启动了
    • 方法二:使用systemctl start crond 查看状态
      • 如果 绿色 ,则表示该服务已经启动了
      • 如果该服务已经停掉了,则不会有颜色

[root@tianqi-01 ~]# systemctl start crond
[root@tianqi-01 ~]# ps aux | grep cron

root        503  0.0  0.1 126232  1600 ?        Ss   07:52   0:00 /usr/sbin/crond -n
root       2688  0.0  0.0 112660   980 pts/0    R+   10:11   0:00 grep --color=auto cron
[root@tianqi-01 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-01-27 07:52:29 CST; 2h 20min ago
 Main PID: 503 (crond)
   CGroup: /system.slice/crond.service
           └─503 /usr/sbin/crond -n

Jan 27 07:52:29 tianqi-01 systemd[1]: Started Command Scheduler.
Jan 27 07:52:29 tianqi-01 systemd[1]: Starting Command Scheduler...
Jan 27 07:52:30 tianqi-01 crond[503]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 42% if used.)
Jan 27 07:52:30 tianqi-01 crond[503]: (CRON) INFO (running with inotify support)
[root@tianqi-01 ~]# 

停掉crond服务,查看状态,没有颜色显示

[root@tianqi-01 ~]# systemctl stop crond
[root@tianqi-01 ~]# systemctl status crond

● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Sat 2018-01-27 10:15:03 CST; 16s ago
  Process: 503 ExecStart=/usr/sbin/crond -n $CRONDARGS (code=exited, status=0/SUCCESS)
 Main PID: 503 (code=exited, status=0/SUCCESS)

Jan 27 07:52:29 tianqi-01 systemd[1]: Started Command Scheduler.
Jan 27 07:52:29 tianqi-01 systemd[1]: Starting Command Scheduler...
Jan 27 07:52:30 tianqi-01 crond[503]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 42% if used.)
Jan 27 07:52:30 tianqi-01 crond[503]: (CRON) INFO (running with inotify support)
Jan 27 10:15:03 tianqi-01 systemd[1]: Stopping Command Scheduler...
Jan 27 10:15:03 tianqi-01 systemd[1]: Stopped Command Scheduler.
[root@tianqi-01 ~]# 

要想执行任务计划,必须开启crond服务

[root@tianqi-01 ~]# systemctl start crond

任务计划不执行的缘由分析

  • 在写了一个计划,放入到配置文件中,但就是不执行
  • 不执行的缘由颇有多是你写的脚本里面,没有使用绝对路径的缘由致使不执行
    • 由于颇有可能,你在使用的命令不在PATH里面,因此要么将命令写一个绝对路径,要么将命令的路径加入到PATH变量里面去
  • 建议:在写一个脚本的时候,都要写追加一个日志,这样能够保证这个任务有据可查,再不执行的时候,查看错误日志便可

任务计划备份

  • crontab -l //列出
  • crontab文件存在位置/var/spool/cron/username
  • 在须要备份的时候,直接把这个文件,或者目录拷贝下便可
  • crontab -r删除任务计划
  • crontab -u username -l //指定用户

[root@tianqi-01 ~]# crontab -e

1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 | xargs rm -f 

[root@tianqi-01 ~]# crontab -l
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 | xargs rm -f 
[root@tianqi-01 ~]# cat /var/spool/cron/root
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 | xargs rm -f 
[root@tianqi-01 ~]# crontab -r
[root@tianqi-01 ~]# crontab -l

no crontab for root
[root@tianqi-01 ~]# crontab -u root -l
no crontab for root
[root@tianqi-01 ~]# 
10.24 chkconfig工具

  • chkconfig --list
  • chkconfig --level 3 network off
  • chkconfig --level 345 network off
  • chkconfig --del network
  • chkconfig --add network

chkconfig工具

  • crond、iptables、firewalld、nginx、httpd、mysql等等,都属于服务。
  • chkconfig工具,在centos6和以前的版本中,控制服务的启动;但在centos7中不多使用了,但为了兼容以前的版本,依然可使用,但在将来的趋势中, 有可能就会被遗弃了,如今就是过分的做用。
  • chkconfig --list //列出全部的系统服务
    • 表示chkconfig工具在centos6或以前的版本中,使用的服务的管理的机制叫 SysV,而centos7的版本中,使用的是 systemd 服务

[root@tianqi-01 ~]# chkconfig --list        //列出全部的系统服务

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@tianqi-01 ~]# 

用top命令查看进程,发现systemd的pid是1,说明这个进程很是重要。

chkconfig命令

  • 服务的脚本存放在 /etc/init.d/ 下面
    • 启动脚本存放该目录下

[root@tianqi-01 ~]# ls /etc/init.d/
functions  netconsole  network  README
[root@tianqi-01 ~]# 

  • chkconfig --list //列出全部的服务
  • chkconfig network off //将network服务关闭

[root@tianqi-01 ~]# chkconfig network off
[root@tianqi-01 ~]# chkconfig --list       
 //会看到2,3,4级别关闭了

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:off    3:off    4:off    5:off    6:off

[root@tianqi-01 ~]# 
注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。

若是您想列出 systemd 服务,请执行 'systemctl list-unit-files'。

欲查看对特定 target 启用的服务请执行 'systemctl list-dependencies [target]'。

[root@tianqi-01 ~]# chkconfig network on        //会看到2,3,4级别又开启了
[root@tianqi-01 ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@tianqi-01 ~]# 

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

若是您想列出 systemd 服务,请执行 'systemctl list-unit-files'。

欲查看对特定 target 启用的服务请执行 'systemctl list-dependencies [target]'。

  • 在系统中有七个级别等级列表:

    • 等级0表示:表示关机
    • 等级1表示:单用户模式
    • 等级2表示:多用户模式,比3少nfs服务
    • 等级3表示:多用户模式,不带图形
    • 等级4表示:是一种保留的级别,暂时没用
    • 等级5表示:带图形界面的多用户模式,若是安装了图形,能够启动5级别
    • 等级6表示:从新启动
  • 在centos6中的 /etc/inittab 中定义开机的级别

  • 在centos7中,已经没有用了,不须要定义开机的级别了

[root@tianqi-01 ~]# vim /etc/inittab

# inittab is no longer used when using systemd.      //已经再也不使用inittab级别了
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
#

chkconfig命令,指定某一级别开启/关闭

  • chkconfig --level 3 network off //指定network中的3级别关闭

[root@tianqi-01 ~]# chkconfig --level 3 network off    //指定network中的3级别关闭
[root@tianqi-01 ~]# chkconfig --list                            //列出全部服务

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:off    4:on    5:on    6:off
[root@tianqi-01 ~]# 

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

若是您想列出 systemd 服务,请执行 'systemctl list-unit-files'。

欲查看对特定 target 启用的服务请执行 'systemctl list-dependencies [target]'。

  • chkconfig --level 345 network on //指定network中的3,4,5级别开启

[root@tianqi-01 ~]# chkconfig --level 35 network off
[root@tianqi-01 ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:off    4:on    5:off    6:off
[root@tianqi-01 ~]# chkconfig --level 345 network on
[root@tianqi-01 ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@tianqi-01 ~]# 

  • 0和1和6级别不能设置成开
    • 0级别在关机状态是不可能开启的
    • 1级别是单用户模式,服务是不可能开启的
    • 6级别在重启的时候,是不可能开启的——>重启至关于先关闭在启动(重启的那一刻是先关闭才对)。

将一个脚本加入到服务列表中

  1. 首先将启动脚本放入到 /etc/init.d 这个目录下——>只有在这个目录下,才能够添加到服务列表中去
  2. 文件名称无所谓,但内容有格式要求
    • 首先是是一个shell脚本
    • 而后chkconfig指定运行级别启动顺序,第10位启动,第90位关闭
    • 下面代码为它的固定格式,必需要有的!!!

[root@tianqi-01 ~]# cd /etc/init.d/
[root@tianqi-01 init.d]# ls

functions  netconsole  network  README
[root@tianqi-01 init.d]# cp network 123
[root@tianqi-01 init.d]# ll

total 48
-rwxr-xr-x  1 root root  7293 Jan 27 10:53 123
-rw-r--r--. 1 root root 17500 May  3  2017 functions
-rwxr-xr-x. 1 root root  4334 May  3  2017 netconsole
-rwxr-xr-x. 1 root root  7293 May  3  2017 network
-rw-r--r--. 1 root root  1160 Aug  5 14:38 README
[root@tianqi-01 init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off

[root@tianqi-01 init.d]# chkconfig --add 123        //将123加入到服务列表中
[root@tianqi-01 init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

123                0:off    1:off    2:on    3:on    4:on    5:on    6:off
netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@tianqi-01 init.d]# 

[root@tianqi-01 init.d]# ls
123  functions  netconsole  network  README
[root@tianqi-01 init.d]# vim 123        //名字无所谓,可是有固定的格式

#! /bin/bash                                    //首先是一个shell脚本
#
# network       Bring up/down networking
#
# chkconfig: 2345 10 90        //指定运行级别的启动顺序,第10位启动,第90位关闭,这个格式必需要有
# description: Activates/Deactivates all network interfaces configured to \    //描述必需要有
#              start at boot time.
#
### BEGIN INIT INFO
# Provides: $network
# Should-Start: iptables ip6tables NetworkManager-wait-online NetworkManager $network-pre
# Short-Description: Bring up/down networking
# Description: Bring up/down networking
### END INIT INFO

# Source function library.
. /etc/init.d/functions

if [ ! -f /etc/sysconfig/network ]; then
    exit 6
fi

. /etc/sysconfig/network

if [ -f /etc/sysconfig/pcmcia ]; then
    . /etc/sysconfig/pcmcia
fi


# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 6

# if the ip configuration utility isn't around we can't function.
[ -x /sbin/ip ] || exit 1


CWD=$(pwd)
cd /etc/sysconfig/network-scripts

注意:只有运行级别和描述有了,才能被识别。

  • chkconfig --del network //删除服务列表中的脚本
  • chkconfig --add network //增长服务列表中的脚本

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

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@tianqi-01 init.d]# 
10.25 systemd管理服务

  • systemctl list-units --all --type=service
  • 几个经常使用的服务相关的命令
  • systemctl enable crond.service //让服务开机启动
  • systemctl disable crond //不让开机启动
  • systemctl status crond //查看状态
  • systemctl stop crond //中止服务
  • systemctl start crond //启动服务
  • systemctl restart crond //重启服务
  • systemctl is-enabled crond //检查服务是否开机启动

systemd工具

  • systemd是centos7管理的一个服务机制,在centos6或以前的版本中可使用chkconfig工具去管理系统的服务,在centos7中,也可使用,但会提示使用 systemctl list-unit-files ,用它来查看全部的服务。
  • systemctl list-unit-files //查看全部的服务

[root@tianqi-01 init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@tianqi-01 init.d]# systemctl list-unit-files    //查看全部的服务,里面不只有service,还有                                                                                                                                      socket,还有target
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相关的命令

  • systemctl list-units --all --type=service //列出全部的service
    • 会列出全部的service
    • 列出描述信息,是不是loaded,是不是active
    • 按 空格 往下翻
    • 如果不加 --all ,则就不会列出 未激活的active

[root@tianqi-01 init.d]# systemctl list-units --all --type=service
  UNIT                                                  LOAD      ACTIVE   SUB     DESCRIPTION
  atd.service                                           loaded    active   running Job spooling tools
  auditd.service                                        loaded    active   running Security Auditing Service
  brandbot.service                                      loaded    inactive dead    Flexible Branding Service
  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
● display-manager.service                               not-found inactive dead    display-manager.service
  dracut-shutdown.service                               loaded    inactive dead    Restore /run/initramfs
  ebtables.service                                      loaded    inactive dead    Ethernet Bridge Filtering tables
  emergency.service                                     loaded    inactive dead    Emergency Shell

等等,只列出了一部分

并在最下面,会告诉你 LOAD,ACTIVE,SUB是什么意思

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.

还会提醒,若想列出全部的 unit files,请使用 systemctl list-unit-files 命令

  • systemctl enable crond.service //让服务开机启动——>service可省略
  • systemctl disable crond //不让开机启动

[root@tianqi-01 init.d]# systemctl enable crond.service    //让服务开机启动
[root@tianqi-01 init.d]# systemctl disable crond        //不让开机启动
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@tianqi-01 init.d]# systemctl enable crond
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@tianqi-01 init.d]# 

  • systemctl status crond //查看状态

[root@tianqi-01 init.d]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-01-27 10:16:25 CST; 1h 1min ago
 Main PID: 2706 (crond)
   CGroup: /system.slice/crond.service
           └─2706 /usr/sbin/crond -n

Jan 27 10:16:25 tianqi-01 systemd[1]: Started Command Scheduler.
Jan 27 10:16:25 tianqi-01 systemd[1]: Starting Command Scheduler...
Jan 27 10:16:25 tianqi-01 crond[2706]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 97% if used.)
Jan 27 10:16:25 tianqi-01 crond[2706]: (CRON) INFO (running with inotify support)
Jan 27 10:16:25 tianqi-01 crond[2706]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
Jan 27 10:24:01 tianqi-01 crond[2706]: (root) RELOAD (/var/spool/cron/root)
[root@tianqi-01 init.d]# 

  • systemctl stop crond //中止服务
  • systemctl start crond //启动服务
  • systemctl restart crond //重启服务
  • systemctl is-enabled crond //检查服务是否开机启动

[root@tianqi-01 init.d]# systemctl is-enabled crond
enabled
[root@tianqi-01 init.d]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@tianqi-01 init.d]# systemctl is-enabled crond
disabled
[root@tianqi-01 init.d]# systemctl enable crond
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@tianqi-01 init.d]# 

  • 而且能够经过输出信息,在 /etc/systemd/system/multi-user.target.wants/crond.service 得到service的配置文件内容

[root@tianqi-01 init.d]# cat /etc/systemd/system/multi-user.target.wants/crond.service
[Unit]
Description=Command Scheduler
After=auditd.service systemd-user-sessions.service time-sync.target

[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process

[Install]
WantedBy=multi-user.target

[root@tianqi-01 init.d]# 

[root@tianqi-01 init.d]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service
lrwxrwxrwx 1 root root 37 Jan 27 11:20 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service

//是一个软链接,从软连接的右边到左边
[root@tianqi-01 init.d]# ll /usr/lib/systemd/system/crond.service    //这里才是文件真正的路径
-rw-r--r--. 1 root root 284 Aug  3 23:33 /usr/lib/systemd/system/crond.service
[root@tianqi-01 init.d]# 

[root@tianqi-01 init.d]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@tianqi-01 init.d]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service
ls: cannot access /etc/systemd/system/multi-user.target.wants/crond.service: No such file or directory

//这里会把软链接挪走
[root@tianqi-01 init.d]# systemctl enable crond
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@tianqi-01 init.d]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service
lrwxrwxrwx 1 root root 37 Jan 27 11:25 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service
[root@tianqi-01 init.d]# 

//此时会从新建立软连接

10.26 unit介绍

  • ls /usr/lib/systemd/system //系统全部unit,分为如下类型:
    • service 系统服务
    • target 多个unit组成的组
    • device 硬件设备
    • mount 文件系统挂载点
    • automount 自动挂载点
    • path 文件或路径
    • scope 不是由systemd启动的外部进程
    • slice 进程组
    • snapshot systemd快照
    • socket 进程间通讯套接字
    • swap swap文件
    • timer 定时器

unit相关的命令

  • systemctl list-units //列出正在运行的unit
    • 并会提示,若要列出全部的units,则须要加 --all
  • systemctl list-units --all //列出全部,包括失败的或者inactive的
  • systemctl list-units --all --state=inactive //列出inactive的unit
  • systemctl list-units --type=service //列出状态为active的service
    • 其中failed是一个特例,也会列出来
  • systemctl is-active crond.service //查看某个服务是否为active

[root@tianqi-01 init.d]# ls /usr/lib/systemd/system
arp-ethers.service                      messagebus.service                             sys-fs-fuse-connections.mount
atd.service                             microcode.service                              sysinit.target
auditd.service                          multi-user.target                              sysinit.target.wants
autovt@.service                         multi-user.target.wants                        sys-kernel-config.mount
basic.target                            NetworkManager-dispatcher.service              sys-kernel-debug.mount
basic.target.wants                      NetworkManager.service                         syslog.socket
blk-availability.service                NetworkManager-wait-online.service             syslog.target.wants
bluetooth.target                        network-online.target                          systemd-ask-password-console.path
等等,还有不少
[root@tianqi-01 init.d]#

[root@tianqi-01 system]# systemctl is-active crond.service        //查看某个服务是否为active
active
[root@tianqi-01 system]# systemctl is-enabled crond.service
enabled
10.27 target介绍

  • 系统为了方便管理target来管理unit
  • systemctl list-unit-files --type=target //列出系统中全部的target
  • systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit
  • systemctl get-default //查看系统默认的target
  • systemctl set-default multi-user.target
  • 一个service属于一种类型的unit
  • 多个unit组成了一个target
  • 一个target里面包含了多个service
  • cat /usr/lib/systemd/system/sshd.service 看[install]部分

target相关命令

  • systemctl list-unit-files --type=target //列出系统中全部的target

[root@tianqi-01 system]# 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  

等等,仅截取了一部分

  • systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit
  • systemctl get-default //查看系统默认的target
  • systemctl set-default multi-user.target //设置默认的target

[root@tianqi-01 system]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─atd.service
● ├─auditd.service
● ├─brandbot.path
● ├─crond.service
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service
● ├─NetworkManager.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
● ├─postfix.service
● ├─rsyslog.service
● ├─sshd.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
● │ ├─microcode.service

等等,仅列出一部分

[root@tianqi-01 system]# systemctl get-default
multi-user.target
[root@tianqi-01 system]# 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@tianqi-01 system]# ls /etc/systemd/system/default.target
/etc/systemd/system/default.target
[root@tianqi-01 system]# ll !$
ll /etc/systemd/system/default.target
lrwxrwxrwx 1 root root 41 Jan 27 11:51 /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target
[root@tianqi-01 system]# 

  • 一个service属于一种类型的unit
  • 多个unit组成了一个target
  • 一个target里面包含了多个service
  • cat /usr/lib/systemd/system/sshd.service 看[install]部分

[root@tianqi-01 system]# 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
[root@tianqi-01 system]# 

  • 只有multi-user.target 里面的service能够设置开机启动。其余的target设置成默认启动没法正常启动。

[root@tianqi-01 system]# cat /usr/lib/systemd/system/crond.service
[Unit]
Description=Command Scheduler
After=auditd.service systemd-user-sessions.service time-sync.target

[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process

[Install]
WantedBy=multi-user.target

[root@tianqi-01 system]# 

友情连接:阿铭Linux

相关文章
相关标签/搜索