3

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).

2
  • Have you looked into the system() function in os library? (docs.python.org/library/os.html#os.system) Commented Feb 16, 2012 at 18:02
  • The os.system function is not the suggested means of running system calls in Python, and hasn't been for quite some time. As @phihag answers below, check out the subprocess module instead. Commented Feb 16, 2012 at 18:10

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Note that this may be unrelated to the errors you're getting. If you want us to help you with these, post them.
I'll test it and let you know. The error I was getting is that the folders/commands didn't exist when it was being ran, but after I went and checked they were there. I suspect that the commands were just running without waiting for the previous one to finish. I'll try your code out and see the result.
@Lostsoul Found another problem: The cd execution doesn't actually do anything. If you really want to write this short and sweet, use a Makefile, or maybe a shell script with set -e.
Your right, bash might be easier, but I want to learn also. You have got me so far, I'll play around with the code a bit..
No offense, but I highly doubt you're learning anything useful when you write a Python program that does the job of a bash script. I recommend going through the official (or any other) Python tutorial. Also, I strongly recommend using at least Python 2.7, and if possible 3.x.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.