LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-25-2014, 05:01 AM   #1
rohitchauhan
Member
 
Registered: Nov 2010
Distribution: RedHat
Posts: 97

Rep: Reputation: Disabled
unable to redirect command output into a file


Hi guys,
I need a little help in scripting.

Code:
[root@base ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          7766       6181       1585          0        200       1270
-/+ buffers/cache:       4710       3056
Swap:         5999         67       5932
I want this output in every 1 min repeatedly. I want to redirect value "3056" into a file.

Therefore, if I run the following command:
# free -m | grep buffers | tail -n 1 | awk '{print $4}' > test1
which is successful.

but when I run the command using "-s" option, it won't work.
# free -m -s 2 | grep buffers | tail -n 1 | awk '{print $4}' > test1


Can anybody tell me:
1. Why it is not working if I use '-s' switch ?
2. If it is not possible to use '-s' switch here, then is there any other way to run this command in every 2 seconds and get the require result into a file?

(If possible, I would prefer one liner script/command)

Thanks.

Last edited by rohitchauhan; 03-25-2014 at 05:03 AM.
 
Old 03-25-2014, 05:11 AM   #2
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,278

Rep: Reputation: 148Reputation: 148
Have a look on this thread
http://www.linuxquestions.org/questi...ommand-739669/
 
1 members found this post helpful.
Old 03-25-2014, 05:16 AM   #3
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,340

Rep: Reputation: Disabled
It doesn't work because the "-s 2" switch means "continue to write new statistics every 2 seconds", which means the output never ends, and this conflicts with two other commands through which you're piping the output.

tail -n 1 means "wait for end-of-file and output last line". Since free produces continuous output with the "-s 2" switch, tail will never see the end of the file, and hence produce no output.

There is a similar issue with the grep/awk combo; see kirukan's link for more information.
 
1 members found this post helpful.
Old 03-26-2014, 01:12 AM   #4
rohitchauhan
Member
 
Registered: Nov 2010
Distribution: RedHat
Posts: 97

Original Poster
Rep: Reputation: Disabled
Thanks to both of you for quick and specific answers.
I get this thing running and working fine for me.
Code:
[root@base ~]# free -m -s 2 |  awk '/buffers\/cache/{print $4;fflush()}' > op &
[1] 13580
[root@base ~]#
[root@base ~]# kill -9 13580
[root@base ~]#
[root@base ~]# cat op
3030
3030
3030
3030
[1]+  Killed                  free -m -s 2 | awk '/buffers\/cache/{print $4;fflush()}' > op
But I cant understand the use of "fflush()" in the command.

Can you please help me in understanding this ?

Thank you !
 
Old 03-26-2014, 01:31 AM   #5
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,278

Rep: Reputation: 148Reputation: 148
Does this manpage make sense?
http://manpages.courier-mta.org/htmlman3/fflush.3.html
 
Old 03-27-2014, 12:27 AM   #6
rohitchauhan
Member
 
Registered: Nov 2010
Distribution: RedHat
Posts: 97

Original Poster
Rep: Reputation: Disabled
Thank you very much.

Now i will mark this thread as solved.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to redirect the SCP command output to text file. gokuls Linux - General 10 02-22-2012 02:12 AM
Redirect output of a command to another file inside shell script mukul_d Linux - General 4 08-27-2010 02:23 PM
How to redirect the output of exec command output in perl? sudhap85 Linux - Newbie 3 08-28-2009 03:35 PM
Unable to redirect script output to a file alien12 Linux - Newbie 4 08-25-2009 03:36 AM
How to redirect standard output of piped command to log file andrewstr Linux - General 10 02-04-2004 02:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:18 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