博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ubuntu下定时任务的执行
阅读量:7210 次
发布时间:2019-06-29

本文共 1897 字,大约阅读时间需要 6 分钟。

概述

linux系统由 cron (crond) 这个系统服务来控制例行性计划任务。Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的。

另外, 由于使用者自己也可以设置计划任务,所以, Linux 系统也提供了使用者控制计划任务的命令 :crontab 命令。

Linux下的任务调度分为两类,系统任务调度和用户任务调度。

系统任务调度:系统周期性所要执行的工作,比如写缓存数据到硬盘、日志清理等。在/etc目录下有一个crontab文件,这个就是系统任务调度的配置文件。

使用者权限文件:

文件:

/etc/cron.deny 说明:该文件中所列用户不允许使用crontab命令

文件:

/etc/cron.allow 说明:该文件中所列用户允许使用crontab命令

文件:

/var/spool/cron/ 说明:所有用户crontab文件存放的目录,以用户名命名

 

crontab文件的含义:

用户所建立的crontab文件中,每一行都代表一项任务,每行的每个字段代表一项设置,它的格式共分为六个字段,前五段是时间设定段,第六段是要执行的命令段,格式如下:

minute   hour   day   month   week   command

在以上各个字段中,还可以使用以下特殊字符:

星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。

逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”

中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”

正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。

 

ubuntu下crontab命令的使用

  1.首先编辑crontab文件。

crontab -e #打开你的用户所属的crontab文件。第一次用这个命令,会让你选择文本编辑器

             以后可以通过命令更改编辑器

select-editor

 原始的crontab文件如下: 

# Edit this file to introduce tasks to be run by cron.# # Each task to run has to be defined through a single line# indicating with different fields when the task will be run# and what command to run for the task# # To define the time you can provide concrete values for# minute (m), hour (h), day of month (dom), month (mon),# and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system# daemon's notion of time and timezones.# # Output of the crontab jobs (including errors) is sent through# email to the user the crontab file belongs to (unless redirected).# # For example, you can run a backup of all your user accounts# at 5 a.m every week with:# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/# # For more information see the manual pages of crontab(5) and cron(8)# # m h  dom mon dow   command* * * * * date >> /opt/time.txt #每一分钟执行一次

保存crontab,重启cron来应用这个计划任务。使用以下命令:

sudo service cron restart

效果图如下:

 

转载地址:http://drrum.baihongyu.com/

你可能感兴趣的文章
.NET平台开源项目速览(9)软件序列号生成组件SoftwareProtector介绍与使用
查看>>
hadoop2.X使用手册1:通过web端口查看主节点、slave1节点及集群运行状态
查看>>
LAN公布java web项目的方法的外侧
查看>>
Android中目的地Intent的使用
查看>>
结构变量输入不正确的顺序可能会导致不正确的操作结果
查看>>
IE浏览器兼容性问题解决方法
查看>>
四、Linux/UNIX操作命令积累【chmod、chown、tail】
查看>>
使用相机闪光灯开启
查看>>
机器学习中的数学(3)-模型组合(Model Combining)之Boosting与Gradient Boosting
查看>>
Java实现文件的加密与解密
查看>>
在开发过程中调试报表插件详细教程
查看>>
Android瀑布流照片墙实现,体验不规则排列的美感
查看>>
重点关注之OData with List
查看>>
Action的动态调用方法
查看>>
[CareerCup] 5.3 Next Binary Representation 下一个二进制表达
查看>>
jQuery整理笔记2----jQuery选择整理
查看>>
C语言中使用结构体
查看>>
Memcache功能具体解释
查看>>
ADB shell出现error:device offline提示
查看>>
hdu 1575 Tr A(矩阵高速电源输入)
查看>>