LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   "too many arguments" when running this script (https://www.linuxquestions.org/questions/linux-server-73/too-many-arguments-when-running-this-script-826474/)

koshihaku 08-16-2010 01:47 AM

"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


acid_kewpie 08-16-2010 01:57 AM

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.

koshihaku 08-16-2010 09:44 PM

Quote:

Originally Posted by acid_kewpie (Post 4067459)
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" ?

konsolebox 08-16-2010 10:38 PM

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


koshihaku 08-16-2010 11:37 PM

Quote:

Originally Posted by konsolebox (Post 4068451)
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

koshihaku 08-17-2010 01:25 AM

need more help
 
Quote:

Originally Posted by konsolebox (Post 4068451)
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

konsolebox 08-17-2010 01:45 AM

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


koshihaku 08-17-2010 08:12 AM

many thanks
 
Quote:

Originally Posted by konsolebox (Post 4068575)
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

koshihaku 08-20-2010 12:36 AM

Quote:

Originally Posted by konsolebox (Post 4068575)
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

konsolebox 08-20-2010 12:55 AM

Quote:

Originally Posted by koshihaku (Post 4071977)
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.

koshihaku 08-20-2010 10:50 AM

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/

koshihaku 08-20-2010 10:52 AM

Quote:

Originally Posted by konsolebox (Post 4071990)
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.

koshihaku 08-23-2010 09:55 PM

Quote:

Originally Posted by konsolebox (Post 4071990)
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

konsolebox 08-24-2010 03:24 AM

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

koshihaku 08-30-2010 08:29 AM

Quote:

Originally Posted by konsolebox (Post 4075866)
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.


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