10.23 linux任务计划cron
10.24 chkconfig工具
10.25 systemd管理服务
10.26 unit介绍
10.27 target介绍
10.23 linux任务计划cron:
在linux中任务计划是必不可少的,因为可能我们凌晨的时候去做一些事情。可能是备份数据或是重启服务啊,这个操作的过程可能是一个脚本,也有可能是一个单独的命令。不管是什么,总得有一个时间要去执行他所以任务计划必不可少。
~. cat /etc/crontab 这个文件下是任务计划的配置文件,他会定义shell 环境变量、发送邮件给谁
[root@afeilinux-01 ~]# 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
SHELL=/bin/bash shell
PATH=/sbin:/bin:/usr/sbin:/usr/bin 变量,命令的路径
MAILTO=root 发送邮件给谁
# For details see man 4 crontabs
# Example of job definition: (下面是他的格式)
# .---------------- minute (0 - 59) 第一位为表示分钟,范围是0-59
# | .------------- hour (0 - 23) 小时,0-23
# | | .---------- day of month (1 - 31) 日期 1-31
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ... 月份1-12,可以写数字也可以写英文的简写
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat 星期,0-6。周日可以写成0或7
# | | | | |
# * * * * * user-name command to be executed
这5个星表示它的 位
user-name 在root用户下定义任务就是root。不写的话就是root
command to be executed 表示要执行的命令,怎么定义呢,写法为:
crontab -e,这样就进入了crontab的配置文件当中
我们写好的他的任务,怎么确定他的唯一性呢。用周来确定。因为今年的周一跟明年的周一是不一样的
~1. crontab -u 、 -e 、 -l 、 -r
综上所述,
-e 进入 crontab的配置文件当中。用法跟vim一样的。按 a 进入编辑模式
-l 可以查看写的任务计划,列出
-r 删除任务
-u 指定用户 例如,crontab -u root -r
~3. 文件 /var/spool/cron/username
crontab的文件在这个路径里面。是用户对应的文件,比如root的就会写上root的,其他的就会写上其他的用户名字,以用户的名字命名的一个文件
~2. 格式:分 时 日 月 周
(user command 进入编辑模式之后,以分时日月周的模式来写,以空格间隔。后面跟命令)
例如 0 3 * * * /binsbin /usr/local/sbin/123.sh (比如写了一个脚本,就这样写任务计划。每天3点执行这个脚本)
0 3 * * * /binsbin /usr/local/sbin/123.sh > /tmp/123.log 2> /tmp/123.log(每天3三点将这个脚本正确的和错误的都输出到这个文件里来。也可以是追加>>
*表示相对应的第几位,表示所有,每天都执行、每月都执行、每周都执行
~4. 分范围0-59,时范围0-23,日范围1-31,月范围1-12,周范围0-6
~5. 可用格式1-5表示一个范围 1到5
0 3 1-5 * * /binsbin /usr/local/sbin/123.sh
1-5号的每天3点执行这个脚本
~6. 可用格式1,2,3表示1或者2或者3
0 0 0 */2 2,5 /binsbin /usr/local/sbin/123.sh
每两个月的周二和周五来执行这个脚本
以逗号分隔,周二和周五
~7. 可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时。只要是被2整除的
0 3 1-5 */2 * /binsbin /usr/local/sbin/123.sh
每两个月,1-5号的3点执行这个脚本
~8. 要保证服务是启动状态
systemctl start crond.service,输入这个才会让他启动这个服务
启动之后可以用 ps aux | grep crond 查看一下,如果有这个服务就代表他已经启动了
root 607 0.0 0.1 126284 1148 ? Ss 9月03 0:00 /usr/sbin/crond -n
root 43791 0.0 0.0 112728 984 pts/0 R+ 14:18 0:00 grep --color=auto crond
也可以用 systemctl status crond 来查看crond的状态。有active (running)并显示为绿色表示已经启动。相反的我们systemctl stop crond 停掉crond,就会有显示inactive (dead)并没有颜色
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2019-09-03 13:56:14 CST; 2 days ago
Main PID: 607 (crond)
CGroup: /system.slice/crond.service
└─607 /usr/sbin/crond -n
9月 03 13:56:14 afeilinux-01 systemd[1]: Started Command Scheduler.
9月 03 13:56:15 afeilinux-01 crond[607]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 42% if used.)
9月 03 13:56:16 afeilinux-01 crond[607]: (CRON) INFO (running with inotify support)
需要注意的是,我们在写完任务计划的时候。必须要启动这个任务计划。 systemctl start crond。
我们在写任务的时候,有用到命令,要写命令的绝对路径才可以生效
任务计划里写上正确和错误的输出文件,以方便我们有据可查
----------------------------------------------------------------------------------------------------------------------------------------------------
10.24 chkconfig工具:
linux系统的服务管理,像我们之前接触到的 crond、firewalld等等都是服务,那我们可以用chkconfig来管理这些服务。怎么样控制这个服务启动,怎么控制这个服务开机启动,怎么让这个服务在指定的级别去启动
chkconfig是centos6之前的版本用的
~1. 我们先来执行 chkconfig --list 列出chkconfig管理的服务
[root@afeilinux-01 ~]# 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:关
php-fpm 0:关 1:关 2:开 3:开 4:开 5:开 6:关
注:该输出结果只显示 SysV 服务,并不包含 6以前的版本服务机制是sysv,所以只显示sysv服务
原生 systemd 服务。SysV 配置数据 7以后得版本为systemd服务机制,所以不会显示
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。所以想要看systemd服务的要执行该命令
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-fs-nfsd.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
var-lib-nfs-rpc_pipefs.mount static
brandbot.path disabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-1.scope static
session-2.scope static
arp-ethers.service disabled
auditd.service enabled
auth-rpcgss-module.service static
autovt@.service enabled
blk-availability.service disabled
查看在具体 target 启用的服务请执行 target是systemd服务的另一个概念
'systemctl list-dependencies [target]'。
\x5btarget\x5d.service
sysv服务机制的只剩下这两个了
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关 。0-6状态各表示6以前版本的运行级别
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
0 关机状态 1单用户 2多用户模式(少了nfs服务) 3多用户模式(不带图形)
4保留级别(暂时没用) 5 多用户带图形 6重启
~2. 这些服务都是为一个服务的脚本,目录在
/etc/init.d/ 这个里面
[root@afeilinux-01 ~]# ls /etc/init.d/
functions mysqld netconsole network nginx php-fpm README
后期我们学其他服务的时候,还会用到这个路径。把一些启动文件放进来,用chkconfig来管理
~3. chkconfg --level 3 network off 指定network服务的3级别关闭
多个级别同时关闭或打开,不用间隔直接写就好。例如:345
0和6级别不会开启服务,因为分别处于关机和重启。1级别也不会开,处于单用户模式
~4. chkconfig --add 123 把123服务加入到这个服务列表里面来
[root@afeilinux-01 ~]# cd /etc/init.d 前提是要在这个目录里的服务,才可以进入到列表里来
[root@afeilinux-01 init.d]# ls
functions netconsole network README
[root@afeilinux-01 init.d]# cp network 123 为保险我们复制一个123,为什么要复制因为文件是有格式的,才能被识别
[root@afeilinux-01 init.d]# chkconfig --add 123 把123加入到列表里
[root@afeilinux-01 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:关 123服务
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
~5. chkconfig --del 123 把123服务在列表里删除
----------------------------------------------------------------------------------------------------------------------------------------------------
10.25 systemd管理服务:
systemd是centos7系统的服务管理机制
~1. systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-fs-nfsd.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
var-lib-nfs-rpc_pipefs.mount static
brandbot.path disabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-1.scope static
session-2.scope static
arp-ethers.service disabled
我们在chkconfig --list的时候,不会列出systemd的服务。但是会提示我们
“要列出 systemd 服务,请执行 'systemctl list-unit-files'”
我们执行这个systemctl list-unit-files,(按空格往下翻)。会发现他不仅显示servicehaiyou target等等服务,会比较乱。
systemctl list-units --all --type=service(只用这个查看systend服务,只查看service的)
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
auth-rpcgss-module.service loaded inactive dead Kernel Module supporting RPCSEC_GSS
blk-availability.service loaded active exited Availability of block devices
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
● display-manager.service not-found inactive dead display-manager.service
dracut-shutdown.service loaded inactive dead Restore /run/initramfs
emergency.service loaded inactive dead Emergency Shell
● exim.service not-found inactive dead exim.service
● fcoe.service not-found inactive dead fcoe.service
getty@tty1.service loaded active running Getty on tty1
gssproxy.service loaded active running GSSAPI Proxy Daemon
httpd.service loaded active running The Apache HTTP Server
● ip6tables.service not-found inactive dead ip6tables.service
● iptables.service not-found inactive dead iptables.service
irqbalance.service loaded inactive dead irqbalance daemon
● iscsi-shutdown.service not-found inactive dead iscsi-shutdown.service
● iscsi.service not-found inactive dead iscsi.service
● iscsid.service not-found inactive dead iscsid.service
● kdump.service loaded failed failed Crash recovery kernel arming
因为我们看的仅仅只是service服务,所以我们可以用 systemctl list-units --all --type=service。这样列出的服务就只有service
~2.几个常用的服务的命令(重点,常用!!!!)
systemctl enable crond.service 让crond服务开机启动(后缀.service可省略)
systemctl disable crond.service 不让crond服务开启启动(后缀.service可省略)
systemctl status crond 查看crond状态
含有active (running)且为绿色,表示已启动
systemctl stop crond 停止crond服务
systemctl start crond 启动crond服务
systemctl restart crond 重启crond这个服务
systemctl is-enabled crond 检查crond这个服务是否开机启动
显示enabled即为开机启动 显示disabled即为非开机启动
----------------------------------------------------------------------------------------------------------------------------------------------------
10.26 unit介绍:
(systemd管理服务)
什么叫unit?
通过上一节,我们学到了systemctl,简单知道了 enabled disabled。如果这个服务为 enabled,他就会创建一个软连接,这个软连接的目录为
ls /usr/lib/systemd/system
[root@afeilinux-01 ~]# ls /usr/lib/systemd/system
arp-ethers.service machine.slice sysinit.target
auditd.service machines.target sysinit.target.wants
autovt@.service messagebus.service sys-kernel-config.mount
basic.target microcode.service sys-kernel-debug.mount
basic.target.wants multi-user.target syslog.socket
blk-availability.service multi-user.target.wants syslog.target.wants
会发现他有很多的类型,像target、mount、wants(目录)。而这些类型的文件就叫做unit
系统所有unit分为以下类型:
~ service 系统服务
~ target 多个组成的组
之间讲到6以前的版本,有7个运行级别。而7也有类似的,就是target文件
[root@afeilinux-01 ~]# cd !$
cd /usr/lib/systemd/system 同样在这个路径里
[root@afeilinux-01 system]# ls -l runlevle* 查看runlevel文件
ls: 无法访问runlevle*: 没有那个文件或目录
[root@afeilinux-01 system]# ls -l runlevel*
同样也有7个运行级别的target,通过也是软连接。他的源就是后面的文件。跟6的7个运行级别,基本对应起来了
lrwxrwxrwx. 1 root root 15 5月 29 07:11 runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 5月 29 07:11 runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 5月 29 07:11 runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 5月 29 07:11 runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 5月 29 07:11 runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 5月 29 07:11 runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 5月 29 07:11 runlevel6.target -> reboot.target
~ device 硬件设备
~ mount 文件系统的挂载点
~ automount 自动挂载点
~ path 文件或路径
~ scope 不是由systend启动的外部进程 比较陌生,熟悉就好
~ slice 进程组
~ snapshot systemd快照
~ socket 进程间通信套接字
~ swap swap文件
~ timer 定时器
unit相关的命令:
~ systemctl list-units 列出列出正在运行的unit
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-host2-target2:0:0-2:0:0:0-block-sr0.device loaded active plugged VMware_Virtual_IDE_CDROM_Drive
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active plugged VMware_Virtual_S 1
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded active plugged VMware_Virtual_S 2
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda3.device loaded active plugged VMware_Virtual_S 3
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda.device loaded active plugged VMware_Virtual_S
~ systemctl list-units --all 列出所有,包括失败的或者inactive的
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point
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\x2dpath-pci\x2d0000:00:07.1\x2data\x2d2.0.device loaded active plugged VMware_Virtual_IDE_CDROM_Drive
~ systemctl list-units --all --state=inactive 列出inactive(闲置的)的unit
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
systemd-ask-password-console.path loaded inactive dead Dispatch Password Requests to Console Directory Watch
auth-rpcgss-module.service loaded inactive dead Kernel Module supporting RPCSEC_GSS
cpupower.service loaded inactive dead Configure CPU power related settings
~ systemctl lit-units --type=service 列出状态为active(活跃的)的service
~ systemctl is-active crond.service 查看某个服务是否为active
active
----------------------------------------------------------------------------------------------------------------------------------------------------
10.27 target介绍:
(systemd管理服务)
一个target是由多个unit组合起来的,那么他们是什么联系起来的:
系统为了方便管理,用target来管理unit
~ systemctl list-unit-files --type=target 这个命令可以列出系统里面所有的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-pre.target static
getty.target static
graphical.target static
halt.target disabled
~ systemctl list-dependencies multi-user.target 指定查看target(multi-user.target)下面有哪些unit
multi-user.target
● ├─auditd.service
● ├─chronyd.service
● ├─dbus.service
● ├─httpd.service
● ├─irqbalance.service
● ├─kdump.service
● ├─mdmonitor.service
● ├─mysqld.service
● ├─network.service
● ├─NetworkManager.service
~ systemctl get-default 查看系统默认的target
就是multi-user.target
~ systemctl set-default multi-user.target 设定默认的target(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.
重新设定的时候就会创建一个新的软连接
! 一个service 属于其中一种类型的unit
多个unit组成了一个target
一个target里面包含了多个service
cat /usr/lib/systemd/system/sshd.service 看install部分 ,可看到他属于哪个target。例如:
[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
cat这个目录,查看sshd.service。看install部分就会知道 WantedBy=multi-user.target
总结:
系统可以说是由多种unit组成的,这么多的unit为了方便管理,那么我们把他们归类,归类成若干个组,我们把它叫做taeget。也就是说target是有多个unit的组成的。那service属于其中一种类型的unit,一个target里面包含了多个service