linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

linux任务计划cron

linux任务计划:在某个时间自动执行命令或者脚本。

任务计划的配置文件
cat /etc/crontab

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

前面两行是定义变量,第三行是指发送邮件给谁,然后最后一行有五个*(星号)分别对应着五个位,也就是上面的五行,下面来介绍一下分别表示什么意思:

1.表示分钟(0-59)

2.表示小时(0-23)

3.表示日期(1-31)

4.表示月份(1-12可以写数字或者英文的简写)

5.表示星期(0-6,0或者7表示周日,也可以写成英文的简写)

最后一行开头部分是用户(在root用户下不写默认就是root)

后面部分,也就是com开头的位置是你要执行的命令。

自定义任务计划(用法和vim一样)
crontab -e

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

案例:每天3点执行123.sh脚本(星号就是所有,比如第三个是 意思就是1-31日都执行)
0 3 * * * /bin/bash /usr/local/sbin/123.sh

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

把正确的结果和错误的结果都输出到123.log里
0 3 * * * /bin/bash /usr/local/sbin/123.sh >/tmp/123.log 2>/tmp/123.log

把正确的结果和错误的结果都追加到123.log里
0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

每隔两个月的1-10号的星期二和星期五的三点 可以写成如下格式。0是分钟,3是小时,1-10是日期1号到10号,星号/2是月份除以2可以整除的那个月份,2,5是这个月的周二和周五
0 3 1-10 */2 2,5

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

设置好之后我们还需要使用systemctl start crond命令启动cron才能启用计划
systemctl start crond

查看是否启用计划
ps aux |grep crom

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

或者使用systemctl start crom(crom是你的计划名,图片中红色的就是计划名)
systemctl start crom

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍绿色就代表已经启用了)

有时候计划了执行脚本,但是没有执行很可能是没有使用绝对路径,解决办法要么在计划里把该条命令的路径添加到PATH里,要么就使用绝对路径。

还有就是建议计划的任务都要写上正确和错误的结果追加到某个文件里,这样才能保证任务有据可查。

-l 列出,查看任务计划列表
crontab -l

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

任务计划cron的文件在/var/spool/cron/目录下,如果是root的cron就是/var/spool/cron/root(cat查看)

-r删除计划
crontab -r

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

-u指定用户
crontab -u root -l(指定查看root的任务计划)

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

chkconfig服务管理工具

chkconfig服务管理工具(主要在centos6及以前的系统中使用,如怎么控制服务启动,如何控制服务开机启动、如何控制服务在指定级别启动等等)

查看当前系统中使用chkconfig的服务(只会列出sysv服务管理模式的进程,centos7很多的都是systemd模式的)
chkconfig --list

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

使用如上命令在centos7只能查看到两个服务,如下图

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

这两个进程文件在如下路径(之后我们可以脚本放到这个目录里用chkconfig工具管理也是可以的)
ls /etc/init.d/

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

表示该服务在0-6级别分别是什么状态,开机启动还是关闭(network是需要关闭或开启的服务名,off是关闭on是开启)
chkconfig network off

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

0-6这七个级别,每个级别的含义

0级别是关机状态、1级别是单用户、2是多用户模式(没有nfs服务)、3是是用户模式(不带图形)、4保留的级别、5多用户模拟(带图形)、6重启

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

指定network的3级别关闭
chkconfig --level 3 network off

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

指定network的3级别和5级别关闭(3跟5中间不加任何符号,也不加空格)
chkconfig --level 35 network off

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

把123加入到服务列表里来(需要把123放到/etc/init.d目录下来,并且确定123是个脚本,同是需要有如下两行格式才能识别)
chkconfig --add 123

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

从服务列表删除
chkconfig --del 123

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

systemd管理服务

systemd是centos7的管理服务的机制。

列出systemd模式的服务
systemctl list-units --all --type=service

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

让服务开机启动,service是服务名(显示出来的是软连接,然后ls这个软连接就可以得到服务的文件地址)
systemctl enable crond.service

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

不让服务开机启动,service是服务名
systemctl disable crond.service

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

查看服务状态,service是服务名
systemctl status crond.service

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

停止服务,service是服务名
systemctl stop crond.service

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

启动服务,service是服务名
systemctl start crond.service

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

重启服务,service是服务名
systemctl restart crond.service

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

检查服务是否开机启动,service是服务名(enabled就是启动)
systemctl is-enabled crond.service

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

unit介绍

上一节课里systemctl enable crond获得的服务文件所在目录,也就是ls /usr/lib/systemd/system,目录里的所有文件都叫做unit。

unit主要分为一下几种类型。

service 系统服务

target 多个unit组成的组

device 硬件设备

mount 文件系统挂载点

automount 自动挂载点

path 文件或路径

scope 不是由systemd启动的外部进程

slice 进程组

snapshot systemd快照

socket 进程间通信套接字

swap swap文件
linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

timer 定时器

unit相关的命令
列出正在运行的unit
systemctl list-units

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

列出所有,包括失败的或者inactive的
systemctl list-units --all

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

列出inactive的unit
systemctl list-units --all --state=inactive

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

列出状态为active的service
systemctl list-units --type=service

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

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

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

target介绍

系统为了方便管理用target来管理unit

查看所有target。
systemctl list-unit-files --type=target

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

查看指定target下面有哪些unit。
systemctl list-dependencies multi-user.target

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

查看系统默认的target。
systemctl get-default

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

设置默认的target。
systemctl set-default multi-user.target

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

一个service属于一种类型的unit
多个unit组成了一个target
一个target里面包含了多个service

查看指定sevice属于那个target。(看install位置)
cat /usr/lib/systemd/system/sshd.service

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

转载于:https://blog.51cto.com/13658403/2115353

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值