文章目录
我用的是ubuntu 20.04.
1.vi /etc/crontab
命令
这个命令只有root用户才可以使用,所以应该是sudo vi /etc/crontab
才够完整。输入命令,系统输出如下结果:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# 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
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
由于我并没有使用此命令,并且此命令使用方法和crontab -e
命令相似,所以不展开来讲解。
2.crontab -e
命令
这一命令是在当前用户下创建自动执行程序任务。
输入crontab -e
,在文件末尾追加你想要运行的命令(程序),就可以自动执行任务。命令的基本格式为:
m h dom mon dow command
分 时 天 月 周 命令
2.1频率设置
m
表示每小时的第几分钟执行command
h
表示每天的第几小时执行command
dom
表示每月小时的第几天执行command
mon
表示每年的第几个月执行command
dow
表示每周的第几天执行command
例如:
*/3 * * * *
表示每三分钟执行一次。
1,3,5 * * * *
表示每小时的第1,3,5分钟各执行一次。
1-5 * * * *
表示每小时的第1,2,3,4,5分钟各执行一次。
* 1,2 * * *
表示每天的凌晨一点和两点各执行一次。
2.2命令设置
例如:
*/1 * * * * /usr/bin/python /home/ubuntu/auto/auto_test.py >> /home/ubuntu/auto/auto_test.log 2>&1
这个命令的意思是每一分钟
通过/usr/bin/python
运行一次/home/ubuntu/auto/auto_test.py
程序,并把日志输出到/home/ubuntu/auto/auto_test.log
。
这样,程序里的print
就会输出到auto_test.log
里。这里的2>&1
表示把错误的输出也输出到标准输出(print),即平时运行出错时,出现在控制台里的错误提示也会被写到auto_test.log
里。
当然,>> /home/ubuntu/auto/auto_test.log 2>&1
这一段可以不写。
3.错误分析
像上面那样写的命令,当程序auto_test.py
例如为以下内容时,crontab可能会如愿地执行程序:
print("Hello,world!")
程序运行后,/home/ubuntu/auto/auto_test.log
里会有一行一行的Hello,world!
。
然而,你的程序却不一定能正常运行。
这里总结了如下原因:
3.1.python路径原因
你可能注意到,上面的例子中,我用了python的绝对路径/usr/bin/python
,这是因为,你的系统中可能会有多个版本的python,如果没有使用绝对路径,可能会使系统找不到你的python。这位博主就是一个例子。
另外,你可以使用which python
查看你的python路径。
3.2. 文件权限原因
假如你是在用户ubuntu
下设置的定时任务,如果ubuntu
对程序auto_test.py
没有执行权限,当然就不能定时执行程序了。可以用命令sudo chmod 777 /home/ubuntu/auto/auto_test.py
来增加权限。
3.3. 文件路径原因
比如auto_test.py
为以下内容:
with open('auto_test.txt', 'a+') as f:
f.write("Hello,world!\n")
f.close()
auto_test.txt
是和auto_test.py
同一目录下的文件即:/home/ubuntu/auto/auto_test.txt
,当程序执行时,系统并找不到这个文件。所以,你应该把'auto_test.txt'
替换为/home/ubuntu/auto/auto_test.txt
。
可是,可能你的项目里不止一个文件,里面有好多文件路径,为了移植方便,不需要频繁改变路径,还是用相对路径比较方便。所以,这里推荐使用.sh
脚本的方法。
例如,在目录/home/ubuntu/auto
下新建auto_test.sh
:
#! /bin/bash
cd /home/ubuntu/auto/
python auto_test.py
crontab的定时执行任务设置为:
*/1 * * * * /home/ubuntu/auto/auto_test.sh >> /home/ubuntu/auto/auto_test.log 2>&1
由于auto_test.sh
里cd
到了项目目录里,就不用写绝对路径了。所以推荐这种使用脚本的方法。
4.其他命令
# crontab 启动
sudo service cron start
# crontab 停止
sudo service cron stop
# crontab 重启
sudo service cron restart
# 列出crontab 任务列表
crontab -l