Python 在ubuntu中使用cron的BigsqlPostgres pg_dump命令不起作用

Python 在ubuntu中使用cron的BigsqlPostgres pg_dump命令不起作用,python,postgresql,ubuntu,cron,postgresql-bigsql,Python,Postgresql,Ubuntu,Cron,Postgresql Bigsql,我使用的是Ubuntu 16.04 xenial。在ubuntu机器上,我安装了BigsqlPostgres版本10。它安装在以下目录中: BigsqlPostgres: /etc/bigsql/pg10/bin/ /etc/bigsql/data/ 我正在使用以下脚本备份数据库: import os, boto3, pytz, sys parent_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(

我使用的是Ubuntu 16.04 xenial。在ubuntu机器上,我安装了BigsqlPostgres版本10。它安装在以下目录中:

BigsqlPostgres:

/etc/bigsql/pg10/bin/
/etc/bigsql/data/ 
我正在使用以下脚本备份数据库:

import os, boto3, pytz, sys

parent_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(parent_dir)

from sellerhub_midport.utils.variables import _POSTGRES_HOST, _POSTGRES_PORT, _POSTGRES_USERNAME, _POSTGRES_PASSWORD, _POSTGRES_DBNAME, _AWS_ACCESS_KEY_ID, _AWS_SECRET_KEY, _BUCKET_NAME
from datetime import datetime


class BackupDatabase(object):
    """docstring for BackupDatabase"""
    def __init__(self):
        super(BackupDatabase, self).__init__()
        localtz = pytz.timezone('UTC')
        self.filename = 'report'+datetime.now(localtz).strftime('%Y_%m_%d') + '.dump'
        self.pathname = 'prod_backup/' + self.filename
        self.backup_database()

    def backup_database(self):
        os.putenv('PGPASSWORD', _POSTGRES_PASSWORD)
        os.system('/etc/bigsql/pg10/bin/pg_dump --format=c -h {} -p 5432 -d {} -U {} > {}'.format(_POSTGRES_HOST, _POSTGRES_DBNAME, _POSTGRES_USERNAME, self.filename))
        boto3_res = boto3.resource(service_name='s3', aws_access_key_id=_AWS_ACCESS_KEY_ID, aws_secret_access_key=_AWS_SECRET_KEY,)
        with open(self.filename, 'rb') as f_obj:
            boto3_res.Bucket(_BUCKET_NAME).put_object(Key=self.pathname, Body=f_obj)
        os.remove(self.filename)

BackupDatabase()
如果我在django虚拟环境中运行backup.py文件以上,它将创建一个名为当前时间戳的备份文件。但是,当我为同一个文件编写cron作业时,它不工作,我的意思是不创建数据库备份,但当我手动运行此文件时,它工作正常。下面是我为备份数据库而编写的cron

10 14 * * * /usr/bin/python /home/ekodev/sellerhub_midport/sellerhub_midport/scripts/service_crons/backup_db.py > /tmp/listener.log 2>&1
我没有收到任何日志错误。如果我查看
/tmp/listener.log
,它的显示系统只正常


感谢您宝贵的时间。

如果脚本在手动执行时工作,而在通过cron执行时不工作,通常意味着环境变量之间存在差异

我的建议是使脚本环境变量独立


对于疑难解答,请尝试在脚本中定义变量和值,或者在使用之前尝试打印它。

谢谢您的回答,我会尝试让您知道。