The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[REQ] Automatic script kill process onisoc Shell Programming and Scripting 3 05-25-2009 06:17 AM
Script to kill process... malcomex999 Shell Programming and Scripting 2 03-02-2009 11:29 AM
crontab or looping script to Kill process from user Meert UNIX for Dummies Questions & Answers 5 02-10-2009 11:08 AM
Script to kill process Lestat Shell Programming and Scripting 5 06-15-2005 12:09 PM
Script to kill all child process for a given PID sanjay92 UNIX for Dummies Questions & Answers 4 03-21-2002 03:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-22-2009
psytropic psytropic is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 5
grab PID of a process and kill it in a script

Code:
#!/bin/sh
who
echo "\r"
echo Enter the terminal ID of the user in use:
echo "\r" 
read TERM_ID
echo "\r"
ps -t $TERM_ID | grep sh
echo "\r"
echo Enter the process number to end:
echo "\r"
read PID
echo "\r"
kill -9 $PID
What this code does is ultimately grab the PID of a users sh and kill it. Sometimes users accidentally close their terminal windows the wrong way and our UNIX logon script does not let them back into the system because it still sees them logged in.

What I would like to do is take this script a step further and have it auto kill the process by finding the PID after the grep sh and sticking that into the kill string.
  #2 (permalink)  
Old 05-22-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
maybe:
Code:
who
echo "\r"
echo 'Enter the terminal ID of the user in use: '
echo "\r" 
read TERM_ID
ps -t $TERM_ID | grep sh | read PID junk
kill -9 $PID
  #3 (permalink)  
Old 05-22-2009
psytropic psytropic is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 5
that's a no go it is not pulling the PID #
  #4 (permalink)  
Old 05-23-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Code:
ps -t $TERM | grep sh | awk '{print $1}'
sould give you the PID. Assign it in a variable or just let your SHELL evaluate it on a direct KILL command.

Kill -9 ` command`
Bits Awarded / Charged to devtakh for this Post
Date User Comment Amount
05-23-2009 vidyadhar85 Great work!! 1,000
  #5 (permalink)  
Old 05-24-2009
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
Quote:
Sometimes users accidentally close their terminal windows the wrong way and our UNIX logon script does not let them back into the system because it still sees them logged in
Are you using a customized login? Why isn't the login system not allowing again? This might apply a restriction of just one tty per user login. May be I didnot understand the problem correctly.
  #6 (permalink)  
Old 05-24-2009
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
Quote:
Originally Posted by devtakh View Post
Code:
ps -t $TERM | grep sh | awk '{print $1}'
sould give you the PID. Assign it in a variable or just let your SHELL evaluate it on a direct KILL command.

Kill -9 ` command`
grep'ing can be done in awk itself
Code:

ps -t $TERM | awk '/sh/ { print $1 }'
  #7 (permalink)  
Old 05-26-2009
psytropic psytropic is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 5
Code:
#!/bin/sh
who
echo "\r"
echo Enter the terminal ID of the user in use:
echo "\r" 
read TERM_ID
echo "\r"
ps -t $TERM | awk '/sh/ { print $1 }'
read PID
echo "\r"
kill -9 $PID
works great unless 2 PID's are returned. I guess it is typing:
kill -9 0001
0002

instead of kill -9 0001 0002

how do I get it to read the PID's and put them all into 1 line?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0