cron、chkconfig、systemd、unit、target

1、Linux 任务计划 cron

cron、chkconfig、systemd、unit、target

先来看一下任务计划的配置文件,输入命令 cat /etc/crontab ,回车,见下图,
cron、chkconfig、systemd、unit、target
重点是上图第二个红色框框的内容,前面的五个“ ”,分别表示分钟、小时、日期、月份、星期。user-name 表示用户名,没有的话默认是 root ,command to be executed 表示要执行的命令。
那么,怎么去定义任务计划呢?输入命令 crontab -e,回车,就会进入到配置文件里,见下图,
cron、chkconfig、systemd、unit、target
用法和 vim 是一样的,按字母“i”进入编辑模式,“Esc”退出编辑,“:wq”保存并退出, “:q”退出,“:q!”强制退出。
现在来讲解一下任务计划:
“0 3
”:表示每天的凌晨3点。表示所有的范围。分钟位就是0-59,小时位表示0-23,日期表示1-31,月份表示1-12,星期表示1-7。
“0 3 1-10 /2 2,5 ”:表示双月1-10号的周二和周五凌晨3点。其中,月份这边“/2”表示可以被2除,就是双月的意思,小时这边/2就表示每隔两小时。
这边没有年份,是因为星期可以确定唯一性,每一年的月份和星期都不一样。
0 3 1-10 /2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log 这个任务计划里,/bin/bash 表示用户,/usr/local/sbin/123.sh 表示文件里面有个shell脚本,执行里面的命令。>>/tmp/123.log 2>>/tmp/123.log 表示任务计划的结果全部追加到文件 /tmp/123.log 里面。
任务计划写入完毕,保存并退出,还需要再启用服务,输入命令 systemctl start crond,回车即可。然后再检查一下服务有没有启动,输入命令 ps aux |grep cron,回车就可以看到进程。
cron、chkconfig、systemd、unit、target
还可以用命令 systemctl status crond,来查看状态,
cron、chkconfig、systemd、unit、target
如果是绿色的就表示已启动,停用的话没有颜色显示。
注意事项:
shell脚本里面的命令要使用绝对路径,否则会没有执行。
不在配置文件里面的PTAH中的命令就需要使用绝对路径,还有一种方法就是将命令加入到配置文件的PTAH中。
最稳妥的办法还是直接写绝对路径比较好,PTAH里面的内容不要去动。
写任务计划的时候,最好全部追加到一个日志文件里,正确和错误的输出都要写上,这样才有据可查。
怎么备份?使用命令 crontab -l,可以看到任务计划。crontab 的文件在 /var/spool/cron/ 目录里,按用户名分成多个文件,例如 root 用户在 /var/spool/cron/root 里。这个文件里面的内容就是命令 crontab -l 执行结果列出的内容。所以备份的话,直接复制这个文件就可以了。选项 -r 是删除的意思。
总结:
crontab -e 编辑
crontab -l 查看
crontab -r 删除

2、chkconfig 工具

cron、chkconfig、systemd、unit、target

chkconfig 这个工具的应用是在 CentOS6 及以前的版本,在CentOS7版本里面没有使用了,但是为了跟之前的版本兼容,依然可以使用 chkconfig。
查看当前系统使用 chkconfig 工具的服务,输入命令 chkconfig --list,回车,见下图,
cron、chkconfig、systemd、unit、target
可以看到只有有两个服务netconsole和network,那么其他的服务在哪里呢?注意阅读上图的注意内容,大概意思就是CentOS6 及以前的版本使用的服务是基于SysV服务,而在CentOS7版本里面使用的服务是基于systemd服务。
接着输入命令 top 来查看,回车,见下图,
cron、chkconfig、systemd、unit、target
可以看到systemd也有一个进程,它的PID是1,说明这个进程是很重要的。在CentOS6 及以前的版本中进程PID为1的进程的是init.d。
chkconfig 这个工具中仅剩的两个服务netconsole和network,存在于文件 /etc/init.d/ 中,见下图,
cron、chkconfig、systemd、unit、target
还有一个用法,关闭和开启服务命令,具体操作见下图,
cron、chkconfig、systemd、unit、target
上图中可以看到,netconsole和network都有7个级别,其中networkd的2、3、4、5级别都是开启的状态,使用命令 chkconfig network off 之后,就全部关闭了,使用命令 chkconfig network on 之后,又可以开启。这7个级别分别表示:0 表示关机,1表示单用户模式,2表示多用户模式(不带 图形,少nfs服务),3表示多用户模式(不带图形),4表示保留级别(暂时没用),5表示多用户模式(带图形),6表示重启。在CentOS6 及以前的版本中才有这些级别之分,开机时定位在哪个级别,就会启动哪个级别的模式。现在的CentOS7版本就没有这些级别之分了。在CentOS6 及以前的版本中,更改文件 /etc/inittab 可以去定义开机的运行级别, 输入命令 vi /etc/inittab,回车,见下图,
cron、chkconfig、systemd、unit、target
上图显示的是CentOS7版本的内容,没有7个级别之分。
可以指定某一个级别关闭,具体操作见下图,
cron、chkconfig、systemd、unit、target
也可以指定某一级别开启,见下图,
cron、chkconfig、systemd、unit、target
这边注意一下,0、1、6级别最好不要开启。
可以把脚本加入到服务列表里面,现在来自定义一个服务,见下图,
cron、chkconfig、systemd、unit、target
这边注意,添加的服务要在 /etc/init.d/ 目录下,才可以添加成功。文件名称随意,但是文件格式有要求,输入命令 vim 123,回车查看,见下图,
cron、chkconfig、systemd、unit、target
删除自定义服务,输入命令 chkconfig --del 123,回车,见下图,
cron、chkconfig、systemd、unit、target

