LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Please help me script (https://www.linuxquestions.org/questions/linux-newbie-8/please-help-me-script-628067/)

hashbangbinbash 03-14-2008 01:10 PM

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?

custangro 03-14-2008 01:16 PM

Quote:

Originally Posted by hashbangbinbash (Post 3088753)
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


osor 03-14-2008 01:44 PM

Some systems also have the utilites pkill and killall.

konsolebox 03-15-2008 12:31 AM

Quote:

Originally Posted by hashbangbinbash (Post 3088753)
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


hashbangbinbash 03-15-2008 06:52 AM

Thanks for this, nice solutions and correction there, very helpful indeed.


All times are GMT -5. The time now is 09:50 AM.