LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-14-2008, 02:10 PM   #1
hashbangbinbash
Member
 
Registered: Mar 2008
Posts: 106

Rep: Reputation: 16
Please help me script


I want to make a script that lifts a PIDs for rtorrent processes, so far I got this:

netstat -tanpu | grep "rtorrent"

gives me a nice list of 7 columns, the PIDs are buried at the end of the 4th column,

tcp 0 0 213.321.40.162:6942 85.235.218.114:62649 ESTABLISHED 28387/rtorrent

I want to lift off the PID 6942 for instance, and then tell system to kill that process, but sed makes my head hurt. How might you do it?
 
Old 03-14-2008, 02:16 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,971
Blog Entries: 1

Rep: Reputation: 208Reputation: 208Reputation: 208
Quote:
Originally Posted by hashbangbinbash View Post
I want to make a script that lifts a PIDs for rtorrent processes, so far I got this:

netstat -tanpu | grep "rtorrent"

gives me a nice list of 7 columns, the PIDs are buried at the end of the 4th column,

tcp 0 0 213.321.40.162:6942 85.235.218.114:62649 ESTABLISHED 28387/rtorrent

I want to lift off the PID 6942 for instance, and then tell system to kill that process, but sed makes my head hurt. How might you do it?
Try to "pgrep" for it instead

Code:
pgrep rtorrent
It will return a PID (or a group of pids). Then you can kill it with your basic kill command

Code:
kill -INT $(pgrep rtorrent)
Or if you had a group of pids...

Code:
for pids in $(pgrep rtorrent)
do
kill -INT ${pids}
done
 
Old 03-14-2008, 02:44 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 75
Some systems also have the utilites pkill and killall.
 
Old 03-15-2008, 01:31 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by hashbangbinbash View Post
tcp 0 0 213.321.40.162:6942 85.235.218.114:62649 ESTABLISHED 28387/rtorrent

I want to lift off the PID 6942 for instance, and then tell system to kill that process, but sed makes my head hurt. How might you do it?
isn't 6942 a port and not a PID? just in case 28387 is the pid, using your netstat command, you can have:
Code:
PID=$(netstat -tanpu | sed -n -e '/rtorrent/s@.*\([0-9]\+\)/rtorrent.*@\1@p')
[ -n "$PID" ] && kill -s SIGKILL $PID

Last edited by konsolebox; 03-15-2008 at 01:33 AM.
 
Old 03-15-2008, 07:52 AM   #5
hashbangbinbash
Member
 
Registered: Mar 2008
Posts: 106

Original Poster
Rep: Reputation: 16
Thanks for this, nice solutions and correction there, very helpful indeed.

Last edited by hashbangbinbash; 03-15-2008 at 07:55 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 12:33 PM
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 05:19 AM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 11:48 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 05:21 PM


All times are GMT -5. The time now is 08:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration