pgrep substitute for porting a linux script to AIX 5.x


 
Thread Tools Search this Thread
Operating Systems AIX pgrep substitute for porting a linux script to AIX 5.x
# 1  
Old 08-22-2007
pgrep substitute for porting a linux script to AIX 5.x

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=ResidentPa ges,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.
# 2  
Old 08-22-2007
I didn't manage to see where do you define $procName or I'm missing something ? Anyways, you can get the PIDs like :
Code:
ps -ef | grep process-name | awk '{print $2}'

HTH
# 3  
Old 08-23-2007
Smilie hi and thanks for that, it worked perfectly.
I have another question, can I get ps out put with --no-header option, I couldn't find it in the man pages, I'm doing this script for AIX 5.2 and 5.3
Thanks for the help.
Below is the script as its coming along.#!/bin/bash
# author siddharth.mody@gmail.com
#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}".csv"

#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 /scripts
pid='pid'
pName='pName'

tokens=''

# get pairs of PID and processNames per line

# FOR EACH PROCESS TO BE WATCHED
# PROCESS A:

for procName in proc1 proc1
do
R=$(ps -ef | grep $procName | awk '{print $2} {print $8}')
tokens=$tokens' '$R
done




echo $tokens

echo +++++++++++++++++++++++++++++++++++++++++++++

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 -p ${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=ResidentPa ges,size=RequiredSize,share=Shared)
mt_memory_pmem=$(ps -p ${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}".csv" ]
then
:
# echo ${pName}"_"${pid}"_"${1}".csv :File already exists"
else
# echo "FileDoesNotExist: Updating header..."
echo "Memory Statistics for user: "${1} >> ${pName}"_"${pid}"_"${1}".csv"
echo "["$(uname -a)"]" >> ${pName}"_"${pid}"_"${1}".csv"
echo "${mainHeader}" >> ${pName}"_"${pid}"_"${1}".csv"
fi
# get memInfo
memInfo=${data_cpu}" "${data_memory}" "${data_time}" "${data_command}
# write timestamp with memInfo
echo ${BACKUPDATE}" ""${logData}" >> ${pName}"_"${pid}"_"${1}".csv"
#===============end=logging===============
fi
done
exit 0

# 4  
Old 08-23-2007
HI I got the solution with:
ps -p 2732036 -o pmem=,pcpu=
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Porting script from Solaris to Linux

I have a script which has commands that are located in different paths on my Linux o/s than on Solaris. For example, to make uname work, I need to do it this way in Solaris: my $host= `/usr/bin/uname -n` But in Linux it is: my $host = `/bin/uname -n`I have this issue with at least 5... (8 Replies)
Discussion started by: newbie2010
8 Replies

2. AIX

Porting from HP-UX to AIX

I am orignally a VC++ programmer, this is the my post in UNIX form. I have an existing source code, that was developed in old HP-UX system, the objective is to make it work on new AIX system. It does'nt appear that AIX has the necessary libraries or even run the application if its... (0 Replies)
Discussion started by: maverick786us
0 Replies

3. AIX

porting an application on 64-bit AIX version 5

Hi all, I am facing problem in porting to 64 bit AIX especially with compiling options to compile and run under 64 bit kernel.Can any one put some light on the issue thanks in advance vishal (14 Replies)
Discussion started by: vishalzone2002
14 Replies

4. Shell Programming and Scripting

porting shell script from Linux to AIX.

Hi, I am porting one shell script from Linux to AIX. I had .ksh file and i have changed it to .sh file for aix. on linux this script is running fine but on aix it gives me "unexpected end of file" error. Could any one suggest me what to do to port this script error free? Thanks in... (6 Replies)
Discussion started by: joy_1
6 Replies

5. AIX

Porting to AIX from Sun Solaris

Hi, When we tried to port our Pro*C programs from sun solaris to AIX, We get following error messages: The following library is not available... This library file is required for Pro*C Object Linking and Bin creation. /usr/lib/libnsl.so.1. Can anybody help us on this ? Regards rbs100 (1 Reply)
Discussion started by: rbs100
1 Replies

6. UNIX for Advanced & Expert Users

Porting linux ftpd code to Aix

Dear All, I have already ported the linux ftpd daemon to AIX earlier. When the ftp client does a "DIR" command, I have intercepted the call to the remote machine at the server code and added my code to browse IBM Message Queue, so that the client will see the files in the IBM Message... (2 Replies)
Discussion started by: srksn
2 Replies

7. UNIX for Advanced & Expert Users

Porting GNU FTPD daemon from Linux to AIX

Dear All, I want to port GNU ftpd daemon server from Linux to AIX. I want to download GNU ftpd daemon for linux. Please tell me from where I can download this code and which code will be most suitable for this porting? Please help in this regard. Thanks Siva (0 Replies)
Discussion started by: srksn
0 Replies

8. Programming

Porting of Shell to AIX

Hi All, I would like to know of the steps needed for the implementation of a shell in AIX. This shell is written by one of our developers and is currently running in Solaris. This needs to be ported to AIX. Does any of you guys know the implementation steps to do this. If so please do let me... (5 Replies)
Discussion started by: dhanamurthy
5 Replies

9. AIX

Porting C codes from HP-UX to AIX

Hi , Can someone help me get some support documents on the common issues/pitfalls and any other details with respect to porting the application running on C codes from HP-UX to AIX. Help will be valuable. Thanks a lot (0 Replies)
Discussion started by: Sinbad
0 Replies
Login or Register to Ask a Question