From: Stu on 6 Jul 2010 15:41 I want to write a script where I compare the date from ps -ef (ie ems 4444306 450796 0 Jul 1 ....... to todays date and if the date is more than N days old I kill the process. Something like the find command - mtime command. Is there a command I can use or some function that anybodys knows about that can compare the Month and day and give back the number of days Using the ps -ef example above Jul 6 - Jul 1 would = 5 days. I am not so worried about the time. For example, If Jul 1 started at 15:00:00 and todays date is Jul 6 14:00:00 that would really be 4 days and 23 hrs. Thanks in advance to all who answer this post.
From: steven_nospam at Yahoo! Canada on 6 Jul 2010 16:13 On Jul 6, 3:41 pm, Stu <beefstu...(a)hotmail.com> wrote: > I want to write a script where I compare the date from ps -ef (ie > > ems 4444306 450796 0 Jul 1 ....... to todays date and if the > date is more > than N days old I kill the process. Something like the find command - > mtime command. > > Is there a command I can use or some function that anybodys knows > about that > can compare the Month and day and give back the number of days > > Using the ps -ef example above Jul 6 - Jul 1 would = 5 days. I am not > so worried > about the time. For example, If Jul 1 started at 15:00:00 and todays > date is Jul 6 14:00:00 that would really be 4 days and 23 hrs. > > Thanks in advance to all who answer this post. You will probably want to be careful using something that randomly aborts PIDs that are older than a set number of days. My server has been up about 30 days and I see processes that (if killed) could cause some big problems. Processes started by init or srcmstr, java agent, things like a third-party software license daemon, Oracle database writers (dbw) or Informix instances (oninit) could have a date from when the server was started and you will likely end up with a lot of irate users. But in the event that you narrow it down to some processes you KNOW are not supposed to be running after X number of days, you may want to look into using the ps command in a different way. Perhaps try this (leave off the # sign): # ps -eo pid,comm,etime or perhaps: # ps -eo pid,etime,args Using those commands let's you pick out the elapsed time, along with the pid to kill and to identify what exactly the command is that is running. Regards, Steve N.
From: Bit Twister on 6 Jul 2010 17:12 On Tue, 6 Jul 2010 12:41:46 -0700 (PDT), Stu wrote: > I want to write a script where I compare the date from ps -ef (ie No problem. Some light reading found here http://tldp.org/LDP/abs/html/index.html > ems 4444306 450796 0 Jul 1 ....... to todays date and if the > date is more > than N days old I kill the process. Parse line for month day, as date to convert to day of year, do a little math. > Using the ps -ef example above Jul 6 - Jul 1 would = 5 days. I am not > so worried > about the time. For example, If Jul 1 started at 15:00:00 and todays > date is Jul 6 14:00:00 that would really be 4 days and 23 hrs. Here is a quick untested kludge to start checking your code. When done testing, change "echo kill" to kill #!/bin/bash # set -x # uncomment line for debugging _yr=$(date %Y) _ps_fn=t.data ps -ef > $_ps_fn while read -r line ; do set -- $line if [ "$1" != "UID" ] ; then # it is not first line of ps command _pid=$2 _day_of_yr=$(date --date="$6 $5 $_yr" +%j) _today_of_yr=$(date --date="today" +%j) let _delta="$_today_of_yr - $_day_of_yr" if [$_today_of_yr -lt $_day_of_yr ] ; then # It's Jan and process let _delta="365 - $_day_of_yr + $_today_of_yr" # started last year fi if [ $_delta > 5 ] ; then echo kill $_pid fi fi done < $_ps_fn
From: Andrew McDermott on 7 Jul 2010 05:39 steven_nospam at Yahoo! Canada wrote: > On Jul 6, 3:41 pm, Stu <beefstu...(a)hotmail.com> wrote: >> I want to write a script where I compare the date from ps -ef (ie >> >> ems 4444306 450796 0 Jul 1 ....... to todays date and if the >> date is more >> than N days old I kill the process. Something like the find command - >> mtime command. >> >> Is there a command I can use or some function that anybodys knows >> about that >> can compare the Month and day and give back the number of days >> > > > You will probably want to be careful using something that randomly > aborts PIDs that are older than a set number of days. My server has (...) > > But in the event that you narrow it down to some processes you KNOW > are not supposed to be running after X number of days, you may want to > look into using the ps command in a different way. Perhaps try this > (leave off the # sign): > > # ps -eo pid,comm,etime > > or perhaps: > > # ps -eo pid,etime,args > > Using those commands let's you pick out the elapsed time, along with > the pid to kill and to identify what exactly the command is that is If you are after a particular process name, you could consider using pgrep along with ps, for example: ps -p $(pgrep -d, -x xterm) -o pid,etime will only list xterm processes. Andrew
|
Pages: 1 Prev: ssh port forwarding questions Next: save your money and open all blocked sites now |