Hi, I'm trying to get this script to work on an AIX 5.3 box, I couldn't get pgrep for AIX, I also realize that ps works differently on the IBM boxes. Could anybody just give me the specifics of a work around for my problem, I'll adjust the whole script:
#!/bin/bash
# applabs.com
#to do:
# eliminate funny name processes [done for esecadm, inProgressforGenericUser:seems to be limited char returned!!!]
# CommonLogfile [will be done after metatags symantics are set]
# Aggregates calculation and other metrics
# Fine tune captured content [inProgress]
# Eliminate redundant data eg: eliminate pid feild in pid based listings [inProgress]
#echo "===============START==============="
ROOT_UID=0 # only users with $UID 0 have root privilages.
E_NOROOT=67 # Non-root exit error.
BACKUPDATE=$(date +%m.%d.%Y-%H\:%M\:%S)","
FILENAME=$1-${BACKUPDATE}".mem"
#check if root user.
# Run as root:
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script"
exit $E_NOTROOT
fi
IsNumeric () {
case "${1}" in
*[!0-9,]*) return 1;; # invalid
*) return 0;; # valid
esac
}
strcmp ()# string == string
{
local _STRING1=$1 _STRING2=$2 _a _b
case $#.$3 in
2.) ;;
3.*:) _a=${3%:} _b=${3#*:}
set -- `python_to_shell_range "$_a" "$_b" ${#_STRING1}`
_STRING1=${_STRING1:$1:$2}
set -- `python_to_shell_range "$_a" "$_b" ${#_STRING2}`
_STRING2=${_STRING2:$1:$2}
;;
*) echo "Usage: strcmp string1 string2 [a:b]"
return 2
;;
esac
[ "$_STRING1" == "$_STRING2" ]
}
#initialize
cd /root/scripts
pid='pid'
pName='pName'
javaname='javaname'
#cd /root/scripts
#echo ""
tokens=''
echo "Collecting data for the python and bash prcessesi"
# get pairs of PID and processNames per line
for procName in python bash; do
R=$(pgrep -l -x $procName)
tokens=$pidlist' '$R
done
echo $tokens
read -r -a tokenArray <<< $tokens
# Seperate PID and pNames in tokens array.
for token in "${tokenArray[@]}"
do
# PID
if IsNumeric "$token"; then
pid=${token}
else
# PROCESS NAME
pName=$(ps --no-header --pid "${pid}" -o comm)
# LIVE DATA COLLECTION [START]
# METATAGS given: pid, pName
# ___CPU___
#echo "Collecting CPU related data for '"${pName}"'."
header_cpu='CPU_usage(%)'
data_cpu=$(ps --no-header --pid ${pid} -o pcpu)
#___MEMORY___
#echo "Collecting MEMORY related data for '"${pName}"'."
header_memory='PhysicalMemory(%)'
# data_memory=$(ps --no-header --pid ${pid} -o pmem=PhysicalMemory\(\%\),sz=CoreImageSize,vsz=VirtualMemory,rss=ResidentSetSize,resident=ResidentPages,size=RequiredSize,share=Shared)
mt_memory_pmem=$(ps --no-header --pid ${pid} -o pmem)
data_memory=${mt_memory_pmem}
\# TTY output:
echo "$\{pName\}"
\# DATA LOGGING CODE [START]
mainHeader="DateAndTimeOfReading,"$\{header\_cpu\}","$\{header_memory\}
logData=$\{data\_cpu\}","$\{data_memory\}
\# Create and inject header into file if it does not exist
if [ -f $\{pName\}"\_"$\{pid\}"_"$\{1\}".mem" ]
then
:
# echo ${pName}"_"${pid}""${1}".mem :File already exists"
else
# echo "FileDoesNotExist: Updating header..."
echo "Memory Statistics for user: "${1} >> ${pName}"_"${pid}""${1}".mem"
echo "["$(uname -a)"]" >> ${pName}"_"${pid}""${1}".mem"
echo "${mainHeader}" >> ${pName}"_"${pid}""${1}".mem"
fi
# get memInfo
memInfo=${data_cpu}" "${data_memory}" "${data_time}" "${data_command}
# write timestamp with memInfo
echo ${BACKUPDATE}" ""${logData}" >> ${pName}"_"${pid}"_"${1}".mem"
#===============end=logging===============
fi
done
exit 0
I just need to know how to get the pids given a few process names.
Thanks in advance.