linux 任务计划cron

10.23 linux任务计划cron

配置文件, cat  /etc/crontab

重点是上图第二个红色框框的内容,前面的五个“ ”,分别表示分钟、小时、日期、月份、星期。user-name 表示用户名,没有的话默认是 root ,command to be executed 表示要执行的命令。

输入命令 crontab -e,回车,就会进入到配置文件里

用法和 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 查看进程

 systemctl status crond   查看状态

如果是绿色的就表示已启动,停用的话没有颜色显示。

扫描二维码关注公众号,回复: 182327 查看本文章

注意事项:
shell脚本里面的命令要使用绝对路径,否则会没有执行。
不在配置文件里面的PTAH中的命令就需要使用绝对路径,还有一种方法就是将命令加入到配置文件的PTAH中。
最稳妥的办法还是直接写绝对路径比较好,PTAH里面的内容不要去动。
写任务计划的时候,最好全部追加到一个日志文件里,正确和错误的输出都要写上,这样才有据可查。
怎么备份?使用命令 crontab -l,可以看到任务计划。crontab 的文件在 /var/spool/cron/ 目录里,按用户名分成多个文件,例如 root 用户在 /var/spool/cron/root 里。这个文件里面的内容就是命令 crontab -l 执行结果列出的内容。所以备份的话,直接复制这个文件就可以了。选项 -r 是删除的意思。
总结:
crontab -e 编辑
crontab -l 查看
crontab -r 删除

10.24 chkconfig工具

chkconfig 这个工具的应用是在 CentOS6 及以前的版本,在CentOS7版本里面没有使用了,但是为了跟之前的版本兼容,依然可以使用 chkconfig。

命令 chkconfig --list  查看当前系统使用的chkconfig服务

可以看到只有有两个服务netconsole和network,那么其他的服务在哪里呢?注意阅读上图的注意内容,大概意思就是CentOS6 及以前的版本使用的服务是基于SysV服务,而在CentOS7版本里面使用的服务是基于systemd服务。
chkconfig 这个工具中仅剩的4个服务,存在于文件 /etc/init.d/ 中

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 可以去定义开机的运行级别

chkconfig --level3 network off   3级别关闭

chkconfig --level45 network off  45级别关闭

chkconfig --level345 etwork on   345级别开启

可以把脚本加入到服务列表里面,自定义一个服务,添加的服务要在 /etc/init.d/ 目录下,才可以添加成功

输入命令 chkconfig --del 123  删除自定义服务

总结:
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工具里

 10.24 systemd管理服务

systemctl list-unit-files     //列出所有启动文件

systemctl list-units --all --type=service  //仅查看service文件

 systemctl list-units --type=service     // 列出启动的service 文件

systemctl enable crond.service   //让服务开机启动,这边的 .service 是可以省略的。
systemctl disable crond  //禁止服务开机启动
systemctl status crond   //查看服务状态

systemctl stop crond //停止服务
systemctl start crond  //启动服务
systemctl restart crond  //重启服务
systemctl is-enabled crond   //检查服务是否开机启动

10.25 unit介绍

软链接文件指向的原文件是 /usr/lib/systemd/system/crond.service ,

使用命令ls  /usr/lib/systemd/system/ 

这个路径下有很多类型的文件和目录,这些文件都叫 unit 。
主要有以下几种类型:
service 系统服务
target 多个unit组成的组
device 硬件设备
mount 文件系统挂载点
automount 自动挂载点
path 文件或路径
scope 不是由systemd启动的外部进程
slice 进程组
snapshot systemd 快照
socket 进程间通信套接字
swap swap文件
timer 定时器
接着进入 /usr/lib/systemd/system/ 目录下,查看 runlevel,见下图,

entOS7这边也有7个运行级别,分别是0-6的target软链接文件,指向的是后面的路径。这7个运行级别,与CenOS6及之前的版本,基本上都可以对应起来。
接着来看一下,unit的几个相关命令,

systemctl list-units   //查看运行的unit

systemctl list-unit --all  //查看所有的unit

systemctl list-units --all --state=inactive   //指定状态查看unit

systemctl list-units --type=service   //列出所有正在运行的、类型为 service 的 Unit 

 systemctl list-units --all --type=service   //列出所有类型为service的unit

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

10.27 target介绍

 systemctl list-unit-files --type=target    //列出系统里的所有target

.

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

systemctl get-default  // 查看默认的target

更改 target 类型,可以更改开机的启动级别,类似CentOS6及以前版本的更改配置文件来实现更改运行级别。 

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

上图中,文件 /etc/systemd/system/default.target 就是一个软链接文件
unit 和 service 的关系如下:
一个 service 属于一种类型的 unit
多个 unit 组成了一个 target
一个 target 里面包含了多个 service

总结:

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

猜你喜欢

转载自my.oschina.net/u/3803395/blog/1809199