总结:
chkconfig --list 列出所有的服务
chkconfig network off 关闭服务
chkconfig network on 开启服务
chkconfig --level 3 network off 关闭3级别服务
chkconfig --level 45 network off 同时关闭4和5级别服务
chkconfig --add 123 将123服务添加到chkconfig工具里

3、systemd 管理服务

cron、chkconfig、systemd、unit、target

上面使用命令 chkconfig --list 查看时,提示我们使用命令 systemctl list-unit-files 查看,回车,见下图,
cron、chkconfig、systemd、unit、target
内容太多太乱,比较复杂,仅截图最后的一屏。还有一个相对简单点的命令,systemctl list-units --all --type=service,仅查看 service 文件,回车,见下图,
cron、chkconfig、systemd、unit、target
使用空格键往下翻到最后一页,见下图,
cron、chkconfig、systemd、unit、target
有一些解释和提醒,提醒查看所有的文件使用刚刚的命令 systemctl list-unit-files。
命令不加 all 的情况,见下图,输入命令 systemctl list-units --type=service,回车,
cron、chkconfig、systemd、unit、target
由上图可以看出,该命令只列出了 active 状态的文件,没有列出 inactive 状态的文件。
接下来介绍几个常用的服务相关的命令。
systemctl enable crond.service 让服务开机启动,这边的 .service 是可以省略的。
systemctl disable crond 禁止服务开机启动
systemctl status crond 查看服务状态
cron、chkconfig、systemd、unit、target
systemctl stop crond 停止服务
systemctl start crond 启动服务
systemctl restart crond 重启服务
systemctl is-enabled crond 检查服务是否开机启动
cron、chkconfig、systemd、unit、target
可以查看配置文件 /etc/systemd/system/multi-user.target.wants/crond.service 的内容,它实际上是一个软链接文件,见下图,
cron、chkconfig、systemd、unit、target
接着关闭 crond 服务,见下图,
cron、chkconfig、systemd、unit、target
可以看到软链接文件失效了,开启服务之后,又重新创建了软链接文件。

4、unit 介绍

cron、chkconfig、systemd、unit、target

上面的软链接文件指向的原文件是 /usr/lib/systemd/system/crond.service ,使用命令 cat /usr/lib/systemd/system/ ,回车,进入查看,见下图,
cron、chkconfig、systemd、unit、target
cron、chkconfig、systemd、unit、target
cron、chkconfig、systemd、unit、target
这个路径下有很多类型的文件和目录,这些文件都叫 unit 。
主要有以下几种类型:
service 系统服务
target 多个unit组成的组
device 硬件设备
mount 文件系统挂载点
automount 自动挂载点
path 文件或路径
scope 不是由systemd启动的外部进程
slice 进程组
snapshot systemd 快照
socket 进程间通信套接字
swap swap文件
timer 定时器
接着进入 /usr/lib/systemd/system/ 目录下,查看 runlevel,见下图,
cron、chkconfig、systemd、unit、target
上图可以看出,CentOS7这边也有7个运行级别,分别是0-6的target软链接文件,指向的是后面的路径。这7个运行级别,与CenOS6及之前的版本,基本上都可以对应起来。
接着来看一下,unit的几个相关命令,
cron、chkconfig、systemd、unit、target
列出正在运行的unit,输入命令 systemctl list-units,回车,见下图,
cron、chkconfig、systemd、unit、target
内容比较多,而且乱,截取最后一屏,可以看到有119个正在运行。
想要查看全部的unit,输入命令 systemctl list-units --all,回车,见下图,
cron、chkconfig、systemd、unit、target
同样截取最后一屏,可以看到一共是有231个unit。所有列出来的unit中,包括失败的或者inactive的。
我们还可以指定unit的状态,来列出结果,输入命令 systemctl list-units --all --state=inactive,回车,见下图,
cron、chkconfig、systemd、unit、target
还可以列出状态为 active 的 service,输入命令 systemctl list-units --type=service,回车,见下图,
cron、chkconfig、systemd、unit、target
上图中,failed 状态的也列出来了,这是一个特例。输入命令 systemctl list-units --type=service --state=active,回车,见下图,
cron、chkconfig、systemd、unit、target
就看不到 failed 状态,全部都是 active 的 service。输入命令 systemctl list-units --all --type=service,回车,见下图,
cron、chkconfig、systemd、unit、target
就能看到全部状态的 service。
接着查看某个服务是否为 active ,输入命令 systemctl is-active crond.service,回车,见下图,
cron、chkconfig、systemd、unit、target

