- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- I'm looking for a perl script to kill a process
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-25-2005 09:51 AM
тАО07-25-2005 09:51 AM
has someone a perl script to look if a process
is running and if it is, then kill it ?
kind regards
chris
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-25-2005 10:41 AM
тАО07-25-2005 10:41 AM
Solutionperl -e '$cc = system("ps -ef|grep PATTERN|grep -v grep| awk {'print $2'}|xargs kill"); exit($cc);'
take it with a lot of salt as I am not an expert in PERL programming. Know just nuff to get myself in trouble.
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-25-2005 09:17 PM
тАО07-25-2005 09:17 PM
Re: I'm looking for a perl script to kill a process
maybe the best (most Perlish) option would be if you installed Proc::ProcessTable from CPAN.
http://search.cpan.org/~durist/Proc-ProcessTable-0.40/ProcessTable.pm
With this module you can scan your system's process table at your heart's content without having to call external programs like ps.
e.g.
Here I retrieve the PID of the dsmc process (i.e. ADMS client)
$ perl -MProc::ProcessTable -le '$pid=(grep $_->{cmd}=~/dsmc/,@{Proc::ProcessTable->new->table})[0]->{pid};print$pid'
Instead of printing you would send it any signal.
You can use Perl's built-in kill() function to this end.
e.g.
kill 15 => $pid;
To send it a SIGTERM.
You can send a whole bunch of processes (you have retrieved from the proc table before) a signal in one go if you pass kill() a list of PIDs.
See "perldoc -f kill"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-25-2005 09:21 PM
тАО07-25-2005 09:21 PM
Re: I'm looking for a perl script to kill a process
if you want to avoid the hassle of installing the extra Proc::ProcessTable you can of course also hand the parsing over to the external ps command.
e.g.
$ perl -le 'chomp($pid=qx(UNIX95= /usr/bin/ps -o pid= -C dsmc));print$pid'
13557
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-25-2005 10:26 PM
тАО07-25-2005 10:26 PM
Re: I'm looking for a perl script to kill a process
kill -9 `ps -ef | grep 'PATTERN' | awk '!/grep/ { print $2 }'`
or
PID=$(ps -ef | grep -v grep | grep -q 'PATTERN')
if [[ $PID != "" ]]
then
kill -15 $PID
fi
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-25-2005 11:34 PM
тАО07-25-2005 11:34 PM
Re: I'm looking for a perl script to kill a process
*IF* you use backticked ps (or system ()), *PLEASE* use perl's builtin functions to process further
# perl -e 'kill 15,map{m/^\s*\w+\s+(\d+)/;$1}grep/pattern/,`ps -ef`'
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-26-2005 01:38 AM
тАО07-26-2005 01:38 AM
Re: I'm looking for a perl script to kill a process
Perls are nice, ksh is a gem:o)
I wrote the attached script some time ago to repeatedly kill self-respawning processes that were running amok & getting in the way.
It works, and is well commented, but do please read the warning.
Share And Enjoy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2005 10:24 AM
тАО07-28-2005 10:24 AM
Re: I'm looking for a perl script to kill a process
@prokura
" *IF* you use backticked ps (or system ()), *PLEASE* use perl's builtin functions to process further "
can you pls give more details ?
and this won't work on my system running as root from command line:
# perl -e 'kill 15,map{m/^\s*\w+\s+(\d+)/;$1}grep/ntop/,`ps -ef`'
ps: Process environment requires procfs(5)
or do I something wrong ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2005 04:46 PM
тАО07-28-2005 04:46 PM
Re: I'm looking for a perl script to kill a process
(or Solaris, albeit a bit more limitted).
Are you trying this on a Linux box?
This should also explain your usage of killall in another of your threads.
Btw, have you noticed that ITRC also maintains a Linux forum?
Chances for a satisfactory answer there might be higher.