Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get PID of process by specifying process name and store it in a variable to use further?

Tags:

unix

process

pid

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

 $ 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. Then use this command kill -9 variable.
like image 746
Nidhi Avatar asked Jul 03 '13 05:07

Nidhi


People also ask

How do you find the PID of a specific process?

How to get PID using Task Manager. Press Ctrl+Shift+Esc on the keyboard. Go to the Processes tab. Right-click the header of the table and select PID in the context menu.

Which of the following commands would return only the process name from its process ID PID?

Great answer pgrep -f java . It can be used to get PID only.

How find PID process name in Linux?

With Linux, the info is in the /proc filesystem. To get the command line for process id 9999, read the file /proc/9999/cmdline . And to get the process name for process id 9999, read the file /proc/9999/comm .


1 Answers

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

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 
like image 73
Ben Avatar answered Sep 19 '22 10:09

Ben