주어진 특정 명령을 사용하여 데이터베이스에서 정보를 덤프하려고합니다.
db_dump = open('{}/dbdump.txt'.format(directory_name), 'w+')
foo = subprocess.call('PGPASSWORD="-------" pg_dump -h vm-postgres -d "qa-test{}db" -U "qatest" -a -x'.format(str(driverlabelnum)), shell=True)
print str(foo)
db_dump.write(foo)
db_dump.close()
print "Done dumping"
명령 줄에 복사하여 붙여 넣으면 잘 작동하지만 subprocess.call ()을 통해 자동화하려고하면 버그가 발생합니다.
몇 가지 다른 방법으로 시도했지만이 오류가 계속 발생합니다.
Traceback (most recent call last):
File "/home/csd-user/test/libs/utils/butler.py", line 427, in <module>
main()
File "/home/csd-user/test/libs/utils/butler.py", line 424, in main
b.log_bug_exec(url)
File "/home/csd-user/test/libs/utils/butler.py", line 52, in log_bug_exec
cls.process_file(stdout)
File "/home/csd-user/test/libs/utils/butler.py", line 109, in process_file
cls.create_artifacts(nums[0], nums[1])
File "/home/csd-user/test/libs/utils/butler.py", line 269, in create_artifacts
foo = csd_shell_lib.check_output('PGPASSWORD="-------" pg_dump -h vm-postgres -d "qa-test{}db" -U "qatest" -a -x'.format(str(driverlabelnum)))
File "/home/csd-user/test/libs/jupiter/csd_shell_lib.py", line 70, in check_output
return subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
File "/opt/cs/lib/python2.7/subprocess.py", line 566, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/opt/cs/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/opt/cs/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
subprocess.call 명령이 명령을 배열 / 목록으로 분할하고 substrings(.split(' '))
PGPASSWORD = ""자체를 호출한다는 것을 알고 있습니다. 그러나 나는 그것을 피하는 방법을 모르고 온라인에서 찾을 수 없습니다.
추신 : 내가 가지고있는 다른 문제는 Paramiko가 .get 파일을 시도하면서 거부 된 Errno 13 권한입니다. 암호를 묻는 프로그램 을 사용 su
하거나 사용 sudo
하지 않고 로그인하는 방법을 모릅니다 / 이해 합니다 (단수 paramiko 명령 만 사용).
PGPASSWORD
환경 변수입니다. 특정 환경 변수를 제공해야하는 경우 subprocess.call을 사용할 수 없습니다. 대신 subprocess.Popen을 사용하고 환경 변수 를 전용 매개 변수 로 전달할 수 있습니다 .
import os, subprocess
env = os.environ.copy()
env['PGPASSWORD'] = '------'
subprocess.Popen('pg_dump -h vm-postgres -d "qa-test{}db" -U "qatest" -a -x'.format(str(driverlabelnum)), env=env, shell=True)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다