Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- ############################################################ IDENT(1)
- #
- # $Title: Script to show running processes started prior to modification $
- #
- ############################################################ INCLUDES
- BSDCFG_SHARE="/usr/share/bsdconfig"
- if [ -f "$BSDCFG_SHARE/common.subr" -a -r "$BSDCFG_SHARE/common.subr" ]; then
- . "$BSDCFG_SHARE/common.subr" || exit 1
- f_include "$BSDCFG_SHARE/strings.subr"
- fi
- ############################################################ FUNCTIONS
- if [ ! "$_COMMON_SUBR" ]; then
- f_which() # Taken from /usr/share/bsdconfig/common.subr (r269354)
- {
- local __name="$1" __var_to_set="$2"
- case "$__name" in */*|'') return $FAILURE; esac
- local __p IFS=":" __found=
- for __p in $PATH; do
- local __exec="$__p/$__name"
- [ -f "$__exec" -a -x "$__exec" ] && __found=1 && break
- done
- if [ "$__found" ]; then
- if [ "$__var_to_set" ]; then
- setvar "$__var_to_set" "$__exec"
- else
- echo "$__exec"
- fi
- return $SUCCESS
- fi
- return $FAILURE
- }
- f_dprintf() # Taken from /usr/share/bsdconfig/common.subr (r269354)
- {
- [ "$debug" ] || return $SUCCESS
- local fmt="$1"; shift
- case "$debugFile" in ""|+*)
- printf "DEBUG: $fmt${fmt:+\n}" "$@" >&${TERMINAL_STDOUT_PASSTHRU:-1}
- esac
- [ "${debugFile#+}" ] &&
- printf "DEBUG: $fmt${fmt:+\n}" "$@" >> "${debugFile#+}"
- return $SUCCESS
- }
- f_getvar() # Taken from /usr/share/bsdconfig/common.subr (r269354)
- {
- local __var_to_get="$1" __var_to_set="$2"
- [ "$__var_to_set" ] || local value
- eval [ \"\${$__var_to_get+set}\" ]
- local __retval=$?
- eval ${__var_to_set:-value}=\"\${$__var_to_get}\"
- eval f_dprintf '"f_getvar: var=[%s] value=[%s] r=%u"' \
- \"\$__var_to_get\" \"\$${__var_to_set:-value}\" \$__retval
- [ "$__var_to_set" ] || { [ "$value" ] && echo "$value"; }
- return $__retval
- }
- fi
- if [ ! "$_STRINGS_SUBR" ]; then
- f_replaceall() # Taken from /usr/share/bsdconfig/strings.subr (r263141)
- {
- local __left="" __right="$1"
- local __find="$2" __replace="$3" __var_to_set="$4"
- while :; do
- case "$__right" in *$__find*)
- __left="$__left${__right%%$__find*}$__replace"
- __right="${__right#*$__find}"
- continue
- esac
- break
- done
- __left="$__left${__right#*$__find}"
- if [ "$__var_to_set" ]; then
- setvar "$__var_to_set" "$__left"
- else
- echo "$__left"
- fi
- }
- f_shell_escape() # Taken from /usr/share/bsdconfig/strings.subr (r263141)
- {
- local __string="$1" __var_to_set="$2"
- f_replaceall "$__string" "'" "'\\''" "$__var_to_set"
- }
- fi
- ############################################################ MAIN
- #
- # Initialize list of pids we care about
- # NB: We only care about pids with ucomm values copacetic to which(1)
- #
- PIDS=
- #
- # Ask ps(1) for a list of pids and their associated short command names.
- # Generate $PIDS as space-separated list of process IDs whose ucomm represents
- # a utility name accessible via $PATH expansion. For each PID, set variables
- # pidcmd_$pid containing the ucomm value (short command name).
- #
- exec <<EOI
- $( ps ax -o pid= -o ucomm= )
- EOI
- while read pid ucomm; do
- f_which "$ucomm" pidcmd || continue
- [ -r "$pidcmd" ] || continue
- PIDS="$PIDS $pid"
- setvar pidcmd_$pid "$pidcmd"
- done
- exec <&1
- #
- # Ask ps(1) for the last start (lstart) date/time of each pid in $PIDS
- #
- exec <<EOI
- $( ps -o pid= -o lstart= -p $PIDS )
- EOI
- while read pid lstart; do
- setvar piddate_$pid "$( date -j -f %c "$lstart" +%s )"
- done
- exec <&1
- #
- # Ask stat(1) for the modification time (epoch) for each running utility.
- # Modification times are stored in ondiskdate_$pid.
- #
- pidcmd_list=
- for pid in $PIDS; do
- f_getvar pidcmd_$pid pidcmd
- f_shell_escape "$pidcmd" _pidcmd
- pidcmd_list="$pidcmd_list '$_pidcmd'"
- done
- exec <<EOI
- $( eval stat -f %m $pidcmd_list )
- EOI
- set -- $PIDS
- while read ondiskdate rest_ignored; do
- pid="$1"
- [ "$pid" ] || break
- setvar ondiskdate_$pid "$ondiskdate"
- shift 1 # pid
- done
- exec <&1
- #
- # Compare on-disk epoch(s) to running epoch(s) (lstart)
- #
- for pid in $PIDS; do
- f_getvar ondiskdate_$pid ondiskdate || continue
- f_getvar piddate_$pid piddate || continue
- f_getvar pidcmd_$pid pidcmd || continue
- echo "Comparing $ondiskdate and $piddate for pid $pid $pidcmd"
- [ $ondiskdate -gt $piddate ] &&
- echo "ALERT! $ondisk needs to be reloaded!"
- done
- exit 0
- ################################################################################
- # END
- ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement