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

可莉
• 阅读 646

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

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

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

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

这部分内容很重要,其实大部分系统管理工作都是通过定期自动执行某一个脚本来完成的,那么如何定期执行某一个脚本呢?这就要借助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/目录得到:

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

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

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

级别(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表示图形登录方式。

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

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

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

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

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

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

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命令可以查看所有的服务:

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

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

按空格键可以往下翻页。

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

设置服务开机启动

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

禁止服务开机启动:

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

查看服务状态:

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

停止服务:

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

启动服务:

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

重启服务:

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

检查服务是否开机启动:

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

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

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

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

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

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/路径下:

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

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

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

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:

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

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

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

列出inactive的unit:

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

列出状态为active的service:

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

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

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

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

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

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:

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

查看系统默认的target:

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

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

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

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

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

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

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

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这