5、target 介绍

cron、chkconfig、systemd、unit、target

使用命令 systemctl list-unit-files --type=target,可以列出系统里的所有 target ,见下图,
cron、chkconfig、systemd、unit、target
查看指定 target 下面有哪些 unit,输入命令 systemctl list-dependencies multi-user.target,回车,见下图,
cron、chkconfig、systemd、unit、target
大部分都是 service ,少部分 socket、mount、path等。做个对比,输入命令 systemctl list-dependencies rescure.target,回车,见下图,
cron、chkconfig、systemd、unit、target
还可以查看系统默认的 target ,输入命令 systemctl get-default,回车,见下图,
cron、chkconfig、systemd、unit、target
这边更改 target 类型,可以更改开机的启动级别,类似CentOS6及以前版本的更改配置文件来实现更改运行级别。设置默认的 target ,输入命令 systemctl set-default multi-user.target,回车,见下图,
cron、chkconfig、systemd、unit、target
上图中,文件 /etc/systemd/system/default.target 就是一个软链接文件,见下图,
cron、chkconfig、systemd、unit、target
unit 和 service 的关系如下:
一个 service 属于一种类型的 unit
多个 unit 组成了一个 target
一个 target 里面包含了多个 service
接着来查看一下 ssh.service 属于哪一个 target,输入命令 cat /usr/lib/systemd/system/sshd.service,回车,见下图,
cron、chkconfig、systemd、unit、target
重点关注上图红色框框的位置,可以看到 ssh.service 属于 multi-user.target 。也可以查看crond.service ,见下图,
cron、chkconfig、systemd、unit、target

总结:

cat /etc/crontab 查看任务计划的配置文件
user-name command to be executed ” 五个“ ”分别表示分钟、小时、日期、月份、星期,user-name 表示用户名,没有的话默认是 root ,command to be executed 表示要执行的命令。
crontab -e 进入配置文件,输入命令行保存退出
0 3 1-10 星/2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log 这个任务计划里,/bin/bash 表示用户,/usr/local/sbin/123.sh 表示文件里面有个shell脚本,执行里面的命令。>>/tmp/123.log 2>>/tmp/123.log 表示任务计划的结果全部追加到文件 /tmp/123.log 里面。
systemctl start crond 重启服务
ps aux |grep cron 检查进程
systemctl status crond 查看状态
crontab -e 编辑
crontab -l 查看
crontab -r 删除

chkconfig --list 查看当前系统使用 chkconfig 工具的服务
chkconfig network off 关闭服务
chkconfig network on 开启服务
chkconfig --level 3(或345) network off 关闭指定的一个或多个级别服务
chkconfig --level 3(或345) network on 开启指定的一个或多个级别服务
cd /etc/init.d/
cp network 123 这两步操作为下面的增加或删除自定义服务做准备
chkconfig --add 123 增加自定义服务
chkconfig --del 123 删除自定义服务

systemctl list-unit-files 查看所有的unit服务
systemctl list-units --all --type=servic 仅查看 service 类型的服务
systemctl list-units --type=service 只列出 active 状态的文件
systemctl enable crond.service 让服务开机启动,这边的 .service 是可以省略的。
systemctl disable crond 禁止服务开机启动
systemctl status crond 查看服务状态
systemctl stop crond 停止服务
systemctl start crond 启动服务
systemctl restart crond 重启服务
systemctl is-enabled crond 检查服务是否开机启动

systemctl list-units 列出正在运行的unit
systemctl list-units --all 列出所有,包括失败的或者inactive的
systemctl list-units --all --state=inactive 列出指定状态inactive的unit
systemctl list-units --type=service 列出指定状态service的unit,failed 状态也会出现
systemctl list-units --type=service --state=active 仅列出指定状态service的unit
systemctl list-units --all --type=service 列出全部状态的 service
systemctl list-units --type=service 列出状态为active的service
systemctl is-active crond.service 查看某个服务是否为active

systemctl list-unit-files --type=target 列出系统里的所有 target
systemctl list-dependencies multi-user.target 查看指定 target 下面有哪些 unit
systemctl get-default 查看系统默认的 target
systemctl set-default multi-user.target 设置默认的 target
cat /usr/lib/systemd/system/sshd.service 查看 ssh.service 属于哪一个 target
unit 和 service 的关系如下:
一个 service 属于一种类型的 unit
多个 unit 组成了一个 target
一个 target 里面包含了多个 service

转载于:https://blog.51cto.com/13503302/2096909

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值