linux 简单添加定时任务
要使用的主要命令
- crontab -l 查看是否有定时任务
- crontab -e 编辑(添加)定时任务
- crontab -r 删除定时任务
- cp 复制文件
- vim 添加或编辑文件
- crontab crontab 创建定时任务
一 添加或编辑定时任务文件
[root@instance-nretneiu ~]# - cp /etc/crontab /home 复制系统定时任务文件到你的家目录
[root@instance-nretneiu ~]# cd /home 进行家目录
[root@instance-nretneiu home]# ls -l 查看目录下的文件或文件夹
total 8
-rw-r--r-- 1 root root 451 Jan 20 11:16 crontab 这个是刚复制的crontab文件
[root@instance-nretneiu home]#vim crontab 编辑定时任务文件
/**下边是crontab中的文件内容**/
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
# 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
# | | | | |
# 以下的每分钟往对应的日志文件中写入当前日期,进行测试
*/1 * * * * echo $(date) >> /home/log.log
vim 文件名 可编辑或添加文件,进入文件后i键可以进行对文件的编写。编写完成后按Esc 再按shift+:输入wq可以进行保存
二 创建定时任务
- 第一步只是添加了一个定时任务文件,添加完成后该任务不会执行
- 需要使用crontab crontab 命令来创建该任务
- 注意需要在你的crontab文件的目录中执行此命令
[root@instance-nretneiu home]# crontab crontab
[root@instance-nretneiu home]# crontab -l
HELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
# 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
# | | | | |
*/1 * * * * echo $(date) >> /home/log.log
[root@instance-nretneiu home]#
三 查看该任务结果
- 查看日志文件是否每分钟都有当前日期写入文件中
- 如果有则代定时任务没问题
[root@instance-nretneiu home]# cat /home/log.log
Mon Jan 20 11:55:01 CST 2020
Mon Jan 20 11:56:01 CST 2020
Mon Jan 20 11:57:01 CST 2020
Mon Jan 20 11:58:01 CST 2020
Mon Jan 20 11:59:01 CST 2020
[root@instance-nretneiu home]#
四 使用crontab -e 编辑定时任务文件
- 为什么开始创建定时任务文件时是复制系统的crontab文件
- 因为考虑到直接crontab -e来生成,文件所有内容需要手动编写,内容格式错误的几率较大
- 此时使用crontab -e 接等同于 vim crontab
[root@instance-nretneiu home]# crontab -e
HELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
# 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
# | | | | |
*/1 * * * * echo $(date) >> /home/log.log
五 删除定时任务
- 使用 crontab -r删除定时任务
[root@instance-nretneiu home]# crontab -l 查看定时任务
HELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
# 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
# | | | | |
*/1 * * * * echo $(date) >> /home/log.log
[root@instance-nretneiu home]# crontab -r 删除定时任务
[root@instance-nretneiu home]# crontab -l 再查看发现定时任务呗删除了
no crontab for root
[root@instance-nretneiu home]#
本人也是刚学习中linux菜鸟,以上都是自己亲自测试过了的,如果你按照以上的流程出现问题请勿喷,可能是系统差别的问题。