LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-14-2010, 05:28 PM   #1
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Rep: Reputation: 47
ftp processes, and how to kill them with a cron


if i do a

ps aux | grep ftp

that would show me at least any active ftp connects started with the ftp command, right?

Is there then a way to use that to somehow kill any stuck sessions that are older than an hour?
 
Old 12-14-2010, 07:06 PM   #2
impert
Member
 
Registered: Feb 2009
Posts: 282

Rep: Reputation: 54
It's ugly, but, FWIW:

I just did a little experiment:
Code:
kill -1 `ps aux | grep gedit | sed 's/://' | awk '{ if($10 > 10) print $2 }'`
killed a running gedit.
Something like:
Code:
kill -1 `ps aux | grep ftp | sed 's/://' | awk '{ if($10 > 100) print $2 }'`
might work for you. Please be careful and check that it can't kill anything that matters.
 
1 members found this post helpful.
Old 12-14-2010, 08:30 PM   #3
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
does that kill the process no matter how long its running, or only if its over a certain "age" or idle for so long?
 
Old 12-14-2010, 09:52 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
Code:
# I'd rather have epoch but at least 'ps' has an "elapsed time" switch:
\ps --no-header -o etime,pid,cmd -C ftp | while read TIME PID PROC; do
 # Time measured in days:
 if [ ${#TIME} -ge 9 ]; then echo "pkill -9 ${PID} ("${PROC}" ${TIME})"
 # ...in hours:
 elif [ ${#TIME} -eq 8 -a ${TIME:1:1} -ge 1 ]; then echo "pkill -9 ${PID} ("${PROC}" ${TIME})"
  # just too young to die..:
 else echo "leaving "${PROC}" ${TIME} alone."; fi; done
 
Old 12-15-2010, 06:10 AM   #5
impert
Member
 
Registered: Feb 2009
Posts: 282

Rep: Reputation: 54
Quote:
does that kill the process no matter how long its running, or only if its over a certain "age" or idle for so long?
If you look at a line from ps aux like:
Code:
cam      18555  4.3  0.1 345992  6736 ?        Ssl  11:27   0:43 /usr/bin/pulsea
the tenth field is the time elapsed - 0:43 in the example above. It's in HH:mm format. sed strips the : out of the line, and awk checks to see if the value of the tenth field ($10) is > 100 ie 1 hour 00 minutes, (this was what you asked for) and "prints" the process number (second field, $2) to the kill command, which may or may not kill it. You could put $10 > 200 for 2 hours or $10 > 30 for 30 minutes.
kill -1 often won't kill hung processes - kill -9 will, but without tucking them into bed nicely. kill -3 might be about right. See man kill
However, you're probably better following unSpawn's advice than mine.
 
Old 12-15-2010, 01:00 PM   #6
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
I'm not even sure I know how to use unSpawn's advice haha, i think its way over my head.
 
Old 12-15-2010, 01:48 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
--no-header: don't display ps header
-o: display only these items
etime: elapsed time
pid: the process identifier
cmd: the command line, also called "args"
-C ftp: only select processname "ftp"
| while read TIME PID PROC; do: make the shell read in the three variables and
if [ ${#TIME} -ge 9 ]; then: if the amount of chars in the variable TIME is greater or equal to 9 (think "33-21:09:03" or 33 days, 21 hours etc), then
echo "pkill -9 ${PID} ("${PROC}" ${TIME})": echo what we should do: when unsure display rather than execute things. Else if
elif [ ${#TIME} -eq 8 -a ${TIME:1:1} -ge 1 ]; then: if the amount of chars equals 8 (think "21:09:03"), then
echo "pkill -9 ${PID} ("${PROC}" ${TIME})": again echo what we do. Else we just
else echo "leaving "${PROC}" ${TIME} alone."; fi; done: echo and be done with it.

In short removing the "echo" and outer double quotes from the 'pkill' lines would allow for execution. Like impert stated before you should be aware of what you kill ('ps' doesn't allow for filtering like 'pgrep -f ".*some.*random.*string*" does) so running it manually on the machine and echoing output should show if it works for you.
 
1 members found this post helpful.
Old 12-15-2010, 02:05 PM   #8
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
Thanks for the explanation, and making it safe so i dont blow up a server ;-)

I'll try it out, but it sounds like this is just what i'm looking for.
 
Old 12-16-2010, 07:52 AM   #9
impert
Member
 
Registered: Feb 2009
Posts: 282

Rep: Reputation: 54
@ unSpawn: That's a great little mini-tutorial! Thanks.
 
Old 12-16-2010, 05:46 PM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
Actually I don't do tutorials but you can always read some:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://wooledge.org/mywiki/BashFAQ?a...direct=BashFaq
http://wooledge.org/mywiki/BashPitfalls
http://www.tldp.org/LDP/abs/html/
that's better than I can explain things.
 
  


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
To kill all processes except mine balakrishnay Linux - General 2 11-06-2009 11:27 AM
Use only one "kill" to kill father and child processes Xosen Programming 7 08-28-2008 04:33 AM
Can't kill certain processes.... Basslord1124 Slackware 2 01-29-2008 11:28 AM
how to kill processes htamayo Linux - Newbie 2 09-25-2007 11:27 AM
how to use kill to kill a batch of processes with same name? dr_zayus69 Linux - Software 2 09-03-2005 07:35 PM


All times are GMT -5. The time now is 07:38 PM.

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