Linux - Server This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
08-16-2010, 01:47 AM
|
#1
|
Member
Registered: Aug 2010
Posts: 50
Rep:
|
"too many arguments" when running this script
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
|
|
|
08-16-2010, 01:57 AM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
Code:
if [ -d /proc/$PROCESS_PID ] ; then
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.
|
|
2 members found this post helpful.
|
08-16-2010, 09:44 PM
|
#3
|
Member
Registered: Aug 2010
Posts: 50
Original Poster
Rep:
|
Quote:
Originally Posted by acid_kewpie
Code:
if [ -d /proc/$PROCESS_PID ] ; then
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.
can I use "for...do" ?
Last edited by koshihaku; 08-16-2010 at 09:56 PM.
|
|
|
08-16-2010, 10:38 PM
|
#4
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
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
|
|
1 members found this post helpful.
|
08-16-2010, 11:37 PM
|
#5
|
Member
Registered: Aug 2010
Posts: 50
Original Poster
Rep:
|
Quote:
Originally Posted by konsolebox
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
|
Thank you very much, I will try this
|
|
|
08-17-2010, 01:25 AM
|
#6
|
Member
Registered: Aug 2010
Posts: 50
Original Poster
Rep:
|
need more help
Quote:
Originally Posted by konsolebox
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
|
|
|
08-17-2010, 01:45 AM
|
#7
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
try this then
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
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
|
|
1 members found this post helpful.
|
08-17-2010, 08:12 AM
|
#8
|
Member
Registered: Aug 2010
Posts: 50
Original Poster
Rep:
|
many thanks
Quote:
Originally Posted by konsolebox
try this then
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
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 will try this tomorrow in my lab.
thanks a lot
|
|
|
08-20-2010, 12:36 AM
|
#9
|
Member
Registered: Aug 2010
Posts: 50
Original Poster
Rep:
|
Quote:
Originally Posted by konsolebox
try this then
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
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
|
|
|
08-20-2010, 12:55 AM
|
#10
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
Quote:
Originally Posted by koshihaku
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.
|
|
1 members found this post helpful.
|
08-20-2010, 10:50 AM
|
#11
|
Member
Registered: Aug 2010
Posts: 50
Original Poster
Rep:
|
Thanks for all of you, your advices and warm supporting helped me a lot.
I will update the progress of my experiment, incase anyone is interested or doing similar things.
This is another problem i encountered
http://www.linuxquestions.org/questi...riment-827370/
|
|
|
08-20-2010, 10:52 AM
|
#12
|
Member
Registered: Aug 2010
Posts: 50
Original Poster
Rep:
|
Quote:
Originally Posted by konsolebox
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.
|
Thank you! I will check this tomorrow in the lab.
|
|
|
08-23-2010, 09:55 PM
|
#13
|
Member
Registered: Aug 2010
Posts: 50
Original Poster
Rep:
|
Quote:
Originally Posted by konsolebox
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
|
|
|
08-30-2010, 08:29 AM
|
#15
|
Member
Registered: Aug 2010
Posts: 50
Original Poster
Rep:
|
Quote:
Originally Posted by konsolebox
|
Today in my experiments, the VMsize is about 3G and the VMRss is about 2G, but the memory of PC is only 1G. I am confused by this.
|
|
|
All times are GMT -5. The time now is 03:48 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|