<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sheenaustin.com &#187; Linux Scripting</title>
	<atom:link href="http://www.sheenaustin.com/category/linux/linuxscripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sheenaustin.com</link>
	<description>my home on the interweb</description>
	<lastBuildDate>Wed, 21 Jul 2010 01:59:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Script to kill processes older than x days.</title>
		<link>http://www.sheenaustin.com/2008/12/06/script-to-kill-processes-older-than-x-days/</link>
		<comments>http://www.sheenaustin.com/2008/12/06/script-to-kill-processes-older-than-x-days/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 05:24:49 +0000</pubDate>
		<dc:creator>Sheen</dc:creator>
				<category><![CDATA[Linux Scripting]]></category>
		<category><![CDATA[Kill processes older than x days]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[SIGKILL]]></category>
		<category><![CDATA[SIGTERM]]></category>

		<guid isPermaLink="false">http://www.sheenaustin.com/?p=5</guid>
		<description><![CDATA[On a linux farm sometimes there would be a need to kill processes that have been running for extended periods of time and notify the users before doing it. I had such a requirement and had to scour the internet for a lot of long hours to find a proper script to get this job [...]


Related posts:<ol><li><a href='http://www.sheenaustin.com/2009/06/19/active-directory-password-expiry-reminder-email/' rel='bookmark' title='Permanent Link: Active Directory Password Expiry Reminder Email'>Active Directory Password Expiry Reminder Email</a></li>
<li><a href='http://www.sheenaustin.com/2009/05/04/active-directory-audit-script/' rel='bookmark' title='Permanent Link: Active Directory Audit Script'>Active Directory Audit Script</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>On a linux farm sometimes there would be a need to kill processes that have been running for extended periods of time and notify the users before doing it. I had such a requirement and had to scour the internet for a lot of long hours to find a proper script to get this job done but couldn&#8217;t find one. This script was written with ideas various sources and has been tested in an RHEL environment.<span id="more-5"></span></p>
<blockquote>
<p style="TEXT-ALIGN: left">#!/bin/bash</p>
<p style="TEXT-ALIGN: left"># This is a script to identify user processes that have been running</p>
<p style="TEXT-ALIGN: left"># longer than a pre set number of days.</p>
<p style="TEXT-ALIGN: left">　</p>
<p style="TEXT-ALIGN: left"># **** BEGIN CONFIGURABLE PARAMETERS ****</p>
<p style="TEXT-ALIGN: left"># WARNING!! The following values should NOT be NULL.</p>
<p style="TEXT-ALIGN: left"># The script will not work with NULL values.</p>
<p style="TEXT-ALIGN: left"># Path to the logfile</p>
<p style="TEXT-ALIGN: left">LOG=/var/log/prockill</p>
<p style="TEXT-ALIGN: left"># List of groups to which users belong to</p>
<p style="TEXT-ALIGN: left"># The format should be &#8216;group1| group2| group3| group4&#8242;</p>
<p style="TEXT-ALIGN: left">GROUP=&#8217;everyone| utmp| 2000&#8242;</p>
<p style="TEXT-ALIGN: left"># Set the maximum age of a process before it is killed or email notification is sent</p>
<p style="TEXT-ALIGN: left"># If you want to notify users after 5 days and kill processes after 8 days then</p>
<p style="TEXT-ALIGN: left"># AGEKILL=&#8217;1-| 2-| 3-| 4-| 5-| 6-| 7-| 8-&#8217;</p>
<p style="TEXT-ALIGN: left"># AGEMAIL=&#8217;1-| 2-| 3-| 4-| 5-&#8217;</p>
<p style="TEXT-ALIGN: left"># AGE=3</p>
<p style="TEXT-ALIGN: left">AGEKILL=&#8217;1-| 2-| 3-| 4-| 5-| 6-| 7-| 8-| 9-| 10-| 11-| 12-| 13-| 14-| 15-| 16-| 17-| 18-| 19-| 20-| 21-| 22-| 23-| 24-&#8217;</p>
<p style="TEXT-ALIGN: left">AGEEMAIL=&#8217;1-| 2-| 3-| 4-| 5-| 6-| 7-| 8-| 9-| 10-| 11-| 12-| 13-| 14-| 15-| 16-| 17-| 18-| 19-| 20-&#8217;</p>
<p style="TEXT-ALIGN: left">AGE=3</p>
<p style="TEXT-ALIGN: left"># Email Address to send notifications to</p>
<p style="TEXT-ALIGN: left"><a href="mailto:MAIL=user@email.com">MAIL=user@email.com</a></p>
<p style="TEXT-ALIGN: left"># Exceptions: processes that you dont want to kill</p>
<p style="TEXT-ALIGN: left">EXCP=&#8217;ldap&#8217;</p>
<p style="TEXT-ALIGN: left"># **** END CONFIGURABLE PARAMETERS ****</p>
<p style="TEXT-ALIGN: left">　</p>
<p style="TEXT-ALIGN: left"># **** USER NOTIFICATION &amp; PROCESS KILL ****</p>
<p style="TEXT-ALIGN: left"># Check if users have been notified</p>
<p style="TEXT-ALIGN: left">if [ -f /tmp/prockill.notified ]</p>
<p style="TEXT-ALIGN: left">then</p>
<p style="TEXT-ALIGN: left"># **** PROCESS KILL ****</p>
<p style="TEXT-ALIGN: left">if [ "`find /tmp/prockill.notified -mtime +$AGE`" != "" ] # Checking if the file was created x days ago</p>
<p style="TEXT-ALIGN: left">then # If file was created x days ago, its been x days since the users were notified.</p>
<p style="TEXT-ALIGN: left"># Demarc start of log</p>
<p style="TEXT-ALIGN: left">echo &#8220;**** Start List Processes Killed on `date` ****&#8221; &gt;&gt; $LOG</p>
<p style="TEXT-ALIGN: left"># Logging list of processes that match our conditions</p>
<p style="TEXT-ALIGN: left">ps -A -o user,pid,etime,group,args | egrep &#8220;$GROUP&#8221; | egrep -v &#8220;$EXCP&#8221; | grep &#8220;-&#8221; | egrep -v &#8220;$AGEKILL&#8221; &gt;&gt; $LOG</p>
<p style="TEXT-ALIGN: left"># Demarc start of log</p>
<p style="TEXT-ALIGN: left">echo &#8220;**** End List Processes Killed on `date` ****&#8221; &gt;&gt; $LOG</p>
<p style="TEXT-ALIGN: left"># Issuing SIGTERM to give processes a chance to exit gracefully</p>
<p style="TEXT-ALIGN: left">kill -15 `ps -A -o user,pid,etime,group,args | egrep &#8220;$GROUP&#8221; | egrep -v &#8220;$EXCP&#8221; | grep &#8220;-&#8221; | egrep -v &#8220;$AGEKILL&#8221; | cut -c10-15` &gt; /dev/null</p>
<p style="TEXT-ALIGN: left"># Wait for 30 seconds to allow processes to terminate</p>
<p style="TEXT-ALIGN: left">echo &#8220;Waiting for processess to cleanly exit&#8230;&#8221;</p>
<p style="TEXT-ALIGN: left">sleep 30s</p>
<p style="TEXT-ALIGN: left"># Issuing SIGKILL to force all misbehaving processes to die.</p>
<p style="TEXT-ALIGN: left">kill -9 `ps -A -o user,pid,etime,group,args | egrep &#8220;$GROUP&#8221; | egrep -v &#8220;$EXCP&#8221; | grep &#8220;-&#8221; | egrep -v &#8220;$AGEKILL&#8221; | cut -c10-15` &gt; /dev/null</p>
<p style="TEXT-ALIGN: left">rm -rf /tmp/prockill.notified # Clear the notified flag</p>
<p style="TEXT-ALIGN: left">exit 1</p>
<p style="TEXT-ALIGN: left">fi</p>
<p style="TEXT-ALIGN: left">else</p>
<p style="TEXT-ALIGN: left"># **** USER NOTIFICATION ****</p>
<p style="TEXT-ALIGN: left"># Get list of users and number of processes owned by them.</p>
<p style="TEXT-ALIGN: left">ps -A -o user,pid,etime,group | egrep &#8220;$GROUP&#8221; | egrep -v &#8220;$EXCP&#8221; | grep &#8220;-&#8221; | egrep -v &#8220;$AGEEMAIL&#8221; | cut -d&#8217; &#8216; -f1 | uniq -c &gt;&gt; /tmp/prockill.eml</p>
<p style="TEXT-ALIGN: left"># Send email only if the filesize is greater than 0</p>
<p style="TEXT-ALIGN: left">if [ -s /tmp/prockill.eml ]</p>
<p style="TEXT-ALIGN: left">then</p>
<p style="TEXT-ALIGN: left"># Log notification.</p>
<p style="TEXT-ALIGN: left">echo &#8220;**** Start List Notified Processes on `date` ****&#8221; &gt;&gt; $LOG</p>
<p style="TEXT-ALIGN: left">echo &#8220;The following users have the indicated number of proceses that will be killed in 24 hours:&#8221;</p>
<p style="TEXT-ALIGN: left">cat /tmp/prockill.eml &gt;&gt; $LOG</p>
<p style="TEXT-ALIGN: left">echo &#8220;**** End List Notified Processes on `date` ****&#8221; &gt;&gt; $LOG</p>
<p style="TEXT-ALIGN: left"># Email Process list</p>
<p style="TEXT-ALIGN: left">mail -s &#8220;$HOSTNAME &#8211; Old Process Report&#8221; $MAIL &lt; /tmp/prockill.eml</p>
<p style="TEXT-ALIGN: left">rm -rf /tmp/prockill.eml</p>
<p style="TEXT-ALIGN: left">touch /tmp/prockill.notified # Set notified flag</p>
<p style="TEXT-ALIGN: left">exit 1</p>
<p style="TEXT-ALIGN: left">else</p>
<p style="TEXT-ALIGN: left"># Log notification</p>
<p style="TEXT-ALIGN: left">echo &#8220;**** Start List Notified Processes on `date` ****&#8221; &gt;&gt; $LOG</p>
<p style="TEXT-ALIGN: left">echo &#8220;No old processes to kill!&#8221; &gt;&gt; $LOG</p>
<p style="TEXT-ALIGN: left">echo &#8220;**** End List Notified Processes on `date` ****&#8221; &gt;&gt; $LOG</p>
<p style="TEXT-ALIGN: left">exit 1</p>
<p style="TEXT-ALIGN: left">fi</p>
<p style="TEXT-ALIGN: left">fi</p>
</blockquote>


<p>Related posts:<ol><li><a href='http://www.sheenaustin.com/2009/06/19/active-directory-password-expiry-reminder-email/' rel='bookmark' title='Permanent Link: Active Directory Password Expiry Reminder Email'>Active Directory Password Expiry Reminder Email</a></li>
<li><a href='http://www.sheenaustin.com/2009/05/04/active-directory-audit-script/' rel='bookmark' title='Permanent Link: Active Directory Audit Script'>Active Directory Audit Script</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sheenaustin.com/2008/12/06/script-to-kill-processes-older-than-x-days/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
