LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-16-2010, 01:47 AM   #1
koshihaku
Member
 
Registered: Aug 2010
Posts: 50

Rep: Reputation: 0
Unhappy "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
 
Old 08-16-2010, 01:57 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
Old 08-16-2010, 09:44 PM   #3
koshihaku
Member
 
Registered: Aug 2010
Posts: 50

Original Poster
Rep: Reputation: 0
Question

Quote:
Originally Posted by acid_kewpie View Post
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.
 
Old 08-16-2010, 10:38 PM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
Old 08-16-2010, 11:37 PM   #5
koshihaku
Member
 
Registered: Aug 2010
Posts: 50

Original Poster
Rep: Reputation: 0
Exclamation

Quote:
Originally Posted by konsolebox View Post
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
 
Old 08-17-2010, 01:25 AM   #6
koshihaku
Member
 
Registered: Aug 2010
Posts: 50

Original Poster
Rep: Reputation: 0
Question need more help

Quote:
Originally Posted by konsolebox View Post
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
 
Old 08-17-2010, 01:45 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
Old 08-17-2010, 08:12 AM   #8
koshihaku
Member
 
Registered: Aug 2010
Posts: 50

Original Poster
Rep: Reputation: 0
Talking many thanks

Quote:
Originally Posted by konsolebox View Post
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
 
Old 08-20-2010, 12:36 AM   #9
koshihaku
Member
 
Registered: Aug 2010
Posts: 50

Original Poster
Rep: Reputation: 0
Question

Quote:
Originally Posted by konsolebox View Post
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
 
Old 08-20-2010, 12:55 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by koshihaku View Post
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.
Old 08-20-2010, 10:50 AM   #11
koshihaku
Member
 
Registered: Aug 2010
Posts: 50

Original Poster
Rep: Reputation: 0
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/
 
Old 08-20-2010, 10:52 AM   #12
koshihaku
Member
 
Registered: Aug 2010
Posts: 50

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by konsolebox View Post
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.
 
Old 08-23-2010, 09:55 PM   #13
koshihaku
Member
 
Registered: Aug 2010
Posts: 50

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by konsolebox View Post
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
 
Old 08-24-2010, 03:24 AM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.

http://linuxdevcenter.com/pub/a/linu...of-memory.html
http://www.mail-archive.com/c-prog@y.../msg05009.html
http://www.linuxforums.org/forum/lin...id-status.html
 
Old 08-30-2010, 08:29 AM   #15
koshihaku
Member
 
Registered: Aug 2010
Posts: 50

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by konsolebox View Post
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.

http://linuxdevcenter.com/pub/a/linu...of-memory.html
http://www.mail-archive.com/c-prog@y.../msg05009.html
http://www.linuxforums.org/forum/lin...id-status.html
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
[SOLVED] Problem running .sh script with "screen" and "java" IJustinI Linux - Newbie 31 06-01-2010 07:13 AM
cant "source" bash script with arguments (PATH updating) monkey_king Linux - General 5 12-30-2008 04:34 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration