Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, guys, I want to redirect the result of sort memory into a file after running top command . I pressed key M will sort by memory, and then press ...
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 realize automatically these operation process using Expect


    Hi, guys, I want to redirect the result of sort memory into a file after running top command .

    I pressed key M will sort by memory, and then press key W will save all current setting, press key q will exit when I launched top command,
    it will be able to display exactly the same as in the previous format.

    Code:
    #!/usr/bin/expect
    set timeout -1                                     
    spawn top
    expect -re ".*" { sleep 2; send "M\r" }    
    expect -re ".*" { sleep 2; send "W\r" }  
    expect -re ".*" {  "q\r" }      
    send "exit\r"         
    expect eof
    But above code cannot work as my desiring:
    it will not sort RES column and will not write configuration into ~/.toprc file and will not exit top.

    My question is:
    1. How to make above all operation automatically in a script using linux Expect Language?
    2. How to save sorted output into a logfile when key M is pressed which sorted RES column of top command ?

    Thanks in advance!
    I am looking forward to your reply and help.
    Last edited by weichanghe2000; 05-11-2013 at 01:49 PM.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hi,

    i'd avoid expect in this instance. instead, start top, and while in it, press "M" to sort by memory. Now press "W", and you should see a message at the top saying

    Code:
    Wrote configuration to '~/.toprc'
    Now exit. Now start top again to make sure that it is sorting by memory. Now exit top. Now start top in batch mode and run it once and exit, using this syntax:

    Code:
    top -c -b -n 1 > top.txt
    now check out "top.txt" and see if it has what you want.

  3. #3
    Just Joined!
    Join Date
    Nov 2012
    Posts
    10
    Thanks atreyu firstly!
    Although it can be a workaround which is reluctant and not so perfect without too much challenge and lack of complex technology research.
    It is important for me to automatically realize all process in a script.
    Last edited by weichanghe2000; 05-12-2013 at 11:51 AM.

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by weichanghe2000 View Post
    It is important for me to automatically realize all process in a script.
    sorry, i'm not tracking...can you be more specific about what you need to do in all scripts?

  6. #5
    Just Joined!
    Join Date
    Nov 2012
    Posts
    10
    Quote Originally Posted by atreyu View Post
    sorry, i'm not tracking...can you be more specific about what you need to do in all scripts?
    Thank Your reply.
    There are two specification:
    1. The script can orderly simulate the key M, key W, key q effect of top command, make all process automatically.
    I can share this script to my friends
    2. save the result into a file after descending sorted memory.

    Thanks Your help.

  7. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    if you really want to send keys automatically, you could use the screen command - it is like a terminal. you can start a program in it and send key sequences to it using screen's stuff command.

  8. #7
    Just Joined!
    Join Date
    Nov 2012
    Posts
    10
    Thanks, I will study on screen to check if it can achieve my goal.

Posting Permissions

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