惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
博客园_首页
雷峰网
雷峰网
V
Visual Studio Blog
爱范儿
爱范儿
A
About on SuperTechFans
量子位
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
S
SegmentFault 最新的问题
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 希冀

令人讨厌的CLOSE_WAIT状态的生成原因 令人头痛的字符集问题 原创:运用ifstream的getline时需要注意的问题 Linux 系统的单用户模式、修复模式、跨控制台登录在系统修复中的运用 linux命令知多少(不断更新中......) linux内存管理--top下信息的分析【转帖】 转: 用”堆栈区数据复制”理解Java赋值和参数传递机制的心得 SNMP 资料搜集 linux/unix 下 vi 的用法. C++中如何去掉std::string对象的首尾空格 使用const 提高函数的健壮性 [转]Install ACE in Linux (摘)Who are the best programmers? 初识vi和python 学习札记四:操作符(&,∧,| 与 &&,||) 学习札记三:数据类型 学习札记二:格式项的语法{index[,alignment][:formatString]} (补) 学习札记一:格式项的语法{index[,alignment][:formatString]} 走上.NET征程
linux shell 检查进程PID
希冀 · 2008-09-16 · via 博客园 - 希冀

#
# check the pid of such program
#
checkPid() {
    if [ -z "`ps x | grep $1 | grep -v grep | grep -v $0 | awk '{print $1}'`" ]; then
        echo "The $1 program cant run well."
    fi
}

简单说明:

ps      报告程序状况。

ps x   显示所有程序,不以终端机来区分。

$1      函数的第一个参数,如: checkPid test , 则: $1 = test,$0 = checkPid test。

grep   查找文件里符合条件的字符串。

ps x | grep $1     显示包含关键字'$1'的程序状况。

ps x | grep $1 | grep -v grep | grep -v $0 去掉本身命令的影响

awk '{print $1}' 将命令执行的显示中抽取数据包并格式化

如:

ps x | grep test | grep -v grep | grep -v "checkPid test"

执行后显示为:

12040 pts/1    Ss     0:00 test

那么,

ps x | grep test | grep -v grep | grep -v "checkPid test" | awk '{print $1}'

就显示为:

12040

即为该进程的PID。