配置周期性计划任务

一、定义:

  定时执行任务 

二、格式

1. 使用 student,检查自己的周期性计划任务,现在是空的 

[student@desktop1 ~]$ crontab -l
no crontab for student

2. 使用crontab-e,编辑student自己的周期进化任务 ,每分钟向/home/student/cron 中追加 date 命令的输出结果

[student@desktop1 ~]$ crontab -e

  * * * * * echo $(date) >> /home/student/cron.txt


no crontab for student - using an empty one
crontab: installing new crontab

3. 用 crontab -l 确认计划任务的配置

[student@desktop1 ~]$ crontab -l
* * * * * echo $(date) >> /home/student/cron.txt

4. 用 tail -f 命令,观察/home/student/cron.txt,确认计划任务是正常工作的 

[student@desktop1 ~]$ tail -f /home/student/cron.txt 
Thu Nov 3 16:36:01 CST 2016
Thu Nov 3 16:37:01 CST 2016
Thu Nov 3 16:38:01 CST 2016
Thu Nov 3 16:39:01 CST 2016
...

5. 用 crontab -e,编辑 student 自己的计划任务,每 2 分钟,向/home/student/cron.txt 追加 hello 字符串

[student@desktop1 ~]$ crontab -e
*/2 * * * * echo hello >> /home/student/cron.txt

6. student 用户,将 crontab -l 的输出结果重定向到/homt/student/mycrontab.cfg 文 件中

[student@desktop1 ~]$ crontab -l >> ~/mycrontab.cfg

7.student 删除自己的所有周期性计划任务 

[student@desktop1 ~]$ crontab -r
[student@desktop1 ~]$ crontab -l
no crontab for student
[student@desktop1 ~]$ 

8. student 用户用~/mycrontab.cfg 文件中的配置信息重新创建自己的计划任务 

  [student@desktop1 ~]$ crontab ~/mycrontab.cfg
  [student@desktop1 ~]$ crontab -l
  * * * * * echo $(date) >> /home/student/cron.txt
  */2 * * * * echo hello >> /home/student/cron.txt

9. 切换到 root 用户,观察/var/spool/cron/student,会看到 student 的计划任务 

[root@desktop1 Desktop]# cat /var/spool/cron/student 
* * * * * echo $(date) >> /home/student/cron.txt
*/2 * * * * echo hello >> /home/student/cron.txt

10. root 用户编辑自己的计划任务,每月 1 日、11 日、21 日、31 日或者星期日,每分钟向 /root/cron.txt 追加一次 date 命令的输出结果

[root@desktop1 Desktop]# crontab -e

  * * 1,11,21,31 * 0 date >> /root/cron.txt 


no crontab for root - using an empty one
crontab: installing new crontab

11. 观察/var/spool/cron/root ,看到 root 用户的计划任务 

[root@desktop1 Desktop]# cat /var/spool/cron/root 
* * 1,11,21,31 * 0 date >> /root/cron.txt

12. 用root用户删除student用户的所有周期性计划任务,并确认 

[root@desktop1 Desktop]# crontab -r -u student
[root@desktop1 Desktop]# crontab -l -u student
no crontab for student
[root@desktop1 Desktop]# 

13. root 用户编辑/etc/crontab 创建系统计划任务,每天 23:59,以 root 用户身份清空 /tmp目录,

另外每天晚上八点,以student身份,更新/home/student/time这个文件的 时间戳

[root@desktop1 Desktop]# vim /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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

59 23 * * * root rm -rf /tmp
* 20 * * * student touch /home/student/time

 

转载于:https://www.cnblogs.com/tdcqma/p/6026573.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值