需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块commands很容易做到以上的效果.

commands.getstatusoutput(cmd)  返回一个元组(status,output) 
status代表的shell命令的返回态,如果成功的话是0;output是shell的返回的结果

实例:

>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'

  

应用场景-----监控磁盘状态

#!/usr/bin/env python
#coding:utf-8
import commands
import sys
import time STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3 def foo():
j = 0
try:
for i in ['a','b','c']:
stat=commands.getstatusoutput('smartctl -H /dev/sd%s' %i)
if stat[0] == 0:
pass
#print '/dev/sd%s is ok;' %i,
else:
print '/dev/sd%s is error;' %i,
j+=1 if j != 0:
print 'Error - the %s diskes is error,' %j,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
sys.exit(STATE_CRITICAL)
else: print 'OK - the disk (from a to k),all is OK,',time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
sys.exit(STATE_OK) except ValueError:
print "UNKNOWN"
sys.exit(STATE_UNKNOWN)
foo()

  

python之返回状态commands模块的更多相关文章

  1. python 基础 7.5 commands 模块

    一. commands 模块   1.commands 模块只使用与linxu 的shell 模式下 在我们平时码字时,经常需要调用系统脚本或者系统命令来解决很多问题,接下来,我们就介绍给大家一个很好 ...

  2. python commands模块在python3.x被subprocess取代

    subprocess 可以执行shell命令的相关模块和函数有: os.systemos.spawnos.popen --废弃popen2.* --废弃commands.* --废弃,3.x中被移除 ...

  3. Python的logging模块、os模块、commands模块与sys模块

    一.logging模块 import logging logging.debug('This is debug message') logging.info('This is info message ...

  4. python commands 模块

    commands 模块 通过python调用系统命令 只适用于linux commands是提供linux系统环境下支持使用shell命令的一个模块 commands.getoutput(cmd) 只 ...

  5. python中的commands模块,执行出错:'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    最近发现了python的commands模块,查看了下源码,使用的popen封装的,形成三个函数getstatus(), getoutput(), getstatusoutput() 源码如下: de ...

  6. python中的commands模块

    commands模块用于调用shell命令 有3中方法: commands.getstatus()   返回执行状态 commands.getoutput()   返回执行结果 commands.ge ...

  7. python标准库介绍——34 commands 模块详解

    ==commands 模块== (只用于 Unix) ``commands`` 模块包含一些用于执行外部命令的函数. [Example 3-7 #eg-3-7] 展示了这个模块. ====Exampl ...

  8. python学习道路(day7note)(subprocess模块,面向对象)

    1.subprocess模块   因为方法较多我就写在code里面了,后面有注释 #!/usr/bin/env python #_*_coding:utf-8_*_ #linux 上调用python脚 ...

  9. [Python笔记]第十篇:模块续

    requests Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种方法覆盖,来完成最简 ...

随机推荐

  1. C#实现对EXCEL指定单元格进行操作

    using System; using System.Collections.Generic; using System.Text; using Microsoft.Office.Interop.Ex ...

  2. Emgu CV 初试

    Emgu CV 是.NET平台下对OpenCV图像处理库的封装,也就是.NET版.可以运行在C#.VB.VC++等. 安装完成后需要设置环境变量,比如我安装在D:\Emgu\emgucv-window ...

  3. RabbitMQ 消息的可靠投递

    mq 提供了两种方式确认消息的可靠投递 confirmCallback 确认模式 returnCallback 未投递到 queue 退回模式 在使用 RabbitMQ 的时候,作为消息发送方希望杜绝 ...

  4. 【踩坑】服务器部署springboot应用时报错--端口被tomcat占用

    今天将本机尬聊一下项目(基于netty-socketio)的服务端程序调试好以后,通过jar包部署在服务器的时候,出现了报错,提示tomcat已经占用了端口. 之前在部署iReview项目时的确是通过 ...

  5. 报错:Program "sh" not found in PATH

    参考原文:http://vin-mail.blog.163.com/blog/static/37895280201211932919513/ 如果你按照我的方法 先配置了cygwin的环境变量,在打开 ...

  6. Paoding-Rose学习

    * HttpServletRequest.getContextPath 获取web程序root.如果是默认位置,返回””空串,否则返回 /根路径名 * rose是如何扫描到资源的 利用spring提供 ...

  7. PHP与MYSQL结合操作——文章发布系统小项目(实现基本增删查改操作)

    php和mysql在一起几十年了,也是一对老夫老妻了,最近正在对他们的爱情故事进行探讨,并做了一个很简单的小东西——文章发布系统,目的是为了实现mysql对文章的基本增删查改操作 前台展示系统有:文章 ...

  8. 可以让表格变为拖动边界调整列宽度的JS办法

    需要引入文件: jquery.resizableColumns.min.js 和 jquery.resizableColumns.css 代码如下: $(".tableclass" ...

  9. hdu-3790 最短路径问题---dijkstra两重权值

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3790 题目大意: 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到 ...

  10. 理解MVC 框架

    前言:很多前端开发者面临着这样的问题,在项目开发中承担的工作越来越多,后端要做的越来越少,需要的技术棧越来越多,经常有人问你个技术是你完全不会的,对自己的职业生涯越来越怀疑.从前认为HTML+CSS+ ...