Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
when I run this script, I got "line 32: [: too many arguments"
what may be the problem?
I run it like this : ./scriptname apache2
Code:
#!/bin/sh
#G.sh 20100813
USAGE="Usage: $0 processName"
if [ $# -ne 1 ]; then
echo $USAGE
exit 1
fi
# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PID=""
while :
do
PROCESS_PID=`/bin/pidof $1`
if [ "$PROCESS_PID.X" != ".X" ]; then
break
fi
done
LOG_FILE="memusage.csv"
echo "ElapsedTime,VmSize,VmRSS" > $LOG_FILE
ELAPSED_TIME=`date +%H:%M:%S:%N`
PERIOD=2 # seconds
while :
do
if [ -d /proc/$PROCESS_PID ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < /proc/$PROCESS_PID/status`
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < /proc/$PROCESS_PID/status`
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
echo "$ELAPSED_TIME,$VM_SIZE,$VM_RSS" >> $LOG_FILE
sleep $PERIOD
VM_SIZE=""
VM_RSS=""
ELAPSED_TIME=`date +%H:%M:%S:%N`
else
echo "$1 is no longer a running process"
exit 0
fi
done
a multi threaded program, like apache will have multiple processes, therefore pidof will have returned more than one result., so this would have been turned in to:
Code:
if [ -d /proc/123 456 789 ] ; then
which I'm sure you can see is nonsense. If you add the -s option to pidof you'll only get one pid back, but then if you're looking to add up the process info, then you'll need to loop through them instead.
a multi threaded program, like apache will have multiple processes, therefore pidof will have returned more than one result., so this would have been turned in to:
Code:
if [ -d /proc/123 456 789 ] ; then
which I'm sure you can see is nonsense. If you add the -s option to pidof you'll only get one pid back, but then if you're looking to add up the process info, then you'll need to loop through them instead.
Thank you acid_kewpie,
you said I need to loop through them, but I am a newbie and not familiar with shell.
You can try this mod. I also made some syntax cleanups. I hope you don't mind.
Code:
#!/bin/sh
#G.sh 20100813
USAGE="Usage: $0 processName"
if [ "$#" -ne 1 ]; then
echo "$USAGE"
exit 1
fi
# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PID=""
while :
do
PROCESS_PID=`/bin/pidof "$1"`
if [ "$PROCESS_PID.X" != ".X" ]; then
break
fi
done
LOG_FILE="memusage.csv"
echo "ElapsedTime,VmSize,VmRSS" > "$LOG_FILE"
ELAPSED_TIME=`date +%H:%M:%S:%N`
PERIOD=2 # seconds
while :
do
for PID in $PROCESS_PID; do
if [ -d "/proc/$PID" ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
echo "$ELAPSED_TIME,$VM_SIZE,$VM_RSS" >> "$LOG_FILE"
sleep "$PERIOD"
VM_SIZE=""
VM_RSS=""
ELAPSED_TIME=`date +%H:%M:%S:%N`
else
echo "$1 ($PID) is no longer a running process"
exit 0
fi
done
done
You can try this mod. I also made some syntax cleanups. I hope you don't mind.
Code:
#!/bin/sh
#G.sh 20100813
USAGE="Usage: $0 processName"
if [ "$#" -ne 1 ]; then
echo "$USAGE"
exit 1
fi
# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PID=""
while :
do
PROCESS_PID=`/bin/pidof "$1"`
if [ "$PROCESS_PID.X" != ".X" ]; then
break
fi
done
LOG_FILE="memusage.csv"
echo "ElapsedTime,VmSize,VmRSS" > "$LOG_FILE"
ELAPSED_TIME=`date +%H:%M:%S:%N`
PERIOD=2 # seconds
while :
do
for PID in $PROCESS_PID; do
if [ -d "/proc/$PID" ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
echo "$ELAPSED_TIME,$VM_SIZE,$VM_RSS" >> "$LOG_FILE"
sleep "$PERIOD"
VM_SIZE=""
VM_RSS=""
ELAPSED_TIME=`date +%H:%M:%S:%N`
else
echo "$1 ($PID) is no longer a running process"
exit 0
fi
done
done
You can try this mod. I also made some syntax cleanups. I hope you don't mind.
Code:
#!/bin/sh
#G.sh 20100813
USAGE="Usage: $0 processName"
if [ "$#" -ne 1 ]; then
echo "$USAGE"
exit 1
fi
# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PID=""
while :
do
PROCESS_PID=`/bin/pidof "$1"`
if [ "$PROCESS_PID.X" != ".X" ]; then
break
fi
done
LOG_FILE="memusage.csv"
echo "ElapsedTime,VmSize,VmRSS" > "$LOG_FILE"
ELAPSED_TIME=`date +%H:%M:%S:%N`
PERIOD=2 # seconds
while :
do
for PID in $PROCESS_PID; do
if [ -d "/proc/$PID" ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
echo "$ELAPSED_TIME,$VM_SIZE,$VM_RSS" >> "$LOG_FILE"
sleep "$PERIOD"
VM_SIZE=""
VM_RSS=""
ELAPSED_TIME=`date +%H:%M:%S:%N`
else
echo "$1 ($PID) is no longer a running process"
exit 0
fi
done
done
when I run my test, I got the result of VMRSS is about 4mb, but that is the RSS of a single apache2 process. Actually, I need the total RSS or VMsize of all apache2 processes. Because I want to see when will the memory used by apache2 goes above a certain value.
Is there a way to achieve this by revising the script? thanks
#!/bin/sh
#G.sh 20100813
USAGE="Usage: $0 processName"
if [ "$#" -ne 1 ]; then
echo "$USAGE"
exit 1
fi
# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PID=""
while :
do
PROCESS_PID=`/bin/pidof "$1"`
if [ "$PROCESS_PID.X" != ".X" ]; then
break
fi
done
LOG_FILE="memusage.csv"
echo "ElapsedTime,VmSize,VmRSS" > "$LOG_FILE"
ELAPSED_TIME=`date +%H:%M:%S:%N`
PERIOD=2 # seconds
while :
do
TOTAL_VM_SIZE=0
TOTAL_VM_RSS=0
for PID in $PROCESS_PID; do
if [ -d "/proc/$PID" ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
let TOTAL_VM_SIZE+=VM_SIZE
let TOTAL_VM_RSS+=VM_RSS
else
echo "$1 ($PID) is no longer a running process"
exit 0
fi
done
echo "$ELAPSED_TIME,$TOTAL_VM_SIZE,$TOTAL_VM_RSS" >> "$LOG_FILE"
sleep "$PERIOD"
ELAPSED_TIME=`date +%H:%M:%S:%N`
done
#!/bin/sh
#G.sh 20100813
USAGE="Usage: $0 processName"
if [ "$#" -ne 1 ]; then
echo "$USAGE"
exit 1
fi
# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PID=""
while :
do
PROCESS_PID=`/bin/pidof "$1"`
if [ "$PROCESS_PID.X" != ".X" ]; then
break
fi
done
LOG_FILE="memusage.csv"
echo "ElapsedTime,VmSize,VmRSS" > "$LOG_FILE"
ELAPSED_TIME=`date +%H:%M:%S:%N`
PERIOD=2 # seconds
while :
do
TOTAL_VM_SIZE=0
TOTAL_VM_RSS=0
for PID in $PROCESS_PID; do
if [ -d "/proc/$PID" ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
let TOTAL_VM_SIZE+=VM_SIZE
let TOTAL_VM_RSS+=VM_RSS
else
echo "$1 ($PID) is no longer a running process"
exit 0
fi
done
echo "$ELAPSED_TIME,$TOTAL_VM_SIZE,$TOTAL_VM_RSS" >> "$LOG_FILE"
sleep "$PERIOD"
ELAPSED_TIME=`date +%H:%M:%S:%N`
done
#!/bin/sh
#G.sh 20100813
USAGE="Usage: $0 processName"
if [ "$#" -ne 1 ]; then
echo "$USAGE"
exit 1
fi
# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PID=""
while :
do
PROCESS_PID=`/bin/pidof "$1"`
if [ "$PROCESS_PID.X" != ".X" ]; then
break
fi
done
LOG_FILE="memusage.csv"
echo "ElapsedTime,VmSize,VmRSS" > "$LOG_FILE"
ELAPSED_TIME=`date +%H:%M:%S:%N`
PERIOD=2 # seconds
while :
do
TOTAL_VM_SIZE=0
TOTAL_VM_RSS=0
for PID in $PROCESS_PID; do
if [ -d "/proc/$PID" ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < "/proc/$PID/status"`
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
let TOTAL_VM_SIZE+=VM_SIZE
let TOTAL_VM_RSS+=VM_RSS
else
echo "$1 ($PID) is no longer a running process"
exit 0
fi
done
echo "$ELAPSED_TIME,$TOTAL_VM_SIZE,$TOTAL_VM_RSS" >> "$LOG_FILE"
sleep "$PERIOD"
ELAPSED_TIME=`date +%H:%M:%S:%N`
done
I got the data like this
ElapsedTime VmSize VmRSS
14:02:15:142435792 369796 69268
14:02:17:304277575 369796 69268
14:02:19:460760757 369796 69268
14:02:21:605478630 369796 69268
14:02:23:780021002 369796 69268
14:02:25:929159430 369800 69268
14:02:28:091948031 369796 69268
14:02:30:244159809 369796 69268
...
...
...
Could this be a reasonable data? the RSS is much smaller than the Vmsize, is that because the argument I used was wrong?
And I did not understand the numbers after time, 14:02:30:244159809
Last edited by koshihaku; 08-20-2010 at 12:37 AM.
Reason: adding
I got the data like this
Could this be a reasonable data? the RSS is much smaller than the Vmsize, is that because the argument I used was wrong?
Sorry but I also don't have a clue what those values are. I think you have properly fetched the data, if not the value of TOTAL_VM_RSS would be 0.
I suggest that you check each process manually to know if the difference of those numbers are consistent.
You can also modify the script to echo the values for every process:
Code:
...
for PID in $PROCESS_PID; do
if [ -d "/proc/$PID" ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < "/proc/$PID/status"`
echo "VmSize of $PID is '$VM_SIZE'"
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < "/proc/$PID/status"`
echo "VmRSS of $PID is '$VM_RSS'"
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
let TOTAL_VM_SIZE+=VM_SIZE
let TOTAL_VM_RSS+=VM_RSS
else
echo "$1 ($PID) is no longer a running process"
exit 0
fi
done
...
You can remove the added lines after debug.
Quote:
And I did not understand the numbers after time, 14:02:30:244159809
Those means nanoseconds. See 'man date' for more info.
Sorry but I also don't have a clue what those values are. I think you have properly fetched the data, if not the value of TOTAL_VM_RSS would be 0.
I suggest that you check each process manually to know if the difference of those numbers are consistent.
You can also modify the script to echo the values for every process:
Code:
...
for PID in $PROCESS_PID; do
if [ -d "/proc/$PID" ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < "/proc/$PID/status"`
echo "VmSize of $PID is '$VM_SIZE'"
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < "/proc/$PID/status"`
echo "VmRSS of $PID is '$VM_RSS'"
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
let TOTAL_VM_SIZE+=VM_SIZE
let TOTAL_VM_RSS+=VM_RSS
else
echo "$1 ($PID) is no longer a running process"
exit 0
fi
done
...
You can remove the added lines after debug.
Those means nanoseconds. See 'man date' for more info.
Sorry but I also don't have a clue what those values are. I think you have properly fetched the data, if not the value of TOTAL_VM_RSS would be 0.
I suggest that you check each process manually to know if the difference of those numbers are consistent.
You can also modify the script to echo the values for every process:
Code:
...
for PID in $PROCESS_PID; do
if [ -d "/proc/$PID" ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < "/proc/$PID/status"`
echo "VmSize of $PID is '$VM_SIZE'"
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < "/proc/$PID/status"`
echo "VmRSS of $PID is '$VM_RSS'"
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
let TOTAL_VM_SIZE+=VM_SIZE
let TOTAL_VM_RSS+=VM_RSS
else
echo "$1 ($PID) is no longer a running process"
exit 0
fi
done
...
You can remove the added lines after debug.
Those means nanoseconds. See 'man date' for more info.
I tried your script, and I think maybe there is no problem.
But I am think about why the VMRss is much smaller than the VMsize
Perhaps VMsize is not really the total amount of memory allocated but only what virtually appears to the process? Sorry I also don't know much about it.
I found this pages while trying to figure it out. Just in case you find something about it.
Perhaps VMsize is not really the total amount of memory allocated but only what virtually appears to the process? Sorry I also don't know much about it.
I found this pages while trying to figure it out. Just in case you find something about it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.