I want to write a script which can generate a kill list for killing process, program name start with f60.., which have been running for more than 8 hours, the list output should looks like:
ps aux | awk '$11 ~ /(^|\/)f60/ { split ($9,t,/:/); if (t[1] > 8) print $2 }'
and it return:
4468962
and I grep the process:
root@lkmprod11i:/home/oracle/victor> ps -ef|grep 4468962
appl115 4468962 6123696 0 09:05:04 - 0:00 f60runm webfile=5,27368,lkmprod11i_9002_PROD11i
root 9077560 10519380 0 15:15:38 pts/0 0:00 grep 4468962
The program start time is 09:05 and the system current time is 15:15, it is less then 8 hours long, it should not be returned, can you please help and review? Thanks!
On the other hand, I have to add one more condition to it, if the process id or parent process id is 1 then don't let it returned, how to do that?
The PPID is not visible in the ps aux output, you might need different options to get that column also. Then it should be a trivial problem to add '&& $3 > 1' with the correct field number of course before the opening curly.
You want the [f] with the square brackets, otherwise you risk to kill the script itself (in theory, mostly, I guess, in the current scenario; but what if you repurpose the script later? Better make it right).
ps aux | awk '$11 ~ /(^|\/)f60/ { split ($9,t,/:/); if (t[1] > 8) print $2 }'
and it return:
4468962
and I grep the process:
root@lkmprod11i:/home/oracle/victor> ps -ef|grep 4468962
appl115 4468962 6123696 0 09:05:04 - 0:00 f60runm webfile=5,27368,lkmprod11i_9002_PROD11i
root 9077560 10519380 0 15:15:38 pts/0 0:00 grep 4468962
The program start time is 09:05 and the system current time is 15:15, it is less then 8 hours long, it should not be returned, can you please help and review? Thanks!
Check man ps for an option which list the actual run time. (Apparently you want wall clock time, not CPU time -- that might not be directly available. Then you need to do some time calculations in awk, which doesn't sounds like much fun.)
The 3rd column is the elapsed time and in Format of [[ dd-]hh:]mm:ss, is it possible I only want it to return the PIDs for processes elapsed longer than 8 hours?
if I issue :
ps -ef -o "user pid ppid cpu etime tty time args" | awk '$8 ~ /(^|\/)[f]60/ && $3>1 { print $2 }'
it returns all the PIDs regardless of elapsed time, can you please advise how to modify the script and let it return only processes elapsed longer than 8 hours?
It prints nothing at all for me, the processes are not called f60 and $3 is not greater than 1.
But to focus on the elapsed time, you could say $5 ~ /[0-9]-/ for anything with more than one day (trivially more than 8h then) and for the remaining ones, split($5, t, /:/) and print if t[1] > 8.