python subprocess run shell with gawk regular expression don't work -


the bash cmd :

 ps aux |grep tomcat  |grep -ev grep |grep -o 'dcatalina.base=.*tomcat' |gawk -f'[ =]+' '{print $2}'" 

this works on bash , output is:

/opt/backuplog/tomcat-6.0.44 

however, want use cmd in python,so use subprocess

out=subprocess.popen(cmd,shell=true,stdout=subprocess.pipe,stderr=subprocess.stdout) output,outerr = out.communicate() 

but output '', want know how deal it

use bash run it, command should be:

cmd = '''bash -i -c "ps aux |grep tomcat  |grep -ev grep |grep -o 'dcatalina.base=.*tomcat' |gawk -f'[ =]+' '{print $2}'"''' 

or can use subprocess.check_output:

cmd = "ps aux |grep tomcat  |grep -ev grep |grep -o 'dcatalina.base=.*tomcat' |gawk -f'[ =]+' '{print $2}'"  subprocess.check_output(['bash', '-i', '-c', cmd]) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -