Advertisement
devinteske

psdiff2.sh

Aug 15th, 2014
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.43 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. if [ -f "$BSDCFG_SHARE/common.subr" -a -r "$BSDCFG_SHARE/common.subr" ]; then
  10.     . "$BSDCFG_SHARE/common.subr" || exit 1
  11.     f_include "$BSDCFG_SHARE/strings.subr"
  12. fi
  13.  
  14. ############################################################ FUNCTIONS
  15.  
  16. if [ ! "$_COMMON_SUBR" ]; then
  17. f_which() # Taken from /usr/share/bsdconfig/common.subr (r269354)
  18. {
  19.     local __name="$1" __var_to_set="$2"
  20.     case "$__name" in */*|'') return $FAILURE; esac
  21.     local __p IFS=":" __found=
  22.     for __p in $PATH; do
  23.         local __exec="$__p/$__name"
  24.         [ -f "$__exec" -a -x "$__exec" ] && __found=1 && break
  25.     done
  26.     if [ "$__found" ]; then
  27.         if [ "$__var_to_set" ]; then
  28.             setvar "$__var_to_set" "$__exec"
  29.         else
  30.             echo "$__exec"
  31.         fi
  32.         return $SUCCESS
  33.     fi
  34.     return $FAILURE
  35. }
  36.  
  37. f_dprintf() # Taken from /usr/share/bsdconfig/common.subr (r269354)
  38. {
  39.     [ "$debug" ] || return $SUCCESS
  40.     local fmt="$1"; shift
  41.     case "$debugFile" in ""|+*)
  42.     printf "DEBUG: $fmt${fmt:+\n}" "$@" >&${TERMINAL_STDOUT_PASSTHRU:-1}
  43.     esac
  44.     [ "${debugFile#+}" ] &&
  45.         printf "DEBUG: $fmt${fmt:+\n}" "$@" >> "${debugFile#+}"
  46.     return $SUCCESS
  47. }
  48.  
  49. f_getvar() # Taken from /usr/share/bsdconfig/common.subr (r269354)
  50. {
  51.     local __var_to_get="$1" __var_to_set="$2"
  52.     [ "$__var_to_set" ] || local value
  53.     eval [ \"\${$__var_to_get+set}\" ]
  54.     local __retval=$?
  55.     eval ${__var_to_set:-value}=\"\${$__var_to_get}\"
  56.     eval f_dprintf '"f_getvar: var=[%s] value=[%s] r=%u"' \
  57.         \"\$__var_to_get\" \"\$${__var_to_set:-value}\" \$__retval
  58.     [ "$__var_to_set" ] || { [ "$value" ] && echo "$value"; }
  59.     return $__retval
  60. }
  61. fi
  62.  
  63. if [ ! "$_STRINGS_SUBR" ]; then
  64. f_replaceall() # Taken from /usr/share/bsdconfig/strings.subr (r263141)
  65. {
  66.     local __left="" __right="$1"
  67.     local __find="$2" __replace="$3" __var_to_set="$4"
  68.     while :; do
  69.         case "$__right" in *$__find*)
  70.             __left="$__left${__right%%$__find*}$__replace"
  71.             __right="${__right#*$__find}"
  72.             continue
  73.         esac
  74.         break
  75.     done
  76.     __left="$__left${__right#*$__find}"
  77.     if [ "$__var_to_set" ]; then
  78.         setvar "$__var_to_set" "$__left"
  79.     else
  80.         echo "$__left"
  81.     fi
  82. }
  83.  
  84. f_shell_escape() # Taken from /usr/share/bsdconfig/strings.subr (r263141)
  85. {
  86.         local __string="$1" __var_to_set="$2"
  87.         f_replaceall "$__string" "'" "'\\''" "$__var_to_set"
  88. }
  89. fi
  90.  
  91. ############################################################ MAIN
  92.  
  93. #
  94. # Initialize list of pids we care about
  95. # NB: We only care about pids with ucomm values copacetic to which(1)
  96. #
  97. PIDS=
  98.  
  99. #
  100. # Ask ps(1) for a list of pids and their associated short command names.
  101. # Generate $PIDS as space-separated list of process IDs whose ucomm represents
  102. # a utility name accessible via $PATH expansion. For each PID, set variables
  103. # pidcmd_$pid containing the ucomm value (short command name).
  104. #
  105. exec <<EOI
  106. $( ps ax -o pid= -o ucomm= )
  107. EOI
  108. while read pid ucomm; do
  109.     f_which "$ucomm" pidcmd || continue
  110.     [ -r "$pidcmd" ] || continue
  111.     PIDS="$PIDS $pid"
  112.     setvar pidcmd_$pid "$pidcmd"
  113. done
  114. exec <&1
  115.  
  116. #
  117. # Ask ps(1) for the last start (lstart) date/time of each pid in $PIDS
  118. #
  119. exec <<EOI
  120. $( ps -o pid= -o lstart= -p $PIDS )
  121. EOI
  122. while read pid lstart; do
  123.     setvar piddate_$pid "$( date -j -f %c "$lstart" +%s )"
  124. done
  125. exec <&1
  126.  
  127. #
  128. # Ask stat(1) for the modification time (epoch) for each running utility.
  129. # Modification times are stored in ondiskdate_$pid.
  130. #
  131. pidcmd_list=
  132. for pid in $PIDS; do
  133.     f_getvar pidcmd_$pid pidcmd
  134.     f_shell_escape "$pidcmd" _pidcmd
  135.     pidcmd_list="$pidcmd_list '$_pidcmd'"
  136. done
  137. exec <<EOI
  138. $( eval stat -f %m $pidcmd_list )
  139. EOI
  140. set -- $PIDS
  141. while read ondiskdate rest_ignored; do
  142.     pid="$1"
  143.     [ "$pid" ] || break
  144.     setvar ondiskdate_$pid "$ondiskdate"
  145.     shift 1 # pid
  146. done
  147. exec <&1
  148.  
  149. #
  150. # Compare on-disk epoch(s) to running epoch(s) (lstart)
  151. #
  152. for pid in $PIDS; do
  153.     f_getvar ondiskdate_$pid ondiskdate || continue
  154.     f_getvar piddate_$pid piddate || continue
  155.     f_getvar pidcmd_$pid pidcmd || continue
  156.     echo "Comparing $ondiskdate and $piddate for pid $pid $pidcmd"
  157.     [ $ondiskdate -gt $piddate ] &&
  158.         echo "ALERT! $ondisk needs to be reloaded!"
  159. done
  160.  
  161. exit 0
  162.  
  163. ################################################################################
  164. # END
  165. ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement