KILLING OLD UNIX PROCESSES

Tags:
Awk
Bash
Bison
Bourne
Grep
KSH
Procmail
Solaris
Unix
Unix Servers
Unix tools
Unix versions
# I want to be able to extract all the oracleprod processes which are 3 days old and kill them. Are there other date functions or methods can I use? I can make a list of processes but how do I compare the date to kill those processes more than 3 days old. This problem is occurring even though I have a regular cron job which kills sessions older than 2 days old in oracle. I am using 8.1.7.4 and Solaris 8. This is where I got stuck. curday=`date +%d` month=`date|cut -d' ' -f2` echo $month $curday ps -ef|grep oracleprod | tr -s " " : | cut -d: -f3,6,7

Answer Wiki

Thanks. We'll let you know when a new response is added.

Hi, one way to solve your problem is to use the “etime” format option of the “ps” command. This option shows the elapsed time since the process was started, in the nice form: [[dd-]hh:]mm:ss

So, the following line will return the list of “oracleprod” processes that have been running for over 1 day[*], sorted by number of running-days (decreasing).

ps -A -o user,pid,etime | grep oracleprod | grep “-” | cut -b1-17 | sort -r -k3

From here I think it is easier to obtain a list of the processes that have been running for over N days. Hope this helps.

[*] Using “etime”, the output lines having the “-” character imply that “dd” is at least 1 => process is running for at least 1 day.
Try it by running the line:
ps -A -o user,pid,etime | grep oracleprod

======================================================================
To kill oracle remote sql connections older than 2 days.

Old Oracle sessions from client needs to be killed on the unix server.
This oracle sessions on unix has the following process name:
“oracle{SID} (LOCAL=NO)” where {SID} is the Oracle database name.

UNIX commands to be used: ps, grep, awk, xargs

UNIX Command that will kill oracle process (LOCAL=NO) from remote connection:

$ ps -A -o etime,pid,user,args|grep -v grep|grep “oracleUAT1 (LOCAL=NO)”|grep -v ^” “|grep -v ^” 1-”|grep -v ^” 2-”|awk ‘{print $2}’|xargs kill -9

Oracle Database name assumed to UAT1
UNIX Command explained:
-A (ps): Lists information for all processes
-o (ps): Prints information according to the format specified
etime (ps): format; the elapsed time of the processes since it started
pid (ps): format; process id
args (ps): command argument of the process
grep: Captures only those rows required as given in the quotes
-v (grep): -v excludes the rows mentioned from the entire process list.
{print $2} (awk): Prints the second column which is the process id
Kill -9 (xargs): Kills the list of process

Explaining the etime format
——————————————————————————————————————–
$ ps -A -o etime,pid,user,args|grep -v grep|grep “oracleUAT1 (LOCAL=NO)”
4-08:58:37 70167 oauat1 oracleUAT1 (LOCAL=NO)
4-08:58:27 70667 oauat1 oracleUAT1 (LOCAL=NO)
4-08:58:37 70163 oauat1 oracleUAT1 (LOCAL=NO)
4-08:58:37 70152 oauat1 oracleUAT1 (LOCAL=NO)
4-08:58:38 70103 oauat1 oracleUAT1 (LOCAL=NO)
4-08:54:33 74602 oauat1 oracleUAT1 (LOCAL=NO)
01:57:33 60521 oauat1 oracleUAT1 (LOCAL=NO)
4-08:58:25 70699 oauat1 oracleUAT1 (LOCAL=NO)
4-08:58:26 70670 oauat1 oracleUAT1 (LOCAL=NO)
4-08:58:37 70142 oauat1 oracleUAT1 (LOCAL=NO)
04:06:44 83133 oauat1 oracleUAT1 (LOCAL=NO)
01:18 35438 oauat1 oracleUAT1 (LOCAL=NO)
4-08:58:39 70055 oauat1 oracleUAT1 (LOCAL=NO)
——————————————————————————————————————–

Note: the etime has the following format: “dd-hh:mm:ss”
where:
dd – is the number of days elapsed since the process started
hh – is hours; mm – is minutes and ss – is seconds.
Then: “4-08:58:37” means this process is running for 4 days; 8 hours; 58 minutes and 37 seconds.

Hence if you want to exclude the current then grep for the first column having double spaces at the start.
For ex:
For excluding current day process…. grep “ “ –> note double spaces
For excluding process of a day old… grep “ 1-“ –> note single space before 1 followed by hyphen “-“
For excluding process of 2 days old.. grep “ 2-“ –> note single space before 2 followed by hyphen “-“

– Sandeep S. Salve.
======================================================================

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Voltaire
    (yesterday I was a bit in a hurry...) This will give you exactly the list of all oracleprod processes running for 3 or more days (respect the spaces embedded in the egrep expression): ps -A -o user,pid,etime | grep oracleprod | egrep -v " 1-| 2-" or, if you want only the list of PIDs: ps -A -o user,pid,etime | grep oracleprod | egrep -v " 1-| 2-" | cut -c10-15
    0 pointsBadges:
    report
  • Sandeepssalve
    To kill oracle remote sql connections older than 2 days. Old Oracle sessions from client needs to be killed on the unix server. This oracle sessions on unix has the following process name: "oracle{SID} (LOCAL=NO)" where {SID} is the Oracle database name. UNIX commands to be used: ps, grep, awk, xargs UNIX Command that will kill oracle process (LOCAL=NO) from remote connection: $ ps -A -o etime,pid,user,args|grep -v grep|grep "oracleUAT1 (LOCAL=NO)"|grep -v ^" "|grep -v ^" 1-"|grep -v ^" 2-"|awk ‘{print $2}’|xargs kill -9 Oracle Database name assumed to UAT1 UNIX Command explained: -A (ps): Lists information for all processes -o (ps): Prints information according to the format specified etime (ps): format; the elapsed time of the processes since it started pid (ps): format; process id args (ps): command argument of the process grep: Captures only those rows required as given in the quotes -v (grep): -v excludes the rows mentioned from the entire process list. {print $2} (awk): Prints the second column which is the process id Kill -9 (xargs): Kills the list of process Explaining the etime format -------------------------------------------------------------------------------------------------------------------- $ ps -A -o etime,pid,user,args|grep -v grep|grep "oracleUAT1 (LOCAL=NO)" 4-08:58:37 70167 oauat1 oracleUAT1 (LOCAL=NO) 4-08:58:27 70667 oauat1 oracleUAT1 (LOCAL=NO) 4-08:58:37 70163 oauat1 oracleUAT1 (LOCAL=NO) 4-08:58:37 70152 oauat1 oracleUAT1 (LOCAL=NO) 4-08:58:38 70103 oauat1 oracleUAT1 (LOCAL=NO) 4-08:54:33 74602 oauat1 oracleUAT1 (LOCAL=NO) 01:57:33 60521 oauat1 oracleUAT1 (LOCAL=NO) 4-08:58:25 70699 oauat1 oracleUAT1 (LOCAL=NO) 4-08:58:26 70670 oauat1 oracleUAT1 (LOCAL=NO) 4-08:58:37 70142 oauat1 oracleUAT1 (LOCAL=NO) 04:06:44 83133 oauat1 oracleUAT1 (LOCAL=NO) 01:18 35438 oauat1 oracleUAT1 (LOCAL=NO) 4-08:58:39 70055 oauat1 oracleUAT1 (LOCAL=NO) -------------------------------------------------------------------------------------------------------------------- Note: the etime has the following format: “dd-hh:mm:ss” where: dd - is the number of days elapsed since the process started hh – is hours; mm - is minutes and ss – is seconds. Then: “4-08:58:37” means this process is running for 4 days; 8 hours; 58 minutes and 37 seconds. Hence if you want to exclude the current then grep for the first column having double spaces at the start. For ex: For excluding current day process.... grep “ “ --> note double spaces For excluding process of a day old… grep “ 1-“ --> note single space before 1 followed by hyphen “-“ For excluding process of 2 days old.. grep “ 2-“ --> note single space before 2 followed by hyphen “-“ -- Sandeep S. Salve.
    25 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following