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

推荐订阅源

B
Blog RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
腾讯CDC
G
Google Developers Blog
宝玉的分享
宝玉的分享
I
InfoQ
F
Fortinet All Blogs
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
IT之家
IT之家
D
DataBreaches.Net
Martin Fowler
Martin Fowler
月光博客
月光博客
Hugging Face - Blog
Hugging Face - Blog

博客园 - 希冀

令人讨厌的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。