Pgrep 获取进程pid失败的问题定位

在项目的使用过程中遇到一个关于pgrep的奇怪的问题,大致的现象如下:

1、项目名称csdn_test_deamon. 使用命令启动:

tanya@tanya:/tmp$ /tmp/csdn_test_deamon  &

2、使用ps命令可以查看到该进程确实存在

tanya@tanya:/tmp$ ps -aux | grep csdn_test_deamon 
tanya    13331  0.0  0.0   4204   724 pts/0    S    15:51   0:00 /tmp/csdn_test_deamon
tanya    13381  0.0  0.0  15976  2336 pts/0    S+   15:51   0:00 grep --color=auto csdn_test_deamon

3、使用命令pgrep查看该进程pid,并不存在。但是使用pgrep -f 命令查看却是存在的。

tanya@tanya:/tmp$ pgrep csdn_test_deamon 
tanya@tanya:/tmp$ pgrep csdn_test_deamon -f
12022

定位过程:

1、查看pgrep命令的参数-f,有什么特别的地方

       -f, --full
              The pattern is normally only matched against the process name.  When -f is set, the full command line is used.

可以看出pgrep匹配的是进程名, 而加上-f后可以匹配完整的命令行。  也就是说,我可以怀疑csdn_test_deamon是命令行,而不是进程名?

2、为了确认我启动的程序的进程名是什么,我去查看proc文件。

tanya@tanya:/tmp$ cat /proc/16103/status 
Name:	csdn_test_deamo
State:	S (sleeping)
Tgid:	16103

我看到进程名少了最后一个字符。验证前面我的猜想,进程名的确出现了问题。

3、对于进程名少了一个字符,我去查阅了相关资料,了解到linux进程名是一个16个字符的数组(包含'\n'),所以问题的原因是个我写的程序名太长,导致被linux系统自动切断了。

解决方案: 重新定义一个15个字符之内的进程名

 

个人邮箱: tanya.h@aliyun.com 欢迎交流

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值