nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Script To Kill process if time and memories of more than 30%

This is a discussion on Script To Kill process if time and memories of more than 30% within the Shell scripting forums, part of the Development/Scripting category; Hello. The script which kills processes which occupy processor time and memories of more than 30% ( for example ) ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 19-04-2009, 09:15 AM
Junior Member
User
 
Join Date: Apr 2009
OS: SUSE 11.0
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
lliriK is on a distinguished road
Default Script To Kill process if time and memories of more than 30%

Hello. The script which kills processes which occupy processor time and memories of more than 30% ( for example ) is necessary to me. I ask the help.
Reply With Quote
  #2 (permalink)  
Old 19-04-2009, 04:39 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,707
Thanks: 11
Thanked 244 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

There is no command exits which can give you ready to use output, however shell pipes can be used along with ps command. To know the top 10 memory consuming process use the following commands:

Code:
ps auxf | sort -nr -k 4 | head -10
Top 10 cpu consuming process
Code:
ps auxf | sort -nr -k 3 | head -10
Now you can write something as follows to print process named that are using 30% memory:
Code:
ps auxf | sort -nr -k 4 | head -10 | awk '{ if ( $4 > 30) print $1}'
And following will find and kill them:
Code:
P=$(ps auxf | sort -nr -k 4 | head -10 | awk '{ if ( $4 > 30) print $1}')
killall $P
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 20-04-2009, 11:38 AM
Junior Member
User
 
Join Date: Apr 2009
OS: SUSE 11.0
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
lliriK is on a distinguished road
Default

Thank, very mach (Спасибо большое)
Code:
#!/bin/bash

max_cpu=$(ps -eo user,pid,pcpu,pmem --no-headers | awk '{ if ( $3 > 30) print $2}')
max_mem=$(ps -eo user,pid,pcpu,pmem --no-headers | awk '{ if ( $4 > 10) print $2}')
   echo $max_cpu
   echo $max_mem
  kill -9 $max_mem
kill -9 $max_cpu

Last edited by nixcraft; 20-04-2009 at 12:51 PM.
Reply With Quote
  #4 (permalink)  
Old 20-04-2009, 06:33 PM
Junior Member
User
 
Join Date: Apr 2009
OS: SUSE 11.0
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
lliriK is on a distinguished road
Default

now there are a couple of new requirements to the program.
1.chtoby program to monitor the process continuously
2.imena users who are not subject to the control
3.nabor process ID to be ignored
4. a signal sending process
5.kuda output logs (default is standard output, or in a specified file)
6. on a parameter monitor process
who can help, it is simply oromnoe thanks =)
Reply With Quote
  #5 (permalink)  
Old 22-04-2009, 07:45 AM
Junior Member
User
 
Join Date: Apr 2009
OS: SUSE 11.0
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
lliriK is on a distinguished road
Default

Will no one help me in this matter?
Reply With Quote
  #6 (permalink)  
Old 22-04-2009, 07:51 AM
Junior Member
User
 
Join Date: Apr 2009
OS: SUSE 11.0
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
lliriK is on a distinguished road
Default

now there are a couple of new requirements to the program.
1. program to monitor the process continuously
2. the names of the users who are not subject to the control
3. set of identifiers to be ignored
4. a signal sending process
5. where to put logs (default is standard output, or in a specified file)
6. on a parameter monitor process
who can help, it is simply oromnoe thanks =)
Reply With Quote
  #7 (permalink)  
Old 22-04-2009, 12:05 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,707
Thanks: 11
Thanked 244 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Quote:
Originally Posted by lliriK View Post
1. program to monitor the process continuously
Use cron jobs to run script every 5 min.
Quote:
Originally Posted by lliriK View Post
2. the names of the users who are not subject to the control
You can use grep to filter out certain users.
Quote:
Originally Posted by lliriK View Post
3. set of identifiers to be ignored
????
Quote:
Originally Posted by lliriK View Post
4. a signal sending process
???
Quote:
Originally Posted by lliriK View Post
5. where to put logs (default is standard output, or in a specified file)
Use logger command to write log to any file in /var/log directory.
Quote:
Originally Posted by lliriK View Post
6. on a parameter monitor process
???

??? === explains in more details...
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #8 (permalink)  
Old 22-04-2009, 09:45 PM
Junior Member
User
 
Join Date: Apr 2009
OS: SUSE 11.0
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
lliriK is on a distinguished road
Default

3 is a view that there is a list of PID, which would ignorirovatsya program and will not be killed
4 a signal is transmitted to a process which should be killed (one of the signals that kill - 9)
6 on a parameter will kill the process (excessive use of memory or processor)
All items must be combined into one script
Reply With Quote
  #9 (permalink)  
Old 25-04-2009, 09:58 AM
Junior Member
User
 
Join Date: Apr 2009
OS: SUSE 11.0
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
lliriK is on a distinguished road
Default

assistance from experts would be? )))))
Reply With Quote
  #10 (permalink)  
Old 28-04-2009, 12:43 AM
Junior Member
User
 
Join Date: Apr 2009
OS: Ubuntu
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
fcojocaru is on a distinguished road
Default

Use the commands you already got to place all heavy consumers in a file containing all data relevant to you like: name|pid|users.Process this file with a set of sed -i -e "/to_remove/d" $process_list_file. Define a config file containing the desired exceptions in variable ( excluded_users="john doe jane rex" for eg ) and include it in your script.Use for loops to get rid of them from the file.Extract from what's left the relevant info(pid?) and send it to kill.
Reply With Quote
Reply

Tags
awk , kill , killall , linux , pkill , shell scripting , unix


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Shell script to kill process Automatically after 3 days darknet Shell scripting 4 02-11-2009 12:40 AM
Killing of a process and send a mail if the process doesnot come up within 2 minutes Prince89 Shell scripting 1 11-02-2008 12:39 PM
kernel panic -not syncing attempt to kill init jhn_daz@yahoo.com Linux software 3 26-11-2007 04:16 PM
Activate NIC at boot time lacloai Linux software 5 13-10-2007 07:10 PM
iptables -m time Joe Linux software 1 14-03-2006 12:53 PM


All times are GMT +5.5. The time now is 10:42 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38