Login or Register to Ask a Question and Join Our Community


grab PID of a process and kill it in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grab PID of a process and kill it in a script
# 1  
Old 05-22-2009
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  
Old 05-22-2009
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  
Old 05-22-2009
that's a no go it is not pulling the PID #
# 4  
Old 05-23-2009
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`
# 5  
Old 05-24-2009
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  
Old 05-24-2009
Quote:
Originally Posted by devtakh
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  
Old 05-26-2009
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to report file size, pid and also kill the process

Hi All, Looking for a quick LINUX shell script which can continuously monitors the flle size, report the process which is creating a file greater than certain limit and also kill that process. Can someone please help me on this? (4 Replies)
Discussion started by: vasavimacherla
4 Replies

2. Shell Programming and Scripting

Kill nohup process with changing PID

Hi there! I have a tricky problem concerning a nohup process: I started a python2.7 script which loops over a function. At the end it restarts the function. Due to a mistake I'm now having a never ending nohup process that I have to kill. I started the program execution with: >>nohup... (4 Replies)
Discussion started by: Lydia
4 Replies

3. Shell Programming and Scripting

Find the Pid and Kill the Process after a Few Minutes

hi guys i had written a shell script Display Information of all the File Systems i want to find the pid and kill the process after few minutes.how can i obtain the pid and kill it??? sample.sh df -a >> /tmp/size.log and my cron to execute every minute every hour every day * *... (5 Replies)
Discussion started by: azherkn3
5 Replies

4. Shell Programming and Scripting

Kill a PID using script

Hi, I wrote a script to kill a process id. I am able to kill the PID only if I enter the root password in the middle of the execution because I did not run as root i.e after i run the script from the terminal, instead of killing directly, it is killing only after entering the pass when it... (12 Replies)
Discussion started by: rajkumarme_1
12 Replies

5. Red Hat

How to Force KILL State -D Process/PID?

Hi Expert, I am not able to kill certain user process as root. I have tried using: pkill -u uname skill KILL -u uname kill -9 PID *** I have not using killall yet, since this server has more than 100 users online atm. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND... (1 Reply)
Discussion started by: regmaster
1 Replies

6. Shell Programming and Scripting

How to Kill process with dynamic PID?

Hello, I have problem with killing red5 process running on linux server. As this process is continuously changing its PID so it can't be killed with "kill -9 PID" command. First I used following command to list RED5 process ps aux | grep red5 which showed me root 5832 0.0 0.0 4820 756pts/0... (4 Replies)
Discussion started by: ninadgac
4 Replies

7. UNIX for Dummies Questions & Answers

How to Kill process with dynamic PID?

Hello, I have problem with killing red5 process running on linux server. As this process is continuously changing its PID so it can't be killed with "kill -9 PID" command. First I used following command to list RED5 process ps aux | grep red5 which showed me root 5832 0.0 0.0 4820 756pts/0... (1 Reply)
Discussion started by: ninadgac
1 Replies

8. Shell Programming and Scripting

How to kill a process and its childs given pid and userid

I need to write a shell script which would take 2 arguments pid , userid. Then it should kill all the child process under it. If a child process is not killed then it should wait for 1 minute and should kill. can anybody give me the idea to write it? (0 Replies)
Discussion started by: nani_g
0 Replies

9. Shell Programming and Scripting

pass pid to kill using script

Hi there, i wonder if anyone can help is there any way that i can write a script that will kill all current ftp processes, for example if ps -ef | grep ftp produces 3 active proceses, then I would like to somehow extract the PID for each one and pass that to kill -9 has anybody done this... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

10. UNIX for Dummies Questions & Answers

Script to kill all child process for a given PID

Is there any build in command in unix to kill all the child process for a given process ID ? If any one has script or command, please let me know. Thanks Sanjay (4 Replies)
Discussion started by: sanjay92
4 Replies
Login or Register to Ask a Question

Featured Tech Videos