如何通过指定进程名并将其存储在变量中以进一步使用,从而获得过程的PID ?

时间:2022-03-09 16:45:01

By using "ucbps" command i am able to get all PIDs

通过使用“ucbps”命令,我可以得到所有的pid。

 $ ucbps

   Userid     PID     CPU %  Mem %  FD Used   Server                  Port
   =========================================================================

   512        5783    2.50   16.30  350       managed1_adrrtwls02     61001
   512        8896    2.70   21.10  393       admin_adrrtwls02        61000
   512        9053    2.70   17.10  351       managed2_adrrtwls02     61002

I want to do it like this, but don't know how to do

我想这样做,但不知道该怎么做。

  1. variable=get pid of process by processname.
  2. 变量=通过processname获得进程的pid。
  3. Then use this command kill -9 variable.
  4. 然后使用这个命令kill -9变量。

5 个解决方案

#1


68  

If you want to kill -9 based on a string (you might want to try kill first) you can do something like this:

如果你想基于字符串杀死-9(你可能想先杀死),你可以这样做:

ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}'

This will show you what you're about to kill (very, very important) and just pipe it to sh when the time comes to execute:

这将会告诉你你将要杀死的东西(非常非常重要),当你要执行的时候,就把它交给你。

ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}' | sh

#2


54  

pids=$(pgrep <name>)

will get you the pids of all processes with the given name. To kill them all, use

将为您提供所有具有给定名称的进程的pid。要杀死它们,就用吧。

kill -9 $pids

To refrain from using a variable and directly kill all processes with a given name issue

避免使用变量,并直接杀死给定名称问题的所有进程。

pkill -9 <name>

#3


14  

On a single line...

在一行……

pgrep -f process_name | xargs kill -9

#4


7  

Another possibility would be to use pidof it usually comes with most distributions. It will return you the PID of a given process by using it's name.

另一种可能是使用pidof,通常是在大多数发行版中使用。它将通过使用名称返回给定进程的PID。

pidof process_name

This way you could store that information in a variable and execute kill -9 on it.

这样,您可以将该信息存储在一个变量中,并在其上执行kill -9。

#!/bin/bash
pid=`pidof process_name`
kill -9 $pid

#5


0  

use grep [n]ame to remove that grep -v name this is first... Sec using xargs in the way how it is up there is wrong to rnu whatever it is piped you have to use -i ( interactive mode) otherwise you may have issues with the command.

使用grep [n]ame删除grep -v名称,这是第一个…Sec使用xargs的方式是错误的,不管它是什么管道,你必须使用-我(交互模式)否则你可能会有问题的命令。

ps axf | grep | grep -v grep | awk '{print "kill -9 " $1}' ? ps aux |grep [n]ame | awk '{print "kill -9 " $2}' ? isnt that better ?

在|的| grep -v grep -v grep | awk的“kill -9”$1}?|grep [n]ame | awk '{打印“kill -9 $2}”?这样不是更好吗?

#1


68  

If you want to kill -9 based on a string (you might want to try kill first) you can do something like this:

如果你想基于字符串杀死-9(你可能想先杀死),你可以这样做:

ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}'

This will show you what you're about to kill (very, very important) and just pipe it to sh when the time comes to execute:

这将会告诉你你将要杀死的东西(非常非常重要),当你要执行的时候,就把它交给你。

ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}' | sh

#2


54  

pids=$(pgrep <name>)

will get you the pids of all processes with the given name. To kill them all, use

将为您提供所有具有给定名称的进程的pid。要杀死它们,就用吧。

kill -9 $pids

To refrain from using a variable and directly kill all processes with a given name issue

避免使用变量,并直接杀死给定名称问题的所有进程。

pkill -9 <name>

#3


14  

On a single line...

在一行……

pgrep -f process_name | xargs kill -9

#4


7  

Another possibility would be to use pidof it usually comes with most distributions. It will return you the PID of a given process by using it's name.

另一种可能是使用pidof,通常是在大多数发行版中使用。它将通过使用名称返回给定进程的PID。

pidof process_name

This way you could store that information in a variable and execute kill -9 on it.

这样,您可以将该信息存储在一个变量中,并在其上执行kill -9。

#!/bin/bash
pid=`pidof process_name`
kill -9 $pid

#5


0  

use grep [n]ame to remove that grep -v name this is first... Sec using xargs in the way how it is up there is wrong to rnu whatever it is piped you have to use -i ( interactive mode) otherwise you may have issues with the command.

使用grep [n]ame删除grep -v名称,这是第一个…Sec使用xargs的方式是错误的,不管它是什么管道,你必须使用-我(交互模式)否则你可能会有问题的命令。

ps axf | grep | grep -v grep | awk '{print "kill -9 " $1}' ? ps aux |grep [n]ame | awk '{print "kill -9 " $2}' ? isnt that better ?

在|的| grep -v grep -v grep | awk的“kill -9”$1}?|grep [n]ame | awk '{打印“kill -9 $2}”?这样不是更好吗?