ubuntu定时执行python脚本方法及实例代码
一、前言
?
本文將介紹ubuntu系統下如何定時執行shell腳本、python腳本,ubuntu系統有一個定時任務的管理器crontab,我們只需要編輯定時任務,然后重啟定時任務服務就好了。
?
二、工具:crontab
?
a、編輯定時任務:
crontab -e?
b、參數定義:
- -u 指定用戶,
- -l 列出用戶任務計劃,
- -r 刪除用戶任務,
- -e 編輯用戶任務
?
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
以上為縮寫,這里提供全拼對照:
minute (m),? hour (h), day of month (dom), month (mon), day of week (dow)
含義如下:
- m 每個小時的第幾分鐘執行該任務
- h 每天的第幾個小時執行該任務
- dom 每月的第幾天執行該任務
- mon 每年的第幾個月執行該任務
- dow 每周的第幾天執行該任務 - command 指定要執行的程序
分????? 小時??? 日????? 月?????? 星期???? 命令
0-59?? 0-23?? 1-31?? 1-12???? 0-6???? command
其他:
- 其中星期中0表示周日。
- * 代表任何時間,比如第一個分鐘,用 * 就代表每一小時的每一分鐘都執行
- - 表示區間,比如1-3
- , 如果區間不連續,可以用,例如1,3,6 編輯完成后wq 保存退出
記住幾個特殊符號的含義:
- ?"*"代表取值范圍內的數字,
- ??"/"代表"每",
- ??"-"代表從某個數字到某個數字,
- ??","分開幾個離散的數字
?
三、方法使用:
?
1、簡易方法:
a、創建腳本文件test.py,在文件開頭需要加上下面一行
#!/home/qq/anaconda3/bin/python? ?上面這行的作用是說明使用那個解釋器來執行該文件,如果不知道python解釋器在哪,可以使用命令which python來查看
b、給該文件添加可執行的權限
chmod ?+x ?test.pyc、添加新一行
? ? 輸入命令,修改配置
crontab -e格式為:分 時 日 月 星期幾 [命令]
*號表示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注意,一定要用絕對路徑。否則可能會執行失敗。
這個編輯器比較神奇,ctrl+x離開,會提示是否保存,按y確定即可。
?
?
離開后,
crontab -l查看是否已寫入命令。
?
2、更本質的方法 vim /etc/crontab
這個方法的神奇之處在于,你甚至可以設置執行該命令的user。
如下文我使用qq來執行,也可以用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注意,一定要用絕對路徑。否則可能會執行失敗。
然后使用: wq! 保存退出。
?
四、重啟cron服務
sudo service cron restart?
五、查看建立成功的定時任務:?
crontab -l???可以看到當前用戶下的定時任務
?
六、刪除定時任務:?
crontab -r??回車,再次在命令行輸入:?
crontab -l?,提示:“no crontab for admin”
總結
以上是生活随笔為你收集整理的ubuntu定时执行python脚本方法及实例代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 总结python处理图片等比例压缩与质量
- 下一篇: python设计模式之享元模式