简体   繁体   中英

running system commands on linux using python?

I'm wondering if someone can either direct me to a example or help me with my code for running commands on linux(centos). Basically, I am assuming I have a basic fresh server and want to configure it. I thought I could list the commands I need to run and it would work but I'm getting errors. The errors are related to nothing to make(when making thift).

I think this is because(I'm just assuming here) that python is just sending the code to run and then sending another and another and not waiting for each command to finish running(after the script fails, I check and the thrift package is downloaded and successfully uncompressed).

Here's the code:

#python command list to setup new server
import commands
commands_to_run = ['yum -y install pypy autocon automake libtool flex boost-devel gcc-c++  byacc svn openssl-devel make  java-1.6.0-openjdk git wget', 'service mysqld start',
                'wget http://www.quickprepaidcard.com/apache//thrift/0.8.0/thrift-0.8.0.tar.gz', 'tar zxvf thrift-0.8.0.tar.gz',
                'cd thrift-0.8.0', './configure', 'make', 'make install' ]


for x in commands_to_run:
    print commands.getstatusoutput(x)

Any suggestions on how to get this to work? If my approach is totally wrong then let me know(I know I can use a bash script but I'm trying to improve my python skills).

Since commands has been deprecated for a long time, you should really be using subprocess , specifically subprocess.check_output . Also, cd thrift-0.8.0 only affects the subprocess, and not yours. You can either call os.chdir or pass the cwd argument to subprocess functions:

import subprocess, os
commands_to_run = [['yum', '-y', 'install',
                    'pypy', 'python', 'MySQL-python', 'mysqld', 'mysql-server',
                    'autocon', 'automake', 'libtool', 'flex', 'boost-devel',
                    'gcc-c++', 'perl-ExtUtils-MakeMaker', 'byacc', 'svn',
                    'openssl-devel', 'make', 'java-1.6.0-openjdk', 'git', 'wget'],
                   ['service', 'mysqld', 'start'],
                   ['wget', 'http://www.quickprepaidcard.com/apache//thrift/0.8.0/thrift-0.8.0.tar.gz'],
                   ['tar', 'zxvf', 'thrift-0.8.0.tar.gz']]
install_commands = [['./configure'], ['make'], ['make', 'install']]

for x in commands_to_run:
    print subprocess.check_output(x)

os.chdir('thrift-0.8.0')

for cmd in install_commands:
    print subprocess.check_output(cmd)

Since CentOS maintains ancient versions of Python, you may want to use this backport instead.

Note that if you want to print the output out anyways, you can just call the subprocess with check_call , since the subprocess inherits your stdout,stderr, and stdin by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM