在项目的使用过程中遇到一个关于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 欢迎交流