Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hi, guys, I type in 'top' in linux terminal and then press shift M, it will sort memuse. I want to let all this process run automatically in a script ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2012
    Posts
    10

    How to read keyboard input shift M in script when run top in linux


    Hi, guys, I type in 'top' in linux terminal and then press shift M, it will sort memuse.
    I want to let all this process run automatically in a script 1.csh and output the sorted-result into a file such as 1.log. so I write 1.csh script which content is as follows:
    Code:
    top << EOF > 1.log
    M
    EOF
    then I run 1.csh, but there will emerge a error : top failed tty get.I don't know how to solve this problem, anyone can help me? thanks in advance with any information.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Hello and welcome!

    If you don't need top continually running, call it in batch mode:
    Code:
    top -n 1 -b > 1.log
    That will run one iteration of top and exit.

    Or if you want it to run continually, while logging, use screen and screen's built-in logging facility, e.g:
    Code:
    screen -dm -S "Top" -L top
    By default, output will go to this file:
    Code:
    ~/screenlog.0

  3. #3
    Just Joined!
    Join Date
    Nov 2012
    Posts
    10
    Thanks atreyu first.
    top -n 1 -b > 1.log runs very well. but it can't descend sort realistic used memory which is RES column after run "top" command.
    It is very important for me to get the final descend sort RES column from "top" command output.
    How to realize memory descend sort .
    Thanks you !

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Try piping it to sort, and telling sort (using the -k flag) to sort by the appropriate field number, e.g.:
    Code:
    top -b -n 1|sort -nr -k6
    note you can get similar output as top using ps, e.g.:
    Code:
    ps auxww

  6. #5
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Afterthought: you might be interested in the pmap utility. It provides a more accurate picture of the memory map of a given process.

    Here is a script that I use to list the memory usage of all processes (identified by pid), to include MAPPED, PRIVATE, and SHARED memory info. If you want the unique memory usage of a given app, you should look at the PRIVATE column, although you should also consider MAPPED and SHARED memory in calculating a processes total usage.

    This script will output all 3 categories, and will sort them by PRIVATE, by default. You can specify any of them by passing a single argument to the script, e.g.:

    Code:
    ./mem_usage.sh mapped
    ./mem_usage.sh private
    ./mem_usage.sh shared
    Here's the script:
    Code:
    #!/bin/bash
    case $1 in
      mapped)  key=2 ;;
      private) key=3 ;;
      shared)  key=4 ;;
      *)
        if [ -n "$1" ]; then
          echo "Usage: $0 {mapped|private|shared}"
          exit 1
        else
          key=2
        fi
    esac
    
    fmt="%-7s%-10s%-10s%-10s%s\n"
    printf $fmt PID MAPPED PRIVATE SHARED COMMAND
    pmap_all(){
      while read line; do
        pid=$(echo "$line"|awk '{print $1}')
        args=$(echo "$line"|sed -e "s|^[[:space:]]*$pid[[:space:]]*||")
        pmapline=$(pmap -d $pid|awk '/^mapped/')
        mapped=$(echo $pmapline|awk '{print $2}')
        private=$(echo $pmapline|awk '{print $4}')
        shared=$(echo $pmapline|awk '{print $6}')
        [ -z "$mapped" -a -z "$private" -a -z "$shared" ] && continue
        printf $fmt $pid "$mapped" "$private" "$shared" "$args"
      done < <(ps -eo pid,cmd --no-heading)
    }
    pmap_all|sort -nr -k${key}

  7. #6
    Just Joined!
    Join Date
    Nov 2012
    Posts
    10
    Quote Originally Posted by atreyu View Post
    Try piping it to sort, and telling sort (using the -k flag) to sort by the appropriate field number, e.g.:
    Code:
    top -b -n 1|sort -nr -k6
    note you can get similar output as top using ps, e.g.:
    Code:
    ps auxww
    Thank atreyu.
    top -b -n 1|sort -nr -k6 will consider the line of RES = 478 K greater than the line of RES = 456M which also greater than the line of RES= 120G,
    because the pipe command sort can be handled only to the value not including the unit(K,M,G).
    this case is impractical under the application, it will give the wrong memory sort result

  8. #7
    Just Joined!
    Join Date
    Nov 2012
    Posts
    10
    my shell version is cshell.
    the above script from #5 cannot run rightly in cshell.
    frankly speaking, I always want to use the method of
    Code:
    top << EOF
    M
    EOF
    with easy to use and understand.
    but there exists some tty get problem.
    it would be much appreciated if atreyu can solve this intractable tty get failed thing for me.
    Last edited by weichanghe2000; 11-13-2012 at 02:05 PM.

  9. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by weichanghe2000 View Post
    top -b -n 1|sort -nr -k6 will consider the line of RES = 478 K greater than the line of RES = 456M which also greater than the line of RES= 120G,
    because the pipe command sort can be handled only to the value not including the unit(K,M,G).
    this case is impractical under the application, it will give the wrong memory sort result
    My top command did not use those units. Looks like you'll have to do some interim processing. Pipe the output of top to a file, then iterate over that file, replacing values in the 6th column containing a K,M,G (or other) suffix with the appropriate value in plain bytes. Then sort the output of that. Here is an example script:

    Code:
    #!/bin/bash
    while read line; do
      res=$(echo $line|awk '{print $6}')
      echo $res|grep -qi ^[0-9]*K$
      if [ $? -eq 0 ]; then
        kb=$(echo $res|sed -e 's|K$||')
        bytes=$(( $kb * 1024 ))
      else
        echo $res|grep -q ^[0-9]*M$
        if [ $? -eq 0 ]; then
          mb=$(echo $res|sed -e 's|M$||')
          bytes=$(( $mb * 1048576 ))
        else
          echo $res|grep -qi ^[0-9]*G$
          if [ $? -eq 0 ]; then
            gb=$(echo $res|sed -e 's|G$||')
            bytes=$(( $gb * 1073741824 ))
          else
            echo $res|grep -qi ^[0-9]*$
            if [ $? -eq 0 ]; then
              bytes=$res
            else
              echo "skipping line containing unreadable RES '$res'"
              continue
            fi
          fi
        fi
      fi
      if [ -n "$bytes" ]; then
        echo "$line"|awk "{\$6=$bytes}{print}"
      else
        echo "$line"
      fi
    done < <(top -b -n 1|awk '/PID/,/\n/')
    It will likely have to be tweaked a little.

  10. #9
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by weichanghe2000 View Post
    my shell version is cshell.
    the above script from #5 cannot run rightly in cshell.
    Did you leave the #!/bin/bash at the top? Did you make it executable? Are you running it like ./script.sh? Show the errors. It should run fine.

    frankly speaking, I always want to use the method of
    Code:
    top << EOF
    M
    EOF
    with easy to use and understand.
    but there exists some tty get problem.
    it would be much appreciated if atreyu can solve this intractable tty get failed thing for me.
    You can use ~/.toprc to make top always run with certain settings (like pressing M). When top is running, press M, then press W and it will save your configuration to ~/.toprc. Next time you run top, it should have those settings you've said from the previous top session.

  11. #10
    Just Joined!
    Join Date
    Nov 2012
    Posts
    10
    this script from #8 looks wonderful, it should be ok,
    thanks atreyu very much for your goodhearted kindhearted help and proposal.
    Last edited by weichanghe2000; 11-14-2012 at 01:21 PM.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •