12.4 linux任务计划cron chkconfig工具 systemd管理服务 unit介绍 target介绍

10.23 linux任务计划cron

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

 

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

这部分内容很重要,其实大部分系统管理工作都是通过定期自动执行某一个脚本来完成的,那么如何定期执行某一个脚本呢?这就要借助linux的cron功能了。

关于cron任务计划功能的操作都是通过crontab这个命令来完成的。其中常用的选项有:

-u :指定某个用户,不加-u选项则为当前用户;

-e :制定计划任务;

-l :列出计划任务;

-r :删除计划任务。

首先创建第一个任务计划:

[root@localhost ~]# crontab -e no crontab for root - using an empty one

使用 crontab -e 来进行编写任务计划,这实际上是使用vim工具打开了crontab的配置文件,我们写下如下内容:

01 10 05 06 3 echo "ok" > /root/cron.log

每个字段的数字分表表示什么呢?从左到右,依次为:分,时,日,月,周,命令行。而上面的例子的含义是:在6月5日(这一天必须是星期3)的10点01分执行命令 echo "ok" > /root/cron.log

crontab -e 实际上是打开了 “/var/spool/cron/username” (如果是root则打开的是/var/spool/cron/root)这个文件。使用的是vim编辑器,所以要保存的话则在命令模式下输入:wq即可。但是,千万不要直接去编辑那个文件,因为可能会出错,所以一定要使用 crontab -e 来编辑。查看已经设定的任务计划使用 crontab  -l 命令:

[root@localhost ~]# crontab -l 01 10 05 06 3 echo "ok" > /root/cron.log

删除计划任务要用 crontab -r

[root@localhost ~]# crontab -r [root@localhost ~]# crontab -l no crontab for root

10.24 chkconfig工具

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

Linux系统所有的预设服务可以查看/etc/init.d/目录得到:

我们可以使用 chkconfig --list 列出所有的服务以及每个级别是否开启:

级别(0,1,2,3,4,5,6)就是 /etc/inittab 里面的那几个启动级别了,0、1、6运行级别被系统保留:其中0作为shutdown动作,1作为重启至单用户模式,6为重启;在一般的Linux系统实现中,都使用了2、3、4、5几个级别,在CentOS系统中,2表示无NFS支持的多用户模式,3表示完全多用户模式(也是最常用的级别),4保留给用户自定义,5表示图形登录方式。

现在我们只是看到了各服务在每个级别下是否开启,那么如何去更改哪个级别下是否开启呢?

用 --level 指定级别,后面是服务名,然后是off或者on,`--level 后还可以跟多个级别:

另外还可以省略级别,默认是针对2,3,4,5级别操作:

chkconfig 还有一个功能就是可以把某个服务加入到系统服务,即可以使用 service 服务名 start 这样的形式,并且可以在 chkconfig --list 中查找到。当然也能删除掉。

[root@localhost ~]# chkconfig --del network [root@localhost ~]# chkconfig --list |grep network [root@localhost ~]# chkconfig --add network [root@localhost ~]# chkconfig --list |grep network

这个功能常用在把自定义的启动脚本加入到系统服务当中。

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是CentOS7的一个服务管理机制,systemctl list-unit-files命令可以查看所有的服务:

systemctl list-units --all --type=service命令仅仅查看service

按空格键可以往下翻页。

如果不加--all选项的话,就不会列出inactive的service。

设置服务开机启动

wKiom1nlzA2SXlqiAAFHIuewByc545.png

禁止服务开机启动:

wKioL1nlyZ2Dy0gcAAIAIcW90Xw623.png

查看服务状态:

停止服务:

wKioL1nlyZ_g2bw_AADpv-5plFQ477.png

启动服务:

wKiom1nlzFWi1V6ZAAEE0Cwv40o996.png

重启服务:

wKioL1nlyaDBe7heAAE5w_Exn1g116.png

检查服务是否开机启动:

wKioL1nlyaGiOkxQAAFmOfgiiWw268.png

设置服务开机启动时会有此服务配置文件路径的信息,这个路径是一个软链接,而这个配置文件的真正路径是/usr/lib/systemd/system/crond.service.:

如果是设置为禁止服务开机启动的话,也会有一个信息,这个信息是把那个软链接删除了的信息:

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都在/usr/lib/systemd/system/路径下:

target是由多个unit、service组成的一个组,在CentOS7里也有类似于CentOS6的运行级别,不同级别的target对应着不同的级别的运行模式:

wKioL1nlya6ST14UAARwsi7R2eM315.png

unit相关的命令
 systemctl list-units //列出正在运行的unit
 systemctl list-units --all //列出所有,包括失败的或者inactive的
 systemctl list-units --all --state=inactive //列出inactive的unit
 systemctl list-units --type=service//列出状态为active的service
 systemctl is-active crond.service //查看某个服务是否为active

systemctl is-enabled crond.service //查看某个服务是否为enabled

列出正在运行的unit:

wKiom1nlzGmwJq98AAgIK7qtfK8429.png

列出所有,包括失败的或者inactive的unit:

wKioL1nlybfhqMcXAAf7xqJALqs082.png

列出inactive的unit:

wKioL1nlybzwEAu0AAtRj-1DU74109.png

列出状态为active的service:

wKiom1nlzHehyom8AAoh7Qv8Om0838.png

查看某个服务是否为active或inactive:

wKioL1nlycKDqOJQAAGiw_P35rc654.png

查看某个服务是否为enable或disable:

wKiom1nlzHiDx3q1AAFmZfTNPgc646.png

10.27 target介绍

系统为了方便管理用target来管理unit
 systemctl list-unit-files --type=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是由多个unit、service组成的一个组,相当于unit、service的一个集合,但是target下也可以包含target。

列出系统里所有的target:

查看系统默认的target:

在CentOS7里可以通过修改target来改变系统的运行级别。

设置默认的target,会创建一个软链接:

  想要查看某个service属于哪个target的话,cat那个service的文件内容看Install部分就知道了,例如要查看sshd.service属于哪个target:

所以target就是由多个unit组成的,而unit又是由多个service组成的,所以target包含unit和service,而target下也可以包含target。

评论

暂无评论