书接上文,python脚本准备好了,但是需要定时执行
开始研究crontab的使用,总结下需求和实现
1、先讲下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
格式说明
基本命令
crontab [-u username] //省略用户表表示操作当前用户的crontab
-e (编辑工作表)
-l (列出工作表里的命令)
-r (删除工作作)
重点来了
需求场景1:每小时的第1分钟执行一次脚本,该如何配置呢?
crontab -e
01 * * * * python /home/scan/scan.py #输入此行
说明:
01代表,每小时的第1分钟执行,查看日志可以得出
------------------------------------------------------
2020-09-09 13:01:01,710 scan.py[line:271] INFO 扫描开始
2020-09-09 13:01:21,712 scan.py[line:275] INFO 扫描完成
2020-09-09 14:01:02,197 scan.py[line:271] INFO 扫描开始
2020-09-09 14:01:21,713 scan.py[line:275] INFO 扫描完成
2020-09-09 15:01:02,334 scan.py[line:271] INFO 扫描开始
2020-09-09 15:01:23,115 scan.py[line:275] INFO 扫描完成
2020-09-09 16:01:01,630 scan.py[line:271] INFO 扫描开始
2020-09-09 16:01:24,070 scan.py[line:275] INFO 扫描完成
2020-09-09 16:12:01,482 scan.py[line:271] INFO 扫描开始
2020-09-09 16:12:24,539 scan.py[line:275] INFO 扫描完成
需求场景1:每分钟执行一次脚本,该如何配置呢?
crontab -e
* * * * * python /home/scan/scan.py #输入此行
说明:
第1位的* 代表每分钟执行
其他的依次类推