yuanqiao 发表于 2015-12-3 11:29:44

linux定时执行python脚本

  每天清晨 4:00:01,用python执行/opt/aa.py文件。
  编辑定时任务:
  #crontab -e
  加入:
  0 4 * * * python /opt/aa.py
  保存,退出即可。
  如果执行的文件在当前目录的二级目录下,则需要先cd进入相应目录,再python:
  */5 * * * * cd /home/iris/; python aa.py

crontab命令格式:
  * * * * * command M H D m d command
  M: 分(0-59) H:时(0-23) D:天(1-31) m: 月(1-12) d: 周(0-6) 0为星期日
  * 代表取值范围内的数字 / 代表"每" - 代表从某个数字到某个数字 , 代表离散的取值(取值的列表)
  示例:
  编辑python文件:



ccc="tttt.txt"
f.open(ccc,'a')
f.write("hello!")
f.close
  保存为hello.py并退出。



crontab -e
*/1 * * * * python /home/zengzichun/hello.py
  保存并退出。不久即可看到目录下多了tttt.txt文件,打开可看到"hello!hello!......" 。
  
  参考博客:http://blog.csdn.net/chenggong2dm/article/details/12649053
页: [1]
查看完整版本: linux定时执行python脚本