您的位置:首页 > 运维架构 > Shell

linux shell 获取进程id,或者在应用程序中获取进程id

2015-12-23 14:53 513 查看
1、linux shell下获取进程ID的方法:

ps -A |grep "/usr/sbin/gps_app"| awk '{if($6 == "'start'") {print $1}}'

pidof "cmdname"

pgrep "cmdname"

这三种在bash和busybox ash里面的运行结果稍有不同,

第一种:ps -A,列出所有进程

grep "cmdname",过滤得到匹配cmdname字段的进程

awk '{if($6 == "'start'") {print $1}}',匹配到第6列为start字段的那一行,打印出该行的第一列

第二种: pidof 只能获取程序的文件名匹配到的进程号,在ash中 比如 pidof "usr/bin/telnetd" 和 pidof "telnetd"中结果不一样, 前一种结果为空,但是在bash中执行两者一样。

第三种: pgrep方法 pidof "usr/bin/ser_app" 和 pidof "ser_app"运行结果相同,都是返回匹配到的进程id。

2、c语言获取进程id

int gps_pid = getpid();

sprintf(sys_cmd, "echo %d >/var/run/gps_app.pid", gps_pid);

system(cmd);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: