OS:
Linux
CentOS 7
----------------------------------------------------
#查看本用户下所有的定时任务
crontab -l
#编辑本用户下的定时任务
crontab -e
#编辑系统定时任务
vi /etc/crontab
例如每天早上9点定时运行一个python的脚本,并将这个脚本运行的错误输出和标准输出,输入到aabb.log里面
0 9 * * * root /usr/bin/python3 aabb.py >> /root/aabb.log 2>&1
关于mycommand >mylog.txt 2>&1 的解释:命令的结果可以通过“%>”的形式来定向输出,%表示文件描述符:1为标准输出stdout、2为标准错误stderr。系统默认%值是1,也就是“1>”,而1>可以简写为>,也就是默认为>。stdout的默认目标是终端,stderr的默认目标为也是终端。
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
0 9 * * * root /usr/bin/python3 aabb.py >> /root/aabb.py.log 2>&1
#一般写完之后需要立刻生效的话重启一下cron,像这种daily的我一般不重启
service cron restart
#查看log
如果没有这个需要开启。这个log很简单就是定时任务几点几分运行了,运行的状态结果是啥。
tail -n 100 /var/log/cron
#停用任务.
直接直接删掉或者注释掉第二天就不会运行了。如果这个脚本正在运行还需要杀掉进程
ps aux | grep aabb.py
kill -9