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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Script for killing a process

Status
Not open for further replies.

maunir

MIS
Apr 30, 2002
62
US
Can anyone help me w/ this. I need to write a perl script for myself that annoys me everytime I log in. I need to kill "xxxx" process everytime I log in. I'm doing it by greping the process and issuing a kill command, I know there is a way you could do it thru perl.
I want Perl to check if the process is running and if it is than kill it and if the process is not running than just print out a statement saying process "xxxx" is not running.
Any help would be appreciated.
 
This is quite achievable, and the steps are pretty much what you're doing now.

1 - run ps and find the pid of the process you want to kill
2 - send that process a signal using kill

#
# Run ps like this:
#
open(PS_OUTPUT,'ps |') || die "Can't run ps\n$!\n";
while(<PS_OUTPUT>){
#
# And then loop through each line of the output from
# ps looking for the process you need. I look for 'ls'
# here, on the basis that this is not likely to do
# any harm.
#
if(/ls/){
#
# ok, found the process, get the PID
#
($pid, $tty, $time, $cmd) = split;
#
# now send it a signal using kill()
#
kill(9, $pid) || die &quot;Kill() failed\n$!\n&quot;;
last;
}
}

Careful what you kill :) Mike
______________________________________________________________________
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Why do you need a perl script to do this. This is very simple as a shell script:
Code:
kill -15 `ps -eo pid,ruser,command | grep <NAME OF PROCESS YOU WANT TO KILL> | grep -v <IDENTIFIER OF PROCESSES THAT YOU DO NOT WANT TO KILL> | awk ' { print $1 } '`

For me I use this to kill my WebSphere server processes. They all start &quot;java&quot;, however there are other java processes that I don't want to kill, hence the &quot;grep -v ...&quot; part.

Why would you want to create a script that annoys you each time you login? Why not just edit your .profile / .login and remove the program that is launched that you don't want/need?
Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top