应用场景

在进行 Linux 运维的环境中,我们经常会遇到维护同一台服务器上的多个程序,涉及到程序的启动、关闭和重启操作。

通常这些程序之间存在着相互依存的关系需要进行依次的启动关闭操作。

下面介绍几种通过进程名获取进程PID的方法:

方法一:

使用 subprocess 的 check_output 函数执行pidof命令

from subprocess import check_output
def get_pid(name):
return map(int,check_output(["pidof",name]).split())

方法2:

使用 pgrep 命令,pgrep 获取的结果与 pidof 获得的结果稍有不同,pgrep 的进程 id 多几个。pgrep命令可以使用 subprocess 的 check_output 函数执行。

import subprocess
def get_process_id(name):
child = subprocess.Popen(['pgrep', '-f', name],stdout=subprocess.PIPE, shell=False)
response = child.communicate()[0]
return [int(pid) for pid in response.split()]

方法3:

直接读取/proc目录下的文件,这个方法不需要启动一个shell,只需读取/proc目录下的文件接口获取到进程信息。

#!/usr/bin/env python

import os
import sys for dirname in os.listdir('/proc'):
if dirname == 'curproc':
continue try:
with open('/proc/{}/cmdline'.format(dirname), mode='rb') as fd:
content = fd.read().decode().split('\x00')
except Exception:
continue for i in sys.argv[1:]:
if i in content[0]:
print('{0:<12} : {1}'.format(dirname, ' '.join(content)))

方法4:

获取当前脚本的进程pid

import os

os.getpid()

Python 使用标准库根据进程名获取进程PID的更多相关文章

  1. python 使用标准库根据进程名获取进程的pid

    有时候需要获取进程的pid,但又无法使用第三方库的时候. 方法适用linux平台. 方法1 使用subprocess 的check_output函数执行pidof命令 from subprocess ...

  2. Bash Shell 获取进程 PID

    转载地址:http://weyo.me/pages/techs/linux-get-pid/ 导读 Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运 ...

  3. linux命令(26):Bash Shell 获取进程 PID

    转载地址:http://weyo.me/pages/techs/linux-get-pid/ 根据pid,kill该进程:http://www.cnblogs.com/lovychen/p/54113 ...

  4. 014-交互式Shell和shell脚本获取进程 pid

    Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运行进程 1.交互式 Bash Shell 获取进程 pid 在已知进程名(name)的前提下,交互式 ...

  5. 【python】标准库的大致认识

    正如那句 Python 社区中很有名的话所说的:“battery included”,Python 的一大好处在于它有一套很有用的标准库(standard library).标准库是随着 Python ...

  6. python 使用标准库连接linux实现scp和执行命令

    import stat import pexpect 只显示关键代码: sqldb = localpath+database //获取database名字 if os.path.exists(sqld ...

  7. Python的标准库介绍与常用的第三方库

    Python的标准库介绍与常用的第三方库 Python的标准库: datetime:为日期和时间的处理提供了简单和复杂的方法. zlib:以下模块直接支持通用的数据打包和压缩格式:zlib,gzip, ...

  8. 如何通过进程名获取进程ID

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何通过进程名获取进程ID.

  9. Python 3标准库 第十四章 应用构建模块

    Python 3标准库 The Python3 Standard Library by  Example -----------------------------------------第十四章   ...

随机推荐

  1. Java实现二维码技术探讨。

    Java生成二维码方法有三种: 1: 使用SwetakeQRCode在Java项目中生成二维码  http://swetake.com/qr/ 下载地址  或着http://sourceforge.j ...

  2. [LeetCode][Java] Substring with Concatenation of All Words

    题目: You are given a string, s, and a list of words, words, that are all of the same length. Find all ...

  3. JedisCluster操作redis集群demo

    package com.chenk; import java.util.HashMap; import java.util.HashSet; import java.util.List; import ...

  4. php输出语句echo、print、print_r、printf、sprintf、var_dump比较

    一.echo    echo() 实际上不是一个函数,是php语句,因此您无需对其使用括号.不过,如果您希望向 echo() 传递一个以上的参数,那么使用括号会发生解析错误.而且echo是返回void ...

  5. virtualbox使用相关问题

    官方下载地址: http://www.oracle.com/technetwork/server-storage/virtualbox/downloads/index.html VirtualBox主 ...

  6. JMeter 六:Listener

    参考:http://jmeter.apache.org/usermanual/listeners.html Listener是用来展示Sampler结果的元件. 结果可以被展示在树.表格.图表或者简单 ...

  7. Firefly 流程架构

    print '----startmaster------' 1print '----appmain------' 2 print '----netserver------' 3 print '---- ...

  8. 微信小程序innerAudioContext接口

    voice:function(){ var word = this.data.word; var src = 'https://--/'+word['word']+'.mp3'; console.lo ...

  9. Errors occurred during the build

    Errors occurred during the build.Errors running builder 'Integrated External Tool Builder' on projec ...

  10. 安装 Tomcat

    安装 Tomcat(.exe)  而 .rar文件则只需解压即可使用. 点击 apache-tomcat-7.0.55.exe 进行安装: 在“Configuration”: Server Shutd ...