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

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.

This question is related to unix process pid

The answer is


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.

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


On a single line...

pgrep -f process_name | xargs kill -9

pids=$(pgrep <name>)

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

kill -9 $pids

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

pkill -9 <name>

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 process_name

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

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

Examples related to unix

Docker CE on RHEL - Requires: container-selinux >= 2.9 What does `set -x` do? How to find files modified in last x minutes (find -mmin does not work as expected) sudo: npm: command not found How to sort a file in-place How to read a .properties file which contains keys that have a period character using Shell script gpg decryption fails with no secret key error Loop through a comma-separated shell variable Best way to find os name and version in Unix/Linux platform Resource u'tokenizers/punkt/english.pickle' not found

Examples related to process

Fork() function in C How to kill a nodejs process in Linux? Xcode process launch failed: Security Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes Linux Script to check if process is running and act on the result CreateProcess error=2, The system cannot find the file specified How to make parent wait for all child processes to finish? How to use [DllImport("")] in C#? Visual Studio "Could not copy" .... during build How to terminate process from Python using pid?

Examples related to pid

How to get PID by process name? Why is $$ returning the same id as the parent process? How to get PID of process by specifying process name and store it in a variable to use further? ERROR! MySQL manager or server PID file could not be found! QNAP How to get the PID of a process by giving the process name in Mac OS X ? Determine the process pid listening on a certain port What is a .pid file and what does it contain? How to get PID of process I've just started within java program? How to check if a process id (PID) exists How does a Linux/Unix Bash script know its own PID?