了解systemctl和chkconfig的服务管理工具
crontab计划任务
crontab计划任务的计时方式:对大部分时间单位都清楚的划分,具体可以划分到分钟,时间单位有分钟,小时,日(一个月内的第几天),月,周(一周的第几天,计划任务中可以是指定为每隔几周来执行某些任务) [[email?protected] ~]# 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) 分钟时间单位,从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
[[email?protected] ~]# systemctl start crond.service [[email?protected] ~]# systemctl status crond.service ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2018-07-17 01:13:53 CST; 12h ago Main PID: 1015 (crond) CGroup: /system.slice/crond.service └─1015 /usr/sbin/crond -n Jul 17 01:13:53 localhost.localdomain systemd[1]: Started Command Scheduler. Jul 17 01:13:53 localhost.localdomain systemd[1]: Starting Command Scheduler... Jul 17 01:13:53 localhost.localdomain crond[1015]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 18% if used.) Jul 17 01:13:53 localhost.localdomain crond[1015]: (CRON) INFO (running with inotify support) [[email?protected] ~]# ps -aux |grep crond root 1015 0.0 0.1 126228 1592 ? Ss 09:07 0:00 /usr/sbin/crond -n root 3752 0.0 0.0 112652 960 pts/0 R+ 13:54 0:00 grep --color=auto crond
[[email?protected] ~]# crontab -l 1 5 */7 * * cp /etc/passwd /usr/local/src/ [[email?protected] ~]# cat /var/spool/cron/root 1 5 */7 * * cp /etc/passwd /usr/local/src/ 45 4 1,10,22 * * /etc/init.d/httpd restart 上面的例子表示每月1、10、22日的4:45重启apache。 10 1 * * 6,0 /etc/init.d/httpd restart 上面的例子表示每周六、周日的1:10重启apache。 0,30 18-23 * * * /etc/init.d/httpd restart 上面的例子表示在每天18:00至23:00之间每隔30分钟重启apache。 0 23 * * 6 /etc/init.d/httpd restart 上面的例子表示每星期六的23:00 重启apache ```。 0 /1 /etc/init.d/httpd restart 0 23-7/1 * /etc/init.d/httpd restart 0 11 4 * mon-wed /etc/init.d/httpd restart 0 4 1 jan * /etc/init.d/httpd restart ----- # chkconfig服务管理工具 在Centos7中保留chkconfig服务管理的方式,我们可以用chkconfig新增自定义服务脚本文件来管理编译安装的服务 centos7系统中关于chkconfig的描述: ----- 注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 ----- ----- chkconfig的选项用法: --add 增加所指定的系统服务,让chkconfig管理它,并同时在系统启动的叙述文件 --del 删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的叙述文件内删除相关数据。 --level <运行级别> 指定读系统服务要在哪一个执行等级中开启或关闭 ----- 0:表示关机 1:单用户模式 2:无网络连接的多用户命令行模式 3:有网络连接的多用户命令行模式 4:不可用 5:带图形界面的多用户模式 6:重新启动 ----- 使用范例,以httpd服务为例: chkconfig --list #列出所有的系统服务 chkconfig --add httpd #增加httpd服务 chkconfig --del httpd #删除httpd服务 chkconfig --level httpd 2345 on #设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态 chkconfig --list #列出系统所有的服务启动情况 chkconfig --list httpd #列出httpd服务设置情况 chkconfig --level 35 httpd on #设定httpd在等级3和5为开机运行服务,--level 35表示操作只在等级3和5执行,on表示启动,off表示关闭 chkconfig httpd on #设定httpd在各等级为on,“各等级”包括2、3、4、5等级 ----- 在chkconfig管理中增加一个服务: 服务脚本必须存放在/etc/ini.d/服务脚本目录下 : chkconfig --add 服务的脚本名称 在chkconfig工具服务列表中增加此服务,此时服务会被在/etc/rc.d/rcN.d中赋予K/S入口了; chkconfig --level 35 服务脚本名 on 修改服务的默认启动等级。 ----- # systemd服务管理 systemd是Centos7版本及更高版本中新增的一个服务管理方式,跟centos6之前的chkconfig服务管理不同的地方是,增加了 ----- systemctl用法 使用systemctl list-units -all --type=service列出所有在运行状态服务的详细信息(正在运行和暂停的服务),--type=service是指定服务类型的内容,rinning正在运行的,inactive 不活跃的 ----- [[email?protected] locales]# systemctl list-units -all --type=service ----- systemctl添加一个服务,并启动、查看服务状态、停止服务和重启服务,这里以crond服务为例 systemctl enable 添加服务,添加服务时如有软连接会直接显示出出来其连接路径,如下(第二行内容): ----- [[email?protected] locales]# systemctl enable crond systemctl start 例如启动crond服务 systemctl status 查看服务的运行状态,服务状态如下 systemctl stop 停止一个服务,并查看它的状态信息 systemctl restart 重启一个服务 使用systemctl is-enabled 服务名 查看一个服务是否开机启动 ----- # unit介绍 在Centos6中chkconfig管理的服务会拥有几个运行级别的区分,而centos7中也有类似的启动级别区分,systemctl启动的脚本存放在/usr/lib/systemd/system/目录下,查看系统所有的unit ----- [[email?protected] locales]# ls /usr/lib/systemd/system ----- unit中的级别 runlevel0 poweroff 关机 runlevel1 rescue 单用户救援 ......234级别略 runlevel5 graphical 图形化 runlevel6 reboot 重启 -----  ----- systemd中的unit类型,我们常见的有以.service结尾的,是为服务提供启动停止管理的unit service 系统服务 target 多个unit组成的组 device 硬件设备 mount 文件系统挂载点 automount 自动挂载点 path 文件或路径 scope 不是由systemd启动的外部进程 slice 进程组 snapshot systemd 快照 socket 进程间通信套接字 swap swap文件 timer 定时器 ----- 几个unit的管理使用命令 systemctl list-units 列出所有正在运行的unit systemctl list-units --all 列出所有的unit包括失败和不活跃的(inactive) systemctl list-units --all --state=inactive 列出所有不活跃(inactive)service的 unit systemctl list-units --type=service 列出所有正在运行的unit,状态为active systemctl is-active crond.service 查看某个服务是否为active ----- # target管理 查看所有的target [[email?protected] system]# systemctl list-unit-files --type=target 查看一个target下有哪些unit [[email?protected] system]# systemctl list-dependencies multi-user.target systemctl get-default 查看默认的target systemctl set-default multi-user.target 指定默认的target,例如指定图形界面的target启动级别 一个service属于一种类型的unit,多个unit组成一个target,target中包含多个service 查看service属于哪个target(查看install部分的内容),如sshd.service [[email?protected] system]# cat /usr/lib/systemd/system/sshd.service [Service] [Install] 从这部分查看属于哪个target,这里的.service中写入的配置是服务启动管理的内容WantedBy=multi-user.target (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |