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

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

1、设置定时任务

cronta -e*/5  *  *  *  * echo "helloworld"#每五分钟执行一次#分时日月周

2、查看定时任务

crontab -l*/5  *  *  *  * echo "helloworld"

3、查看定时任务执行情况

tail -100f /var/log/cron | grep helloworldDec 10 11:05:01 localhost CROND[16619]: (root) CMD (echo "helloworld")Dec 10 11:10:01 localhost CROND[27467]: (root) CMD (echo "helloworld")Dec 10 11:15:01 localhost CROND[6146]: (root) CMD (echo "helloworld")

4、crontab 指定执行用户

linux下可以通过配置crontab来定时执行任务,执行体可以是一条系统命令或自己写的一个脚本,同时可以指派用户来执行。配置crontab有两种方法。

方法1、使用crontab命令,例如添加一个新的或编辑已有的,使用:
crontab -e

就可以进入配置文件。此时配置crontab的执行者是当前登入用户,如果当前用户是root,需要为其他用户配置,可以使用

crontab -e -u 用户名

su 用户名crontab -e

这种方法有一个缺点,就是当前系统中配置的crontab不在一个配置文件中,让管理员不方便查询系统到底有多少个crontab。

方法2、直接在/etc/crontab文件中添加,不过需要是root身份。打开文件,应该会看到类似下面的信息
SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/# 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

要添加新的crontab,只需要在文件最后增加即可。注意这里面需要指定用户名;而方法1中则不需要,如果指定了,它会认为是命令的一部分,从而可能导致crontab执行失败。

如果服务器都是有root来管理,建议添加crontab使用方法2,这样系统中的所有计划任务都在一起,一目了然。

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

你可能感兴趣的文章
div标签布局的使用
查看>>
HTML中表格的使用
查看>>
(模板 重要)Tarjan算法解决LCA问题(PAT 1151 LCA in a Binary Tree)
查看>>
(PAT 1154) Vertex Coloring (图的广度优先遍历)
查看>>
(PAT 1115) Counting Nodes in a BST (二叉查找树-统计指定层元素个数)
查看>>
(PAT 1143) Lowest Common Ancestor (二叉查找树的LCA)
查看>>
(PAT 1061) Dating (字符串处理)
查看>>
(PAT 1118) Birds in Forest (并查集)
查看>>
数据结构 拓扑排序
查看>>
(PAT 1040) Longest Symmetric String (DP-最长回文子串)
查看>>
(PAT 1145) Hashing - Average Search Time (哈希表冲突处理)
查看>>
(1129) Recommendation System 排序
查看>>
PAT1090 Highest Price in Supply Chain 树DFS
查看>>
(PAT 1096) Consecutive Factors (质因子分解)
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>