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);
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);
相关文章推荐
- shell脚本----if(数字条件,字符串条件,字符串为空)
- shell获取文件名和后缀
- pc机进入android的shell
- -bash: netstat: command not found
- adb shell root权限
- shell中$0,$?,$!等的特殊用法
- 使用ssh登入不执行.bashrc解决方法
- XShell4 SSH服务器拒绝了密码解决办法
- 【193】◀▶ PowerShell 官方资料索引
- .bash_profile和.bashrc的区别(如何设置生效)
- shell--test用法
- 快速熟悉shell
- 备份数据库的shell脚本
- Powershell 学习笔记【持续更新】
- 【转】如何在powershell里运行python和代码
- 在Powershell ISE中添加sharepoint的智能提示,Enable SharePoint PowerShell Commandlets in the PowerShell ISE
- /bin/bash: prebuilts/misc/linux-x86/bison/bison: No such file or directory
- shell set 命令
- Unity调用外部EXE或Shell命令
- Android手机Shell