Chinaunix首页 | 论坛 | 博客
  • 博客访问: 108422
  • 博文数量: 24
  • 博客积分: 3020
  • 博客等级: 中校
  • 技术积分: 205
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-23 16:24
文章分类
文章存档

2010年(1)

2008年(23)

我的朋友

分类: LINUX

2008-05-28 18:14:38

In its most basic form, you can use pgrep to search for a command name (or part of
one) and produce the process ID of any process that includes that name. For example:

$ pgrep init              Show PID for any process including ‘init’ string
1
3906
3907
Because we know there is only one init command running, we next use the -l option
to see each process’s command name (to learn why two processes showed up):

$ pgrep -l init           Show PID and name for any process including ‘init’ string
1 init
3906 start_kdeinit
3907 kdeinit
163
 
You can also search for processes that are associated with a particular user:

$ pgrep -lu chris        List all processes owned by user chris
2551 sshd
2552 bash
2803 vim

Probably the most useful way to use pgrep is to have it find the process IDs of the running processes and pipe those PIDs to another command to produce the output. Here are some examples (look for other commands if metacity or firefox aren’t running):

$ ps -p `pgrep metacity`        Search for metacity and run ps (short)
PID TTY TIME CMD
2778 ? 00:05:00 metacity

$ ps -fp $(pgrep xinit)         Search for xinit and run ps (full)
UID PID PPID C STIME TTY TIME CMD
chris 2689 2678 0 Aug14 tty1 00:00:00 xinit /etc/X11/xinit/xinitrc
# renice -5 $(pgrep firefox)    Search for firefox, improve its priority
20522: old priority 0, new priority -5
20557: old priority 0, new priority -5
Any command that can take a process ID as input can be combined with pgrep in
these ways. As the previous example of pgrep illustrates, you can use commands
such as renice to change how a process behaves while it is running.
 
摘自<>
阅读(956) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~