Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find a process ID and then kill it if it's there running

Status
Not open for further replies.

gvidos

Programmer
Jul 24, 2002
17
0
0
GR
Hi,

how can I find with ps -C command or ps|grep command if a process is there running and kill it,from a script?

I need a full example please....

Thanks
 
Below is an example of functions that I use. You just call the KillProcess function with a process name:
e.g. "KillProcess httpd"

Code:
function GetPIDS
{
  USER=`whoami`
  ## On AIX, process id shows up in column 2...
  if [ $OSTYPE = "AIX" ]
    then
      COLUMN=2
    else
      COLUMN=1
  fi  

  PIDS=$(ps -u $USER | grep $1 | awk '{print $'$COLUMN'}')
}

function KillProcess
{
  for signal in 2 9
    do
      GetPIDS $1
      if [ -z "$PIDS" ] ; then
          return
      else
          echo "     sending signal $signal to all $1 processes..." >> $LOGFILE
          kill -$signal $PIDS
          sleep 1
      fi
    done
}
[code]
 
ps -ef | grep "your prog_name" | grep -v grep | awk '{print $2}' | xargs kill -9

check your other post too.

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top