pro check_oldproc, hours_age=hours_age, debug=debug, $
   kill_pattern=kill_pattern, user=user, purge=purge, nomail=nomail
;+
;   Name: check_oldprocess
;
;   Purpose: check old processes
;
;   Calling Sequence:
;      check_oldprocess 
;
;   History: 
;      15-Feb-1995 (SLF) 
;
;   Method - look at queue of old chk_process logs and flag/remove old
;            hung jobs (generally called from check_process.pro)
;-
if not keyword_set(user) then user=get_user()		; default owner
if not keyword_set(hours_age) then hours_age=6
mailit=1-keyword_set(nomail)
debug=keyword_set(debug)

prefix='chk_proc_'
logs=file_list('$DIR_SITE_LOGS',prefix + '??????.????')

if logs(0) eq '' then begin
   message,/info,"No old check_process logs..."
   return
endif

break_file,logs,log,path,file,ext,ver
files=file + ext + ver
fids=strmid(files,strlen(prefix),50)

times=anytim2ints(fid2ex(fids))			; times of old logs
curtime=anytim2ints(!stime)
oldtime=timegrid(curtime,hours=-(hours_age))	; 

ssnew=(tim2dset(times,curtime))(0)			; current log
ssold=(tim2dset(times,oldtime))(0)			; closest log

if ssnew eq ssold then begin
   message,/info,"New and Old logs are the same (only 1 log??)
   return
endif


newdata=rd_tfile(logs(ssnew),10,/compress,nocomment='ps:') 
olddata=rd_tfile(logs(ssold),10,/compress,nocomment='ps:')

which_new=where(newdata(0,*) eq user,ncnt)
if ncnt eq 0 then begin
   message,/info,"No jobs for user: " + user
   return
endif

newpids=reform(newdata(1,which_new))
newjobs=reform(newdata(9,which_new))
newstring= user + newpids

which_old=where(olddata(0,*) eq user,ocnt)
if ocnt eq 0 then begin
   message,/info,"No old jobs for user: " + user
   return
endif

oldpids=reform(olddata(1,which_old))
oldjobs=reform(olddata(9,which_old))
oldstring= user + oldpids 

chk=where_arr(newstring,oldstring,mcnt)

if mcnt eq 0 then begin
   message,/info,"No old processes..."
   return
endif

badpids=newpids(chk)
badjobs=newjobs(chk)

mailfile=concat_dir('$DIR_SITE_LOGS','check_oldproc.mail')
mess=["check_oldprocess run at: " + systime(),"",		$
      "   on host: " + get_host(),"",				$
      strjustify(["Comparing New Log: " + logs(ssnew), 		$
                  "To Old Log: " + logs(ssold)],/right),""]      

mess=[mess,"The following jobs have been around a while...", "" , $
   user + " " + strjustify(badpids) + " " + strjustify(badjobs),""]

file_append,mailfile,mess,/new
prstr,rd_tfile(mailfile)

killit='kill -9 '
for i=0,n_elements(kill_pattern)-1 do begin
   message,/info,"Looking for " + kill_pattern(i) + " jobs..."
   ss=where(strpos(badjobs,kill_pattern(i)) ne -1, pcnt)

   if pcnt gt 0 then begin
      pids=arr2str(badpids(ss),' ')
      mess=["Attempting to kill " + kill_pattern(i) + " jobs...","",killit + pids]
      file_append,mailfile,mess
      spawn,killit + pids, result
      if result(0) ne '' then $
         file_append,mailfile,["Kill command result...",result]
   endif else message,/info,"None found..."
endfor

if mailit then mail, /self, file=mailfile, Subj="CHECK_OLDPROCESS info...", $
   /no_defsubj

if keyword_set(purge) then file_purge,logs,keep=purge

if debug then stop
return
end

