ubuntu定时执行python脚本方法及实例代码
一、前言
?
本文將介紹ubuntu系統(tǒng)下如何定時(shí)執(zhí)行shell腳本、python腳本,ubuntu系統(tǒng)有一個(gè)定時(shí)任務(wù)的管理器crontab,我們只需要編輯定時(shí)任務(wù),然后重啟定時(shí)任務(wù)服務(wù)就好了。
?
二、工具:crontab
?
a、編輯定時(shí)任務(wù):
crontab -e?
b、參數(shù)定義:
- -u 指定用戶,
- -l 列出用戶任務(wù)計(jì)劃,
- -r 刪除用戶任務(wù),
- -e 編輯用戶任務(wù)
?
c、英文介紹:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
d、中文解釋:
格式:
m h dom mon dow command
以上為縮寫,這里提供全拼對(duì)照:
minute (m),? hour (h), day of month (dom), month (mon), day of week (dow)
含義如下:
- m 每個(gè)小時(shí)的第幾分鐘執(zhí)行該任務(wù)
- h 每天的第幾個(gè)小時(shí)執(zhí)行該任務(wù)
- dom 每月的第幾天執(zhí)行該任務(wù)
- mon 每年的第幾個(gè)月執(zhí)行該任務(wù)
- dow 每周的第幾天執(zhí)行該任務(wù) - command 指定要執(zhí)行的程序
分????? 小時(shí)??? 日????? 月?????? 星期???? 命令
0-59?? 0-23?? 1-31?? 1-12???? 0-6???? command
其他:
- 其中星期中0表示周日。
- * 代表任何時(shí)間,比如第一個(gè)分鐘,用 * 就代表每一小時(shí)的每一分鐘都執(zhí)行
- - 表示區(qū)間,比如1-3
- , 如果區(qū)間不連續(xù),可以用,例如1,3,6 編輯完成后wq 保存退出
記住幾個(gè)特殊符號(hào)的含義:
- ?"*"代表取值范圍內(nèi)的數(shù)字,
- ??"/"代表"每",
- ??"-"代表從某個(gè)數(shù)字到某個(gè)數(shù)字,
- ??","分開幾個(gè)離散的數(shù)字
?
三、方法使用:
?
1、簡(jiǎn)易方法:
a、創(chuàng)建腳本文件test.py,在文件開頭需要加上下面一行
#!/home/qq/anaconda3/bin/python? ?上面這行的作用是說(shuō)明使用那個(gè)解釋器來(lái)執(zhí)行該文件,如果不知道python解釋器在哪,可以使用命令which python來(lái)查看
b、給該文件添加可執(zhí)行的權(quán)限
chmod ?+x ?test.pyc、添加新一行
? ? 輸入命令,修改配置
crontab -e格式為:分 時(shí) 日 月 星期幾 [命令]
*號(hào)表示every
## Output of the crontab jobs (including errors) is sent through# email to the user the crontab file belongs to (unless redirected).## For example, you can run a backup of all your user accounts# at 5 a.m every week with:# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/## For more information see the manual pages of crontab(5) and cron(8)## m h dom mon dow command59 23 * * * /home/qq/anaconda3/bin/python /home/qq/test.py注意,一定要用絕對(duì)路徑。否則可能會(huì)執(zhí)行失敗。
這個(gè)編輯器比較神奇,ctrl+x離開,會(huì)提示是否保存,按y確定即可。
?
?
離開后,
crontab -l查看是否已寫入命令。
?
2、更本質(zhì)的方法 vim /etc/crontab
這個(gè)方法的神奇之處在于,你甚至可以設(shè)置執(zhí)行該命令的user。
如下文我使用qq來(lái)執(zhí)行,也可以用root之類的。
# /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/shPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin# m h dom mon dow user command59 23 * * * qq /home/qq/anaconda3/bin/python /home/qq/test.py注意,一定要用絕對(duì)路徑。否則可能會(huì)執(zhí)行失敗。
然后使用: wq! 保存退出。
?
四、重啟cron服務(wù)
sudo service cron restart?
五、查看建立成功的定時(shí)任務(wù):?
crontab -l???可以看到當(dāng)前用戶下的定時(shí)任務(wù)
?
六、刪除定時(shí)任務(wù):?
crontab -r??回車,再次在命令行輸入:?
crontab -l?,提示:“no crontab for admin”
總結(jié)
以上是生活随笔為你收集整理的ubuntu定时执行python脚本方法及实例代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 总结python处理图片等比例压缩与质量
- 下一篇: python设计模式之享元模式