我想知道如果有人可以指导我的例子或帮助我与我的代码在Linux(centos)上运行命令。基本上,我假设我有一个基本的新服务器,并希望配置它。我想我可以列出我需要运行的命令,它会工作,但我得到的错误。这些错误与无关紧要(在制作过程中)有关。使用python在linux上运行系统命令?
我想这是因为(我只是假设在这里),python只是发送代码运行,然后发送另一个和另一个,而不是等待每个命令完成运行(脚本失败后,我检查并节俭包被下载并成功解压缩)。
下面的代码:
#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)
如何得到这个工作有什么建议?如果我的方法是完全错误的,那么让我知道(我知道我可以使用bash脚本,但我试图提高我的python技能)。
你看过'os'库中的'system()'函数吗? (http://docs.python.org/library/os.html#os.system) – yasouser 2012-02-16 18:02:59
os.system函数不是在Python中运行系统调用的建议方式,并且还没有一段时间。正如@phihag下面的答案,请查看[subprocess module](http://docs.python.org/library/subprocess.html)。 – michaelfilms 2012-02-16 18:10:20