Advertisement
devinteske

psdiff3.sh

Aug 15th, 2014
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.29 KB | None | 0 0
  1. #!/bin/sh
  2. ############################################################ IDENT(1)
  3. #
  4. # $Title: Script to show running processes started prior to modification $
  5. #
  6. ############################################################ INCLUDES
  7.  
  8. BSDCFG_SHARE="/usr/share/bsdconfig"
  9. . "$BSDCFG_SHARE/common.subr" || exit 1
  10. f_include "$BSDCFG_SHARE/strings.subr"
  11.  
  12. ############################################################ MAIN
  13.  
  14. #
  15. # Initialize list of pids we care about
  16. # NB: We only care about pids with ucomm values copacetic to which(1)
  17. #
  18. PIDS=
  19.  
  20. #
  21. # Ask ps(1) for a list of pids and their associated short command names.
  22. # Generate $PIDS as space-separated list of process IDs whose ucomm represents
  23. # a utility name accessible via $PATH expansion. For each PID, set variables
  24. # pidcmd_$pid containing the ucomm value (short command name).
  25. #
  26. exec <<EOI
  27. $( ps ax -o pid= -o ucomm= )
  28. EOI
  29. while read pid ucomm; do
  30.     f_which "$ucomm" pidcmd || continue
  31.     [ -r "$pidcmd" ] || continue
  32.     PIDS="$PIDS $pid"
  33.     setvar pidcmd_$pid "$pidcmd"
  34. done
  35. exec <&1
  36.  
  37. #
  38. # Ask ps(1) for the last start (lstart) date/time of each pid in $PIDS
  39. #
  40. exec <<EOI
  41. $( ps -o pid= -o lstart= -p $PIDS )
  42. EOI
  43. while read pid lstart; do
  44.     setvar piddate_$pid "$( date -j -f %c "$lstart" +%s )"
  45. done
  46. exec <&1
  47.  
  48. #
  49. # Ask stat(1) for the modification time (epoch) for each running utility.
  50. # Modification times are stored in ondiskdate_$pid.
  51. #
  52. pidcmd_list=
  53. for pid in $PIDS; do
  54.     f_getvar pidcmd_$pid pidcmd
  55.     f_shell_escape "$pidcmd" _pidcmd
  56.     pidcmd_list="$pidcmd_list '$_pidcmd'"
  57. done
  58. exec <<EOI
  59. $( eval stat -f %m $pidcmd_list )
  60. EOI
  61. set -- $PIDS
  62. while read ondiskdate rest_ignored; do
  63.     pid="$1"
  64.     [ "$pid" ] || break
  65.     setvar ondiskdate_$pid "$ondiskdate"
  66.     shift 1 # pid
  67. done
  68. exec <&1
  69.  
  70. #
  71. # Compare on-disk epoch(s) to running epoch(s) (lstart)
  72. #
  73. for pid in $PIDS; do
  74.     f_getvar ondiskdate_$pid ondiskdate || continue
  75.     f_getvar piddate_$pid piddate || continue
  76.     f_getvar pidcmd_$pid pidcmd || continue
  77.     echo "Comparing $ondiskdate and $piddate for pid $pid $pidcmd"
  78.     [ $ondiskdate -gt $piddate ] &&
  79.         echo "ALERT! $ondisk needs to be reloaded!"
  80. done
  81.  
  82. exit 0
  83.  
  84. ################################################################################
  85. # END
  86. ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement