linux 定时任务 crond 服务介绍
linux定时任务crond服务应用指南
Linux的定时任务crond(crontab)服务
1、crond是什么?
crond是linux系统中用来定期执行命令或者指定程序的程序(软件)
2、秒级任务
(1)crond需要方式实现
(2)自己写守护进程shell循环
(3)Quartz也可实现妙计任务
3、定时任务的作用
服务器:7*24小时开机提供服务是网站的基本特征
有重要数据:提高备份周期和备份数量
Linux的定时任务分类
linux系统中定时任务调度的工作可以分为一下两种
(1)系统自身的定期执行的任务
系统周期性自行执行的任务工作。如轮询系统日志、备份系统数据、清理系统缓存等
[root@ysolin ~]# ls -l /var/log/messages* 系统的日志
-rw-------. 1 root root 201123 6月 28 14:44 /var/log/messages
-rw-------. 1 root root 200796 6月 26 03:49 /var/log/messages-20160626
系统自动轮询任务的设置配置路径:
[root@ysolin ~]# ls -l /etc/ |grep cron
-rw-------. 1 root root 541 11月 23 2013 anacrontab
drwxr-xr-x. 2 root root 4096 6月 24 18:13 cron.d
drwxr-xr-x. 2 root root 4096 9月 27 2011 cron.daily
-rw-------. 1 root root 0 11月 23 2013 cron.deny
drwxr-xr-x. 2 root root 4096 9月 27 2011 cron.hourly
drwxr-xr-x. 2 root root 4096 9月 27 2011 cron.monthly
-rw-r--r--. 1 root root 457 9月 27 2011 crontab
drwxr-xr-x. 2 root root 4096 9月 27 2011 cron.weekly
(2)用户执行的定时任务
某个用户或系统管理员定期要做的任务工作,用户执行放入任务工作,也就是我们运维管理人员执行的任务工作。
例如:服务器时间同步
[root@ysolin ~]# crontab -l #查看定时任务
time sync by solin
*/5 * * * * /sbin/ntpdate time.windows.com >/dev/null 2>&1
2、Linux系统下定时任务软件种类
(1)at适合执行一次就结束的调度任务。依赖服务atd
(2)anacron适合于非7*24小时开机的服务准备。anacron并不能指定具体时间执行任务工作,而是以天为周期或者在系统每次开机后执行的任务工作,它会检测服务器停机期间应该执行,但没有执行的任务工作,并将该任务执行一遍。
(3)crond,crond服务默认情况(每分钟),会检查系统中是否有需要执行的定时任务,若有会根据定义好的规则来执行这个定时任务。
注:
(1)crond服务是运行的程序,而crontab命令用户用来设置定时规则的命令
(2)crond服务是企业生产工作中常用的重要服务,at和anacron很少使用,可以忽略。
定时任务crond使用说明
1、语法指令
crontab [-u user] {-l | -e | -r |-i}
查看帮助
[root@ysolin ~]# crontab --help
crontab: invalid option -- '-'
crontab: usage error: unrecognized option
usage: crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]
(default operation is replace, per 1003.2)
-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting user's crontab)
-s (selinux context)
2、指令说明
通过crontab我们可以在固定的间隔时间执行指定的系统指令或script脚本。时间间隔的单位是分、时、日、月、周及以上的任意组合。
3、使用者权限文件
文件 说明
/etc/cron.deny #该文件中所列用户不允许使用crontab命令
/etc/cron.allow #该文件中所列用户允许使用crontab命令,优先于/etc/cron.deny
/etc/spool/cron/ #所有用户crontab配置文件默认都存在此目录,文件名以用户名命令
注:
crontab -e = vi /var/spool/cron/root
crontab -l = cat /var/spool/cron/root
4、指令选项说明含义表
-e (edit user's crontab) #编辑crontab文件内容
-l (list user's crontab) #查看crontab文件内容
-r (delete user's crontab) #删除crontab文件内容
-i (prompt before deleting user's crontab) #删除crontab文件内容,删除前会提示。
-u #指定使用的用户执行任务
5、指令的使用格式
(1)crond语法格式中时间段的含义
[root@ysolin ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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
(2)crontab语法格式中特殊符号含义
* # *号,表示任意时间,如每分、每小时、每日、每月、每周
- # 减号,表示分隔符,表示一个范围区间
, # 逗号,表示分割时间段的意思。
/n # n代表数字,即“每隔n单位时间”
(3)crontab依赖服务
[root@ysolin ~]# chkconfig --list crond
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@ysolin ~]# /etc/init.d/crond status
crond (pid 1430) is running...
[root@ysolin ~]# ps -ef |grep crond | grep -v grep
root 1430 1 0 Jun28 ? 00:00:00 crond
[root@ysolin ~]# /etc/init.d/crond restart
Stopping crond: [ OK ]
Starting crond: [ OK ]
定时任务crond实例说明
范例1:每五分钟同步一次系统时间
[root@ysolin ~]# vi /var/spool/cron/root
[root@ysolin ~]# echo 'time sync by solin' >>/var/spool/cron/root
[root@ysolin ~]# echo '*/5 * * * * /sbin/ntpdate time.windows.com >/dev/null 2>&1' >>/var/spool/cron/root
[root@ysolin ~]# crontab -l
time sync by solin
*/5 * * * * /sbin/ntpdate time.windows.com >/dev/null 2>&1
范例2:每个月1、10、20日的凌晨3:30分重启apache
30 3 1,10,20 * * /application/apache/bin/apachectl graceful
生产环境crond专业实例
1、规范定时任务两例(练习)
(1)每分钟打印一次solil到/var/log/solin.log中
[root@ysolin ~]# crontab -e
添加任务
#print my name to log
* * * * * echo solil >>/var/log/solin.log
[root@ysolin ~]# crontab -l
#time sync by solin
*/5 * * * * /sbin/ntpdate time.windows.com >/dev/null 2>&1
#print my name to log
* * * * * echo solil >>/var/log/solin.log
[root@ysolin ~]# cat /var/log/solin.log
solil
solil
solil
(2)周期任务使用脚本每周六、日上午九点和下午两点(周期)打印时间(格式:****-**-**)
创建脚本
[root@ysolin ~]# date +%F
2016-06-29
[root@ysolin ~]# echo 'date +%F' >scripts/date.sh
[root@ysolin ~]# cat scripts/date.sh
date +%F
[root@ysolin ~]# sh scripts/date.sh
2016-06-29
设置定时任务
[root@ysolin ~]# echo "#print date " >>/var/spool/cron/root
[root@ysolin ~]# echo "00 09,14 * * 6,7 /bin/sh /root/scripts/date.sh >/dev/null 2>&1" >>/var/spool/cron/root
[root@ysolin ~]# crontab -l |tail -2
#print date
00 09,14 * * 6,7 /bin/sh /root/scripts/date.sh >/dev/null 2>&1
写定时任务规则时尽可能的加上注释(最好是英文英文注释),方便后期更高改,其他运维人员理解定时任务的内容
执行定时任务时,如果是执行脚本,请尽量在脚本后面带上/bin.sh命令,防止脚本没有执行权限而出错
(3)定时任务命令或脚本结尾加>/dev/null 2>&1
2>&1表示让标准错误和标准输出一样,即把脚本正常和错误输出都都重定向到/dev/null,不记录任何输出。
>/dev/null 2>&1等价于1>/dev/null 2>/dev/null 等价于 &>dev/null
如果定时任务规则结尾不加>/dev/null 2>&1,很容易导致inode磁盘空间被占满,从而系统服务不正常。
在开发定时任务程序或脚本时,在调试好脚本程序后,应尽量把DEBUG及命令输出的内容信息屏蔽掉,如果确实需要输出日志,可定向指定日志文件,避免产生系统垃圾。
(7)定时任务执行的脚本要规范路径/server/scripts
首先要在命令行操作成功,然后复制成功的命令到脚本里,减少配置定时任务错误;
[root@ysolin ~]# ll /var/log/messages* #系统报错日志
-rw-------. 1 root root 201538 6月 29 19:48 /var/log/messages
-rw-------. 1 root root 200796 6月 26 03:49 /var/log/messages-20160626
[root@ysolin ~]# ll /var/log/secure* #安全信息和系统登录与网络连接的信息
-rw-------. 1 root root 8562 6月 29 23:43 /var/log/secure
-rw-------. 1 root root 6490 6月 26 01:45 /var/log/secure-20160626
[root@ysolin ~]# ls /etc/cron.daily/
[root@ysolin ~]# cat /etc/cron.daily/logrotate # logrotate 程序是一个日志文件管理工具
/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
[root@ysolin ~]# cat /etc/logrotate.conf # logrotate 的配置文件
# see "man logrotate" for details
# keep 4 weeks worth of backlogs
# create new (empty) log files after rotating old ones
# use date as a suffix of the rotated file
# uncomment this if you want your log files compressed
# RPM packages drop log rotation information into this directory
# no packages own wtmp and btmp -- we'll rotate them here
# system-specific logs may be also be configured here.
个人的开发配置环境è办公室的测试环境èIDC机房的测试环境èIDC机房的正式环境
把系统的时间调整为定时任务执行的前五分钟,确保定时任务能够正确执行。
在脚本中加入日志输出,然后把输出打到指定的日志中,然后观察日志内容结果,看是否执行或正确执行。
如:*/1 * * * * * echo “Hello Word” >> /tmp/solin.log >/dev/null 2>&1
因前面已经有了重定向,后面要去掉/dev/null 2>&1
在调试java程序的时候,注意环境变量,把环境变量的定义加到脚本里
[root@ysolin ~]# tail -f /var/log/cron
Jun 30 02:00:01 ysolin CROND[3537]: (root) CMD (/sbin/ntpdate time.windows.com >/dev/null 2>&1)
Jun 30 02:01:01 ysolin CROND[3541]: (root) CMD (run-parts /etc/cron.hourly)
Jun 30 02:01:01 ysolin run-parts(/etc/cron.hourly)[3541]: starting 0anacron
Jun 30 02:01:01 ysolin anacron[3550]: Anacron started on 2016-06-30
Jun 30 02:01:01 ysolin anacron[3550]: Jobs will be executed sequentially
Jun 30 02:01:01 ysolin anacron[3550]: Normal exit (0 jobs run)
Jun 30 02:01:01 ysolin run-parts(/etc/cron.hourly)[3552]: finished 0anacron
Jun 30 02:05:01 ysolin CROND[3555]: (root) CMD (/sbin/ntpdate time.windows.com >/dev/null 2>&1)
Jun 30 02:10:01 ysolin CROND[3559]: (root) CMD (/sbin/ntpdate time.windows.com >/dev/null 2>&1)
Jun 30 02:15:01 ysolin CROND[3563]: (root) CMD (/sbin/ntpdate time.windows.com >/dev/null 2>&1)
crond定时任务生产应用问题10箴言
5、>/dev/null 2>&1,防止磁盘空间(inode)被占满
crond生产环境案例及解决过程
1、No space left on davice生产环境故障
最后检查发现/var/spool/clientmqueue/下有大量的文件,执行cd /var/spool/clientmqueue ;rm -f * 自动跳出,无法删除
[root@ysolin ~]# cd /var/spool/clientmqueue && ls |xargs rm -f
[root@ysolin ~]# cd /var/spool && rm -rf clientmqueue
2、No space left on davice生产环境故障
inode译成中文就是索引节点,每个存储设备(例如硬盘)或存储设备的分区被格式化为文件系统后,应该有两部份,一部份是inode,另一部份是Block,Block是用来存储数据用的。
(1)find锁定占用空间最大的目录(通常目录大小越大,表示目录下的文件越多)
(2)删除多余的inode节点。(find /xxx -type f -exec rm {} \;)
find / -type f -size -1k | xargs -i dirname {} | sort -nr| uniq -c | sort -nr | tail
删除:(一定要确定是自己的站点,而且所删除的东西已经不重要)
转载于:https://blog.51cto.com/chengxiaobai/1860633
linux 定时任务 crond 服务介绍相关推荐
- linux未被识别crond服务,linux 定时任务 crond 服务介绍
linux定时任务crond服务应用指南 Linux的定时任务crond(crontab)服务 1.crond是什么? crond是linux系统中用来定期执行命令或者指定程序的程序(软件) 2.秒级 ...
- linux查看定时服务,linux 定时任务 crond 服务介绍
linux定时任务crond服务应用指南 Linux的定时任务crond(crontab)服务 1.crond是什么? crond是linux系统中用来定期执行命令或者指定程序的程序(软件) 2.秒级 ...
- Linux服务项目实例,linux 定时任务 crond 服务应用指南(项目实例)
linux定时任务crond服务应用指南 Linux的定时任务crond(crontab)服务 1.crond是什么? crond是linux系统中用来定期执行命令或者指定程序的程序(软件) 2.秒级 ...
- Linux定时任务crond服务
一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...
- linux定时任务Crond生产规范案例精讲09
linux定时任务Crond生产规范案例精讲09说明:本文来自老男孩linux运维实战培训-就业课程视频内容,本文内容为系列内容,更多分享信息见:http://oldboy.blog.51cto.co ...
- Linux系统Logrotate服务介绍
Linux系统Logrotate服务介绍 本文转自 民工哥 51CTO博客,原文链接:http://blog.51cto.com/mingongge/1957688
- linux定时任务打印日志,Centos7(linux)使用crond服务定时执行js脚本并将脚本的输出内容记录日志文件的简单应用...
- ## 在Centos7.8上使用crond服务定时执行js脚本并将脚本执行的输出记录日志 1. 安装wget 2. wget https://npm.taobao.org/mirrors/node ...
- linux定时任务crond那些事!
来自老男孩教育的运维班面授课程内容,17期依良同学作业整理而得! 1.定时任务crond介绍 1.1 crond是什么 crond是linux系统中用来定期执行命令或指定程序任务的一种服务或软件. 特 ...
- linux 定时任务crond
1.定时任务crond 1.1 crond是什么 crond是linux系统中用来定期执行命令或指定程序任务的一种服务或软件.特殊需求:(秒级别)crond服务就无法搞定了,一般工作中写脚本守护进程执 ...
最新文章
- CentOs7安装tomcat
- 来自星星的你,我要代表月亮消灭你一
- Java IO (二),常见的输入/输出流
- centos7 安装配置mesos+marathon+zookeeper
- php短信接口源码,比较简单,但也实用
- Delphi7的控件安装与删除
- [转载] Python列表排序 list.sort方法和内置函数sorted
- 架构专家高磊:缓存为王——无线缓存架构优化
- 北京最值得逛的50个地方:后海、798、簋街...
- 银河麒麟-麒麟软件操作系统 kylin 搭建yum源 ubuntu搭建yum源 yum软件仓库搭建ftp源、python源、硬盘源
- 大数据时代的图表可视化利器——highcharts,D3和百度的echarts
- VBOX下安装RHEL5.4增强工具失败的解决办法
- 自定义添加文字的电脑桌面_将可自定义的免费应用程序启动器添加到Windows桌面...
- 街机模拟器 WinKawaks 及街机 ROM 下载
- 联通猫无线灯闪 不能连接服务器,猫如何连接路由器,光猫光信号一直闪红灯
- 结束任务管理器电脑黑屏怎么办
- Java 过滤器Url通配符处理
- 在eclipse上运行html文件
- 【ICC】icc基本使用步骤(转载)(初稿)
- OCS认证有机含量标准