以下脚本动态构建应用程序/进程数组。这是一个优势,因为您不需要在启动之前“了解”每个进程。但是,重要的是要注意,由于此设计随着进程数量的增加,将新进程添加到阵列所需的时间越长。为了弥补这一点,脚本保持数组的内容被缓存,以便以后使用它们,从而无需将它们重新添加到数组中。
另外:如果在进程停止之前没有将应用程序/进程动态加载到数组中,则记录器将使“命令”列保持为空。当动态构建的数组基于其缓存信息构建时,这将变得越来越不可能。添加到上一点。有时“列”中可能没有显示“命令”,但它已经添加到数组中。这可能是因为某些进程像“sort”一样被删除,因为它们在脚本中使用并且会不断启动和停止(向记录器添加误报)。目前,数据将被放入文件/ command_logger中。这当然可以根据您的需要进行更改。配置文件/tmp/command_logger.plist 也可以重命名/移动但请记住它将数据保存到不断增长的App / Process数组中。
请随意使用并根据需要修改以下脚本。请记住在主要环境中运行之前使用测试环境。请享用。
测试:
• 10.5.x
• 10.6.x
• 10.7.4
输出将包括以下内容:
- 启动/停止
- 时代(时间戳)
- 约会时间
- PID
- 申请流程
注意:以下脚本包含内部循环。
动态APP /过程记录器
#!/bin/bash
On="true"
TMP="/tmp/command_logger"
LOG_CONFIG="/command_logger.plist"
LOG="/command_logger"
if [[ ! -e ${TMP} ]]; then
mkdir ${TMP}
if [[ ! -e ${TMP}${LOG_CONFIG} ]]; then
/usr/libexec/PlistBuddy -c "Add:Commands Dict" ${TMP}${LOG_CONFIG} > /dev/null 2>&1
fi
elif [[ -e ${TMP} ]]; then
if [[ ! -e ${TMP}${LOG_CONFIG} ]]; then
/usr/libexec/PlistBuddy -c "Add:Commands Dict" ${TMP}${LOG_CONFIG} > /dev/null 2>&1
find ${TMP} -type f ! -name '*.plist' -exec rm -f {} \;
elif [[ -e ${TMP}${LOG_CONFIG} ]]; then
find ${TMP} -type f ! -name '*.plist' -exec rm -f {} \;
fi
fi
if [[ ! -e ${LOG} ]]; then
echo -e "Status\tTimestamp\tDate/Time\t\tPID\tCommand\n" > ${LOG}
elif [[ -e ${LOG} ]]; then
TITLE=$(head -1 ${LOG} | awk '{print $1}')
if [[ ${TITLE} != "Status" ]]; then
echo -e "Status\tTimestamp\tDate/Time\t\tPID\tCommand\n" > ${LOG}
fi
fi
while [[ ${On} == "true" ]]; do
IFS=""
Live_Array=$(ps -Ac | sed 's/ /|/g' | sed 's/ /_/g' | sed 's/[[:digit:]]_.*[[:digit:]]_/ /g' | sed 's/:/!/' | sed 's/$/.comm/' | sed 's/^_//g' | sed 's/^|//g' | sed 's/^_//g' | sed 's/^|.*//g' | sed 's/ $//g' | sed 's/_$//g' | sed 's/|$//g' | sed 's/PID_TTY||.*//g' | awk '{print $2}' | sed 's/.*-sh.*//' | sed 's/.*CMD.*//' | sed 's/.*PID.*//' | sort -u | grep "[[:graph:]]" | tr -s "[\n]" "[,]")
Live_Array=$(echo ${Live_Array%\,})
IFS=","
Live_Array_2=( $Live_Array )
livarray=${#Live_Array_2[@]}
for (( liv=0; liv<${livarray}; liv++ ));
do
/usr/libexec/PlistBuddy -c "Add:Commands:${Live_Array_2[$liv]} bool true" ${TMP}${LOG_CONFIG} 2> /dev/null
Config_Array=$(/usr/libexec/PlistBuddy -c "Print:Commands" ${TMP}${LOG_CONFIG} | grep "=" | sed 's/=.*//' | sed 's/ //g' | sed 's/^.comm//' | sed 's/ $//g' | grep "[[:graph:]]" | tr -s "[\n]" "[,]")
Config_Array_2=( $Config_Array )
conarray=${#Config_Array_2[@]}
for (( con=0; con<${conarray}; con++ ));
do
STRING=$(ps -Ac | sed 's/ /|/g' | sed 's/ /_/g' | sed 's/[[:digit:]]_.*[[:digit:]]_/ /g' | sed 's/:/!/' | sed 's/$/.comm/' | sed 's/^_//g' | sed 's/^|//g' | sed 's/^_//g' | sed 's/^|.*//g' | sed 's/|_.comm/.comm/g' | sed 's/PID_TTY||.*//g' | awk '{print $2,$1}' | sed 's/.*-sh.*//' | sed 's/.*CMD.*//' | sed 's/.*PID.*//' | sort -u)
Launched_Command=$(echo ${STRING} | awk '{print $1}' | sort -u | awk "/${Config_Array_2[$con]}/")
PID=$(echo ${STRING} | sort -u | awk '{print $2,$1}' | awk "/${Config_Array_2[$con]}/" | sed 's/ .*//')
if [[ ${Launched_Command} != "" ]] && [[ ${PID} != "" ]]; then
DATE=$(date "+%m-%d-%Y %T")
EPOCH=$(date "+%s")
if [[ ${Launched_Command} == ${Config_Array_2[$con]} ]] && [[ ! -e ${TMP}/${Config_Array_2[$con]}-RUNNING ]]; then
echo -e "STARTED\t${EPOCH}\t${DATE}\t${PID}\t${Config_Array_2[$con]}" | sed 's/.comm$//g' | sed 's/_/ /g' | sed 's/.*sort.*//g' | sed 's/.*sed.*//g' | sed 's/awk//g'| awk '/STARTED/' >> ${LOG}
rm -f ${TMP}/${Config_Array_2[$con]}-STOPPED
touch ${TMP}/${Config_Array_2[$con]}-RUNNING
elif [[ ${Launched_Command} == ${Config_Array_2[$con]} ]] && [[ -e ${TMP}/${Config_Array_2[$con]}-RUNNING ]]; then
:
elif [[ ${Launched_Command} == ${Config_Array_2[$con]} ]] && [[ -e ${TMP}/${Config_Array_2[$con]}-RUNNING ]]; then
if [[ -e ${TMP}/${Config_Array_2[$con]}-STOPPED ]]; then
:
elif [[ ! -e ${TMP}/${Config_Array_2[$con]}-STOPPED ]]; then
echo -e "STOPPED\t${EPOCH}\t${DATE}\t${PID}\t${Config_Array_2[$con]}" | sed 's/.comm$//g' | sed 's/_/ /g' | sed 's/.*sort.*//g' | sed 's/.*sed.*//g' | sed 's/awk//g' | awk '/STOPPED/' >> ${LOG}
rm -f ${TMP}/${Config_Array_2[$con]}-RUNNING
touch ${TMP}/${Config_Array_2[$con]}-STOPPED
fi
fi
elif [[ ${Launched_Command} == "" ]] && [[ -e ${TMP}/${Config_Array_2[$con]}-RUNNING ]]; then
if [[ -e ${TMP}/${Config_Array_2[$con]}-STOPPED ]]; then
:
elif [[ ! -e ${TMP}/${Config_Array_2[$con]}-STOPPED ]]; then
echo -e "STOPPED\t${EPOCH}\t${DATE}\t${PID}\t${Config_Array_2[$con]}" | sed 's/.comm$//g' | sed 's/_/ /g' | sed 's/.*sort.*//g' | sed 's/.*sed.*//g' | sed 's/awk//g' | awk '/STOPPED/' >> ${LOG}
rm -f ${TMP}/${Config_Array_2[$con]}-RUNNING
touch ${TMP}/${Config_Array_2[$con]}-STOPPED
fi
fi
done
done
done