LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 05-27-2012, 04:05 AM   #1
deepak_message
Member
 
Registered: Oct 2007
Posts: 175

Rep: Reputation: 17
I need to kill the processes older then then one day


I need to kill the processes in redhat which are older then one day for the user only Not for the system processess.

Actually, I need to kill the process for the user who have taken session for last one day.


Script will show the list first then aks to kill (yes/No)

Can you please help, who have the good scripting skill.


Please help

Last edited by deepak_message; 05-27-2012 at 04:12 AM.
 
Old 05-27-2012, 11:49 AM   #2
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Rep: Reputation: 73
You can start something like this:

Code:
for proc in $(find /proc -maxdepth 1 -type d -name "[0-9]*" -mmin +1440); do 
   pid=$(basename ${proc})
   user=$(stat -c %U ${proc})
   if [ "${user}" = "root" ]; then
      continue
   else
      kill ${pid}
   fi
done
With this you can find all processes which are one or more days older and then you can check if they belong or not to use root, if they don't you can kill them.

And using this little idea you can do whatever you want or need.
 
Old 05-27-2012, 04:03 PM   #3
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Rep: Reputation: 73
Actually this script is not a totally good idea, coz it will kill all processes which are not started with root user, and in most cases there will be some processes running with different user then root which you shouldn't kill.

Code:
for proc in $(find /proc -maxdepth 1 -type d -name "[0-9]*" -mmin +1440); do 
   pid=$(basename ${proc})
   uid=$(stat -c %u ${proc})
   if [ ${user} -qe 500 ]; then
      kill ${pid}
   fi
done
So this new example will kill all processes which belong to users with a id bigger or equal to 500, which shouldn't be any "special" system user.
 
Old 05-27-2012, 06:52 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Use ALL-CAPS variable names for readability, use "while" loop instead, speed up by excluding root-owned processes, speed up by avoiding basename and stat, derive value of 500 from /etc/login.defs, check shell, it's not "-qe" but "-ge" BTW and allow process to end before killing it:
Code:
_MIN_UID=$(awk '/UID_MIN/ {print $2}' /etc/login.defs); _UID_LIST=$(awk -F: '{ if ($7 == "/bin/bash" && $3 >= 500 ) { print $3}}' /etc/passwd)
find /proc -maxdepth 1 -type d -name "[0-9]*" -mmin +1440 -not -uid 0 -printf "%f %U\n" | while read _PID _UID; do
 LIST=${#_UID_LIST}; CHECK=${_UID_LIST//${_UID}/}; CHECK=${#CHECK}; [ ${_UID} -ge ${_UID_MIN:=500} -a "${LIST}" -ne "${CHECK}" ] \
&& { kill -15 ${_PID}; sleep 3s; kill -9 ${_PID}; }; done
 
Old 05-28-2012, 02:10 AM   #5
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Rep: Reputation: 73
Yep, it's a typo there:

Code:
${user} -qe 500
suppose to be:

Code:
${user} -ge 500
 
Old 05-28-2012, 03:30 AM   #6
deepak_message
Member
 
Registered: Oct 2007
Posts: 175

Original Poster
Rep: Reputation: 17
Really, you guys are coder... Thank you so much to all of you..

But I have one more request please ... if you can help that would be helpful fro me...

I have required another script, which will kill only more then one hour old for specific sshd process for all of the user including root also

Suppose, I need to kill only sshd process, which has time got over more then an hour to all of the user inculding root also.
 
Old 05-28-2012, 03:59 AM   #7
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
It would be better to start a new thread for a new question, but I guess this is just an extension.

First, use
Code:
LANG=C LC_ALL=C ps -o pid= -o etime= -o cmd= -C sshd
to obtain the PID, elapsed time (age), and command line for each SSH process. The elapsed time is MM:SS if less than a hour, HH:MM:SS if less than a day, and DD-HH:MM:SS if a full day or longer.

Then, you need to ignore the process which matches command /usr/sbin/sshd, because that is the listening server, not an SSH connection. You also need to ignore the processes that have not run long enough yet, and send a termination signal (TERM or KILL) to the processes that have run long enough.

Once again, I'd use awk for this. Using awk in a Bash script:
Code:
export LC_ALL=C
export LANG=C

PIDS=$(ps -o pid= -o etime= -o cmd= -C sshd | awk '#
    # Remove leading whitespace if necessary.
    ($1 ~ /^[\t\v\f ]*$/) {
        sub(/^[\t\v\f ]*/, "", $0)
    }

    # Skip the listening SSH server process.
    ($3 == "/usr/sbin/sshd") { next }

    # Parse the time.
    {
        # To calculate the elapsed time in seconds, split it into fields.
        timetemp = $2
        gsub(/[^0-9]+/, " ", timetemp)
        n = split(timetemp, timepart)

        # Compute the number of seconds based on n fields in timepart.
        if (n >= 4)
            time = timepart[4] + 60*timepart[3] + 60*60*timepart[2] + 24*60*60*timepart[1]
        else if (n == 3)
            time = timepart[3] + 60*timepart[2] + 60*60*timepart[1]
        else
            time = timepart[2] + 60*timepart[1]

        # Ignore if less than a hour, 3600 seconds.
        if (time < 3600)
            next

        # print the PID.
        print $1
    }')

# Kill the processes that are old enough.
[ -n "$PIDS" ] && kill -KILL $PIDS
Note that AIX ps might use different flags. Also, I have not tested the code, so there might be typos or bugs lurking in there; you need to carefully check it first.
 
  


Reply



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
find one day older files kirukan Linux - Newbie 3 05-31-2011 10:37 PM
Use only one "kill" to kill father and child processes Xosen Programming 7 08-28-2008 03:33 AM
Can't kill certain processes.... Basslord1124 Slackware 2 01-29-2008 10:28 AM
how to kill processes htamayo Linux - Newbie 2 09-25-2007 10:27 AM
how to use kill to kill a batch of processes with same name? dr_zayus69 Linux - Software 2 09-03-2005 06:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:59 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
Open Source Consulting | Domain Registration