美文网首页
Crontab定时运行python程序

Crontab定时运行python程序

作者: 逍遥_yjz | 来源:发表于2021-05-13 16:00 被阅读0次

    centos Crontab基本入门
    首先获悉crontab的使用方法后,并使用其定时运行Python程序

    1. 编辑定时命令

    每分钟,运行一次程序

    * * * * * /usr/local/python3.6/bin/python3  /home/dingshi/dingshi.py >>  /home/dingshi/crontest.py.log 2>&1
    
    • dingshi.py文件内容
      测试功能:读取文件test.txt,并生成一个文件write.txt。
      将打印的数据存储到crontest.py.log
    # conding:utf-8
    import datetime
    now_time  = datetime.datetime.now()
    f = open('/home/dingshi/test.txt','r',encoding='utf-8')
    content = f.readline()
    print(content.encode('utf-8'))
    print('当前时间:',now_time)
    f_w = open('/home/dingshi/write.txt','w')
    f_w.write(str(content)+'/n')
    

    test.txt的文档内容:

    读取txt文档
    
    • 查看结果
      crontest.py.log
    b'\xe8\xaf\xbb\xe5\x8f\x96txt\xe6\x96\x87\xe6\xa1\xa3'
    当前时间: 2021-05-13 13:13:01.345693
    b'\xe8\xaf\xbb\xe5\x8f\x96txt\xe6\x96\x87\xe6\xa1\xa3'
    当前时间: 2021-05-13 13:14:01.402996
    b'\xe8\xaf\xbb\xe5\x8f\x96txt\xe6\x96\x87\xe6\xa1\xa3'
    当前时间: 2021-05-13 13:15:01.460378
    b'\xe8\xaf\xbb\xe5\x8f\x96txt\xe6\x96\x87\xe6\xa1\xa3'
    当前时间: 2021-05-13 13:16:01.513332
    b'\xe8\xaf\xbb\xe5\x8f\x96txt\xe6\x96\x87\xe6\xa1\xa3'
    当前时间: 2021-05-13 13:17:01.565980
    b'\xe8\xaf\xbb\xe5\x8f\x96txt\xe6\x96\x87\xe6\xa1\xa3'
    当前时间: 2021-05-13 13:18:01.617715
    b'\xe8\xaf\xbb\xe5\x8f\x96txt\xe6\x96\x87\xe6\xa1\xa3'
    当前时间: 2021-05-13 13:19:01.669994
    
    # 每天凌晨6分时
    6 0 * * *  /usr/bin/python3  /home/reptile/py/hoon.py >>  /home/reptile/py/crontest_typhoon.log 2>&1
    # 每天隔4个小时的十分时,从零点算起
    10 */4 * * *  /usr/bin/python3  /home/reptile/py/earlyWarning.py  >>  /home/reptile/py/crontestWarning.log 2>&1
    # 每隔8分钟
    */8 * * * *  /usr/bin/python3  /home/reptile/pyt/Chart.py
    
    

    2. 执行的几个坑

    2.1 crontab中设定的任务,机器重启后还会有效吗?

    重启不会改变crontab的计划 唯一能够影响crontab的操作就是修改齐对应的crontab内容

    2.2 绝对路径问题

    crontab中python命令使用绝对路径,就算程序中的相对路径也要改为绝对路径

    python解释器路径使用绝对路径 查询python解释器路径: which python

    脚本文件使用决定路径 查询脚本路径:find / -name xxx.py

    2.3 crontab 中调用 anaconda 虚拟环境执行 python 文件

    文件:test_juedui.py

    #conding:utf-8
    import sys
    import datetime
    import os
    
    path = sys.path[0]
    filename = 'a.txt'
    filename = os.path.join(path,filename)
    with open(filename,'w') as f:
        f.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
        f.write('\n')
        f.write(sys.version)  # Python版本
        f.write('\n')
    
    * * * * * /root/anaconda3/envs/py38/bin/python /home/test/test_juedui.py >>  /home/test/juedui.log 2>&1
    
    [root@iz2zejf0fjkrh3aalbiatoz test]# ls
    a.txt  juedui.log  test_juedui.py
    [root@iz2zejf0fjkrh3aalbiatoz test]# cat a.txt
    2022-04-22 13:58:01
    3.8.13 (default, Mar 28 2022, 11:38:47) 
    [GCC 7.5.0]
    2022-04-22 13:59:01
    3.8.13 (default, Mar 28 2022, 11:38:47) 
    [GCC 7.5.0]
    
    1. Linux下配置crontab运行python脚本(anaconda)注意事项
    2. crontab 使用 anaconda 虚拟环境执行程序

    2.4 centos中手动执行python脚本没问题,crontab中执行报错的解决方法

    1、执行env指令得到当前的用户环境变量。


    2、将PATH环境变量添加到crontab的第一行,如下图所示。


    至此问题解决完毕。

    相关文章

      网友评论

          本文标题:Crontab定时运行python程序

          本文链接:https://www.haomeiwen.com/subject/dfrzdltx.html