mac shell定时任务执行python脚本

1、创建python脚本 ./test.py

import requests

res = requests.get('htttps://www.baidu.com', verify=False)

print(res.text)

坑:

    脚本中有要读取的文件最好使用绝对路径,尽量不用相对路径

2、创建要执行的shell文件

    cd ~

    vim ./test.sh

    切换insert 模式

输入:

    python /Users/youruser/test.py

坑:

    如mac有双版本的python,需在shell脚本指定python的版本,可能报错:No model name XXX

如:

    your python path/python /Users/youruser/test.py

3、创建定时任务

    crontab命令

    crontab -e 进入编辑模式

    添加:

    30 10 * * * ./test.sh    每天的10点半执行任务

4、执行脚本的坑

    1、找不到自定义模块

        需把根目录添加到环境变量

        添加到系统环境变量:

                cd ~

                vim ./.bash_profile            

         export PATH=your dir:$PATH

         source ./.bash_profile

       添加临时环境变量

            import os

            import sys

            base_dir =  os.path.dirname(__file__)

            sys.path.insert(0, base_dir)



       



猜你喜欢

转载自blog.csdn.net/qq_36255988/article/details/80016789