Hello and welcome to LQ, hope you like it here.
I would like to figure out the processes that are idle for more than 5 hours.
I rephrase that as "processes that are and older than 5 hours and have idle state right now" since I don't know how to track idle vs running state w/o keeping per-process, per-timeslice statistics. Only requirement of Bash kludge below is 'procstat' which you can get at Senko.net because else I'll have to look at the kernel source for /proc/PID/stat fields. Hopefully I overlooked something essential and somebody will present a qualitatively better way. YMMV(VM):
Code:
#!/bin/sh --
AGE=$[60*60*5]; echo -en "Aged ${AGE}: "; /bin/ps ax -eostate,pid --sort=state 2>/dev/null|\
while read STATE PID; do case "${STATE}" in S*) STARTTIME=(`procstat -t "${PID}" 2>/dev/null|\
while read LINE; do case "${LINE}" in Started*) echo "${LINE}";; esac; done`); if [ -n \
"${STARTTIME[2]}" ]; then STARTTIME=${STARTTIME[2]//.*/}; STARTTIME=`date +%s --date=\
"${STARTTIME}"`; [ $[`date +%s`-${STARTTIME}] -gt ${AGE} ] && echo "${PID}"; fi;; esac; done|\
xargs; exit 0