一、crond任务计划:用来管理系统和执行系统周期性的任务,相似于windows下的任务计划,当安装完操做系统后,系统会默认安装此服务,而且会自行启动crond进程,crond没分钟会按期检查是否有要执行的任务,若是有要执行的任务,则自动执行:shell
crontab options vim
options:windows
-u:查看指定用户的任务计划,不指定则为root用户:bash
-l:列出计划任务:可结合-u:并发
-r:表示删除计划任务:可结合-u:dom
-e:编辑任务计划,等同于打开了vim /var/spool/cron/user/某个用户的文件:ssh
1:制定计划任务:socket
crontab的配置文件: cat /etc/crontabide
[root@localhost ~]# cat /etc/crontab SHELL=/bin/bash #运行格式 PATH=/sbin:/bin:/usr/sbin:/usr/bin #PATH路径 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 分 时 日 月 周 root 命令
如上图示例:crond的命令格式:工具
分 时 日 月 周 username command
经常使用命令格式以下:
1-5:表示一个范围(从1点到5点之间执行)里面:
1,2,3:表示1点或者2点或者3点分别执行:
*/2:表示被2整除,如每两小时执行一次:
示例1:添加任务计划: crontab -e
[root@localhost ~]# crontab -e #编辑此任务计划: 0 03 * * * /bin/bash /usr/sbin/slow.backup #天天晚上凌晨三点执行/usr/sbin/slow.backup: 01 10 05 06 3 echo " " >root/1.log #每一年5月5号10点零一分星期三则清空/root/1.log这个文件: 0 */2 * * * ntpdate time.windows.com #天天隔两个小时执行时间同步脚本: 0 1,12,18 * * * /bin/bash /usr/local/iptables.sh #天天的1点、12点、18点执行/usr/local/iptables.sh 0 9-18 * * * /bin/bash /usr/local/iptables.sh #天天9点到18点执行/usr/local/iptables.sh这个脚本:
示例2:给指定用户添加任务计划:crontab -u yuanhh -e
[root@localhost ~]# crontab -u yuanhh -e 01 10 05 06 3 echo "ok" >root/cron.log #给root下的crond.log写入文件内容:
注意:建议后面的命令要写绝对路径(否则会报错),编辑时直接使用-e,不要直接编辑/var/spool/cron/下的配置文件:
2:查看当前用户及某个用户的任务计划: crontab -l
[root@localhost ~]# crontab -l 0 03 * * * /bin/bash /usr/sbin/slow/backup 01 10 05 06 3 echo "1" >root/1.log 0 */8 * * * ntpdate time.windows.com 0 1,12,18 * * * /bin/bash /usr/local/iptables.sh 0 9-18 * * * /bin/bash /usr/local/iptables.sh [root@localhost ~]# crontab -u yuanhh -l #查看yuanhh的crond计划: 01 10 05 06 3 echo "ok" >root/cron.log
3:删除当前用户及某个用户的任务计划:crontab -r
[root@localhost ~]# crontab -r #删除root的crond计划: [root@localhost ~]# crontab -l #查看root的crond计划: no crontab for root [root@localhost ~]# crontab -u yuanhh -r #删除yuanhh用户的任务计划: [root@localhost ~]# crontab -u yuanhh -l #查看yuanhh用户的任务计划: no crontab for yuanhh
注释:建议删除某个任务计划时不要使用-r命令,能够先使用crontab -e进入到任务计划,而后再删除某个任务计划:
4:启动和关闭crond任务计划:
[root@localhost ~]# systemctl stop crond #关注crond服务: [root@localhost ~]# systemctl start crond #开启crond服务: [root@localhost ~]# systemctl status crond #查看crond的状态: ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled) Active: active (running) since 一 2018-08-13 07:40:28 CST; 20s ago Main PID: 2943 (crond) CGroup: /system.slice/crond.service └─2943 /usr/sbin/crond -n 8月 13 07:40:28 localhost.localdomain systemd[1]: Started Command Scheduler. 8月 13 07:40:28 localhost.localdomain systemd[1]: Starting Command Scheduler... 8月 13 07:40:28 localhost.localdomain crond[2943]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 24% if used.) 8月 13 07:40:28 localhost.localdomain crond[2943]: (CRON) INFO (running with inotify support) 8月 13 07:40:28 localhost.localdomain crond[2943]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
二、chkconfig:服务管理工具,能够查看当前系统中有哪些服务,也能够经过此命令添加和删除服务是否开机自启动,查看服务的各个运行级别,
服务的目录保存/etc/init.d/目录下,不过从Centos 7开始不在沿用此服务管理工具:
语法: chkconfig options
options:
--list:查看使用chkconfig的服务的状态:
--add:增长指定服务:
--del:删除指定服务:
--level:指定某服务在系统运行级别状态开启或者关闭:
1:查看服务及其运行级别状态:
[root@localhost ~]# chkconfig --list 注:该输出结果只显示 SysV 服务,并不包含 原生 systemd 服务。SysV 配置数据 可能被原生 systemd 配置覆盖。 要列出 systemd 服务,请执行 'systemctl list-unit-files'。 查看在具体 target 启用的服务请执行 'systemctl list-dependencies [target]'。 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关 network 0:关 1:关 2:开 3:开 4:开 5:开 6:关 服务名称 关机 单用户 不带NFS 命令行 保留 图形化 重启
如上图例:查看时发现只有几个服务了,这是由于Centos并无沿用chkconfig服务管理(SysV),而是使用systemd做为管理工具,不过能够向下兼容使用chkconfig如图知道:须要查看服务请执行systectl list-unit-files 这个命令:
2:chkconfig --add 增长某个服务:
[root@localhost init.d]# chkconfig --add 123 #添加123这个服务: [root@localhost init.d]# chkconfig --list 123 #查看其各个级别的运行状态: 123 0:关 1:关 2:开 3:开 4:开 5:开 6:关 [root@localhost init.d]# chkconfig --level 2345 123 off #关闭2345的运行级别状态: [root@localhost init.d]# chkconfig --list 123 #查看其服务状态: 注:该输出结果只显示 SysV 服务,并不包含 原生 systemd 服务。SysV 配置数据 可能被原生 systemd 配置覆盖。 要列出 systemd 服务,请执行 'systemctl list-unit-files'。 查看在具体 target 启用的服务请执行 'systemctl list-dependencies [target]'。 123 0:关 1:关 2:关 3:关 4:关 5:关 6:关 [root@localhost init.d]# chkconfig --del 123 #删除这个服务:
3:chkconfig启动脚本类:
chkconfig 345 10 90
chkconfig 运行级别 开机时第10位启动 关机时第90为关闭
[root@localhost init.d]# cat network #! /bin/bash # # network Bring up/down networking # # chkconfig: 2345 10 90 #chkconfig 运行级别 第10位启动 第90位关闭 # description: Activates/Deactivates all network interfaces configured to \ # start at boot time. # ### BEGIN INIT INFO # Provides: $network
三、systemd服务管理:Centos开始沿用systemd做为服务管理,不在使用SysV,由于systemd支持多个服务并发启动,而SysV只能一个一个的启动,全部systemd的启动会比较快不少的:
1:启动类经常使用命令:
[root@localhost ~]# systemctl disable crond #关闭crond服务开机自启动:
[root@localhost ~]# systemctl enable crond #开启crond服务开机机自启动:
[root@localhost ~]# systemctl stop crond #关闭crond服务:
[root@localhost ~]# systemctl start crond #开启crond服务:
[root@localhost ~]# systemctl restart crond #从新启动crond服务:
[root@localhost ~]# systemctl status crond #查看crond的服务状态:
注释:伴随着某服务的开/关会创建/删除一个指向该服务的软链接“/etc/systemd/system/multi-user.target.wants/crond.service”-->“/usr/lib/systemd/system/crond.service”
[root@localhost ~]# systemctl enable crond #开启服务时: Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service. [root@localhost ~]# ls /etc/systemd/system/multi-user.target.wants/crond.service /etc/systemd/system/multi-user.target.wants/crond.service #发现此文件存在: [root@localhost ~]# systemctl disable crond 关闭服务时: Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service. ls: 没法访问/etc/systemd/system/multi-user.target.wants/crond.service: 没有那个文件或目录 #发现此文件不存在:
2:服务权限:systemd有系统和用户之分:存放位置不一样:
系统:/usr/lib/systemd/system/
用户:/etc/systemd/system/ #而用户建立通常保存在/etc/systemd/system/此目录下:
[root@localhost ~]# ls /usr/lib/systemd/system/ #系统 chrony-wait.service runlevel0.target console-getty.service runlevel1.target console-shell.service runlevel1.target.wants/ container-getty@.service runlevel2.target cpupower.service runlevel2.target.wants/ crond.service runlevel3.target cryptsetup-pre.target runlevel3.target.wants/ cryptsetup.target runlevel4.target ctrl-alt-del.target runlevel4.target.wants/ dbus-org.freedesktop.hostname1.service runlevel5.target dbus-org.freedesktop.import1.service runlevel5.target.wants/ [root@localhost ~]# ls /etc/systemd/system/ #用户 basic.target.wants default.target multi-user.target.wants dbus-org.freedesktop.NetworkManager.service default.target.wants system-update.target.wants dbus-org.freedesktop.nm-dispatcher.service getty.target.wants
3:unit:当执行ls /usr/lib/systemd/system/目录下,在此目录下,每种类型的文件都为一个unit,正是这写unit才组成了系统的各个设备、各个服务、各个资源等:
[root@localhost ~]# ls /usr/lib/systemd/system/ arp-ethers.service local-fs.target.wants swap.target auditd.service machine.slice sys-fs-fuse-connections.mount autovt@.service machines.target sysinit.target basic.target messagebus.service sysinit.target.wants basic.target.wants microcode.service sys-kernel-config.mount blk-availability.service multi-user.target sys-kernel-debug.mount bluetooth.target multi-user.target.wants syslog.socket brandbot.path NetworkManager-dispatcher.service syslog.target.wants
后缀名结尾:
.service:系统服务:
device:硬件设备:
mount:文件系统挂载点:
automount:自动挂载点:
path:文件或路径:
target:多个unit组成的组:
unit的经常使用命令:
systemctl list-units #列出正在运行的unit:
systemctl list-units #列出全部unit(失败和成功的)
systemctl lint-units --type=service #列出状态为active的service:
systemctl list-units --all --type=service #列出全部状态的service:
[root@localhost ~]# systemctl list-units #列出正在运行的unit: UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automou 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_CDRO 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 [root@localhost ~]# systemctl list-units all #列出全部unit(失败和成功) UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automou 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_CDRO 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 [root@localhost ~]# systemctl list-units --type=service #列出状态为active的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 ● kdump.service loaded failed failed Crash recovery kernel arming [root@localhost ~]# systemctl list-units --all --type=service #列出全部状态的service: UNIT LOAD ACTIVE SUB DESCRIPTION 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 ● display-manager.service not-found inactive dead display-manager.service
也能够查看某个unit是不是active: systemctl is-active crond
[root@localhost ~]# systemctl is-active crond active
四、target:相似于Centos 6的启动级别,当target支持多个target同时启动,traget实际上是多个unit的组合,系统启动就是在启动多个unit,因此用target来管理这些unit:
查看系统里全部target:
[root@localhost ~]# systemctl list-unit-files --type=service UNIT FILE STATE arp-ethers.service disabled auditd.service enabled autovt@.service enabled blk-availability.service disabled
列出一个target下包含哪些unit:
[root@localhost ~]# systemctl list-dependencies multi-user.target multi-user.target ● ├─auditd.service ● ├─brandbot.path ● ├─chronyd.service ● ├─crond.service ● ├─dbus.service ● ├─irqbalance.service
查看系统默认的target:并设置新的target:
[root@localhost ~]# systemctl get-default #查看默认的target: multi-user.target [root@localhost ~]# systemctl set-default multi-user.target #设置target: Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
注释:最后简单介绍下unit和target service的关系:
一个service属于一种类型的unit:
多个unit组成一个target:
因此一个target里面也包含了许多个service:
注释:若是想看这个服务属于哪一个target,看下面配置文件的install部分:
[root@localhost ~]# cat /usr/lib/systemd/system/sshd.service #查看sshd属于按个target: [Unit] [Install] WantedBy=multi-user.target #属于mulit-user.target
扩展:systemd添加自定义系统服务:
链接:https://www.jb51.net/article/100457.htm