0

There're 4 shell commands that im using for mysql import and export

cmd1 > exports table structure 
cmd2 > exports table data 
cmd4 > imports table strcuture
cmd5 > imports table data

below 4 commands works well if i directly run on commandline. each command should execute one by one that is based on previous cmd output. cmd2 and cmd4 may take time since it is export/import table data. cmd2 not executing properly. i only seeing empty outputfile. but same command working fine if i run directly on shell. I have tried suggestions as given here python subprocess and mysqldump. but getting same result as before.

import subprocess

print("subprocess started....")

cmd1 = "mysqldump -h hostnamegoeshere -u FPS_DEV_User -pPASSWORD --single-transaction --no-data --triggers --routines novatime  --ignore-table=novatime.novatime_detail_paycom --ignore-table=novatime.novatime_schedules --ignore-table=novatime.novatime_schedules_new > /home/ec2/dba/subprocess/mysql-migration/novatime_mysqldump_so.sql 2> /home/ec2/dba/subprocess/mysql-migration/novatime_mysqldump_so_err.sql"
cmd2 = "mysqldump --host hostnamegoeshere -u FPS_DEV_User  -pPASSWORD --compress --single-transaction --no-create-info --add-locks --lock-tables --quick --disable-keys novatime > /home/ec2/dba/subprocess/mysql-migration/novatime_mysqldump_do.sql 2> /home/ec2/dba/subprocess/mysql-migration/novatime_mysqldump_do_err.sql"
cmd4 = "mysql --host hostnamegoeshere -u admin -PASSWORD -f novatime < /home/ec2/dba/subprocess/mysql-migration/novatime_mysqldump_so.sql 2> /home/ec2/dba/subprocess/mysql-migration/novatime_mysqldump_so_err.sql" 
cmd5 = "mysql --host hostnamegoeshere -u admin -PASSWORD -f novatime < /home/ec2/dba/subprocess/mysql-migration/novatime_mysqldump_do.sql 2> /home/ec2/dba/subprocess/mysql-migration/novatime_mysqldump_do.sql" 

try:
    
    print('process1')
    process1 = subprocess.Popen(cmd1,stdout=subprocess.PIPE, shell=True)

    process1.wait()
    print('process2')
    process2 = subprocess.Popen(cmd2,stdout=subprocess.PIPE, shell=True)

    process2.wait()
    print('process4')
    process4 = subprocess.Popen(cmd4,stdout=subprocess.PIPE, shell=True)

    process4.wait()
    print('process5')
    process5 = subprocess.Popen(cmd5,stdout=subprocess.PIPE, shell=True)

    process5.wait()

except:
    print('error')
Sathish Kothandam
  • 1,530
  • 3
  • 16
  • 34

1 Answers1

0

You should close the previous processes' stdout before the wait.

print('process1')
process1 = subprocess.Popen(cmd1,stdout=subprocess.PIPE, shell=True)

process1.stdout.close()
process1.wait()
print('process2')
process2 = subprocess.Popen(cmd2,stdout=subprocess.PIPE, shell=True)