crontab/chkconfig/systemctl

查看配置文件:/etc/crontablinux

[root@linux ~]# 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

从配置文件中能够看出,5个星号从左到右分别表示: 分(取值范围:0-59) 时(取值范围:0-23) 日(取值范围:1-31) 月(取值范围:1-12)或者 jan,feb,mar,apr等英文简写表示 星期几(取值范围:0-6,星期天等于0或者7)或者mon,tue,wed,thu,fri,sat,sunbash

星号(*):表明全部可能的值,例如month字段若是是星号,则表示在知足其它字段的制约条件后每个月都执行该命令操做。网络

逗号(,):能够用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”session

中杠(-):能够用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”socket

正斜线(/):能够用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线能够和星号一块儿使用,例如*/10,若是用在minute字段,表示每十分钟执行一次。rest

crontab任务以用户来区分,对应用户的任务计划文件位置:/var/spool/cron/ 该目录下,目录名表明用户名,每一个用户设定的任务计划在各自的目录中,如需备份直接复制该目录中的crontab文件便可code

启动:systemctl start crond.servicecrontab

关闭:systemctl stop crond.serviceip

重启:systemctl restart crond.serviceci

查看状态:systemctl status crond.service (active为绿色即运行中,白色未运行)

-u 参数:指定用户(查看、新增、删除指定用户的任务计划)

-e 参数:编辑crontab任务计划,如不指定用户,默认编辑当前用户的crontab文件

-l 参数:显示任务计划内容,如不指定用户,默认显示当前用户的任务计划

-r 参数:删除某个用户的crontab文件,如不指定用户,默认删除当前用户的crontab文件

备份crontab:

1.拷贝/var/spool/cron/目录下的文件

2.crontab -l > /home/cronbak

恢复:crontab /home/cronbak

实例1:每1分钟执行一次command

* * * * * command

实例2:每小时的第1和第30分钟执行

1,30 * * * * command

实例3:在上午7点到10点的第1和第30分钟执行

1,30 8-11 * * * command

实例4:每隔两天的上午7点到10点的第1和第30分钟执行

1,30 7-10 */2 * * command

实例5:每一个星期一的上午7点到10点的第1和第30分钟执行

1,30 8-11 * * 1 command

实例6:每晚的22:30重启firewalld

30 22 * * * /usr/bin/systemctl restart firewlld.service

实例7:每个月一、十、25日的5 : 30重启firewalld

30 5 1,10,22 * * /usr/bin/systemctl restart firewlld.service

实例8:每周6、周日的2 : 30重启firewalld

30 2 * * 6,0 /usr/bin/systemctl restart firewlld.service

实例9:天天19:00至23:00之间每隔20分钟重启一次firewalld

0,20,40 19-23 * * * /usr/bin/systemctl restart firewlld.service

实例10:每星期六的晚上11 : 00 pm重启firewalld

0 23 * * 6 /usr/bin/systemctl restart firewlld.service

实例11:每小时重启一次firewalld

* */1 * * * /usr/bin/systemctl restart firewlld.service

实例12:晚上10点到早上8点之间,每隔一小时重启firewalld

* 22-8/1 * * * /usr/bin/systemctl restart firewlld.service

实例13:每个月的4号与每周一到周三的11点重启firewalld

30 2 5 * mon-wed /usr/bin/systemctl restart firewlld.service

实例14:一月一号的5点重启firewalld

0 5 1 jan * /usr/bin/systemctl restart firewlld.service

实例15:五月每隔7天重启firewalld * * */7 5 * /usr/bin/systemctl restart firewlld.service

查看系统服务:

[root@linux ~]# 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:关

系统显示了netconsole和network两个服务,对应的值表示各个运行级别下的服务开关状态,若是是开,该服务将开机启动,服务脚本路径在/etc/init.d/下:

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

运行级别: runlevel命令查看

0:表示关机状态 1:表示单用户模式 2:表示多用户模式(与3级别相比没有NFS服务) 3:多用户模式 4:未使用,系统保留 5:图形界面的多用户模式 6:表示重启

开启服务:

[root@linux ~]# chkconfig network on

关闭服务:

[root@linux ~]# chkconfig network off

指定运行级别下关闭或开启操做:

[root@linux ~]# chkconfig --level 345 network off
[root@linux ~]# chkconfig --list 
network        	0:关	1:关	2:开	3:关	4:关	5:关	6:关

--level :指定操做的运行级别,示例中指定3,4,5级别下关闭

添加自定义脚本到chkconfig管理:

1.先将脚本放入目录 /etc/init.d/中

[root@linux ~]# cp /etc/init.d/network /etc/init.d/test
[root@linux ~]# ls /etc/init.d/
functions  netconsole  network  README  test

2.添加服务:

[root@linux ~]# chkconfig --add test
[root@linux ~]# chkconfig --list
test           	0:关	1:关	2:开	3:开	4:开	5:开	6:关

3.删除服务:

[root@linux ~]# chkconfig --del test

在Centos7如下版本,主要使用chkconfig管理系统服务,Centos7中主要使用systemd管理,Centos7也保留了chkconfig命令

列出系统服务:

[root@linux ~]# systemctl list-unit-files

该命令会列出全部系统服务,以及服务的状态(enabled、disabled、static:表示该服务与其余服务关联,不能单独设置状态,可能须要等待其余服务唤醒)

只查看service服务:

[root@linux ~]# systemctl list-units --all --type=service

不加- -all 只列出在运行的(active)服务,未运行(inactive)的服务不会显示

启动服务:

[root@linux ~]# systemctl restart crond.service

关闭服务:

[root@linux ~]# systemctl stop crond.service

重启服务:

[root@linux ~]# systemctl restart crond.service

查看服务状态:

[root@linux ~]# systemctl status crond.service

开机禁止启动:

[root@linux ~]# systemctl disable crond.service

开机启动:

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

查看服务是否开机启动:

[root@linux ~]# systemctl is-enabled crond.service 
enabled

unit:

服务文件路径:

root@linux ~]# ls /usr/lib/systemd/system

该目录中包含系统全部服务的文件,将这些服务文件称为unit,unit分为如下类型: service 系统服务 target 多个unit组成的组 device 硬件设备 socket 网络通讯的套接字 swap sawp文件 等等……

显示正在运行的unit:

[root@linux ~]# systemctl list-units

- -all 显示全部,包括inactive的unit

显示状态为inactive的unit;

[root@linux ~]# systemctl list-units --all --state=inactive

显示状态为active而且类型为service的unit:

[root@linux ~]# systemctl list-units --type=service

查看某个服务是否为active:

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

target:

target中包含多个unit,能够理解为用于管理unit,一个service属于一种类型的unit,多个unit组成了一个target

查看系统中的target:

[root@linux ~]# systemctl list-unit-files --type=target

查看指定的target中包含了哪些unit:

[root@linux ~]# systemctl list-dependencies multi-user.target

target中还能够包含其余target

查看系统默认的target:

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

设置系统默认的target:

[root@linux ~]# systemctl set-default reboot.target

若是将reboot.target设为默认target,那么系统开机后会一直重启

查看指定unit属于哪一个target:(服务文件中的 [Install] 项)

[root@linux ~]# 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   #属于mulit-user.target
本站公众号
   欢迎关注本站公众号,获取更多信息