LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-19-2010, 07:16 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 37
Time limit in script.


How would i implement time limit.
For example I m thinking of writing a script which will monitor the RAM performance for given amount of time and give me the RAM status.

I will be using output of free -m and i would like to consider values of free -m for given amount of time with 1 second interval.

How can i establish this?
 
Old 07-19-2010, 07:20 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Sounds like the most complex thing about this idea will be the 'sleep' command inside a loop, with a counter to count each iteration until you have reached the 'given amount of time' for which you wish to do this loop.
 
Old 07-19-2010, 07:29 AM   #3
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by GrapefruiTgirl View Post
Sounds like the most complex thing about this idea will be the 'sleep' command inside a loop, with a counter to count each iteration until you have reached the 'given amount of time' for which you wish to do this loop.

Code:
echo "Enter Time Interval"
read inputtime
i=1
while [ $i -lt $inputtime ]
do
free -m /tmp/raminfo
sleep 1
i=$( expr $i + 1 )
done

will this do?
if atall this works what will happen if i run 2 instances of the script?
Will two instances write to same file ? I thing this will be very dangerous for the system. please suggest.

Last edited by pinga123; 07-19-2010 at 07:41 AM.
 
Old 07-19-2010, 07:38 AM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Code:
inputtimeinsec=300; count=0

while [ $count -le $inputtimeinsec ]
do
  free -m > /tmp/tmp.txt
  sleep 2
  count=$((count+1))
done
You got the right idea. I have changed -eq to -le, otherwise your loop would never have operated. Also, you were missing a mechanism to keep track of how many loops had occurred, so it never would have compared for the 300 increments.

Your sleep of 2 will cause a total of 300 checks of `free` over 600 seconds (probably a little more than 600 exactly, due to the time consumed actually doing running the program; you would need to check for actual time passed if you wanted this to run for an absolute amount of time).

Finally, I changed the ( ) to [ ] for proper syntax with the while loop, and added the $ on $count.

Should work now.

EDIT - put the right slashes! Forward slashes on Linux for file paths, not \\\ backslashes.

Last edited by GrapefruiTgirl; 07-19-2010 at 07:41 AM.
 
Old 07-19-2010, 07:43 AM   #5
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by GrapefruiTgirl View Post
Code:
inputtimeinsec=300; count=0

while [ $count -le $inputtimeinsec ]
do
  free -m > /tmp/tmp.txt
  sleep 2
  count=$((count+1))
done
You got the right idea. I have changed -eq to -le, otherwise your loop would never have operated. Also, you were missing a mechanism to keep track of how many loops had occurred, so it never would have compared for the 300 increments.

Your sleep of 2 will cause a total of 300 checks of `free` over 600 seconds (probably a little more than 600 exactly, due to the time consumed actually doing running the program; you would need to check for actual time passed if you wanted this to run for an absolute amount of time).

Finally, I changed the ( ) to [ ] for proper syntax with the while loop, and added the $ on $count.

Should work now.

EDIT - put the right slashes! Forward slashes on Linux for file paths, not \\\ backslashes.
Thanks for your reply but this only answers my first query.
What about the second one.
if at all this works what will happen if i run 2 instances of the script?
Will two instances write to same file ? I thing this will be very dangerous for the system. please suggest.
 
Old 07-19-2010, 07:47 AM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
It won't be dangerous at all, but your multiple instances of the script will be writing to the same file, so the results will be jumbled up. Also, I neglected to indicate the using a single > sign will repeatedly overwrite the output file; use a >> instead if you want to keep adding to the file.
 
Old 07-19-2010, 08:06 AM   #7
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
what can be done to avoid this?
 
Old 07-19-2010, 08:09 AM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
To avoid "what" exactly? Both scripts writing to the same file? Use a different file for each script.
 
Old 07-19-2010, 08:21 AM   #9
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
If you want only one instance to run, then a simple way might be to use a small control file with path fully qualified . ie. not a relative path as a flag. If the file exists, then some other user is using it. You can put all sorts of other information in thatt file. Eg. tty of the user who has created the file ... On EOJ, the shell script would kill the file.
 
Old 07-19-2010, 08:38 AM   #10
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
another option is to use some variables to make the log file-names unique. for my scripts i usually use the pid of the process ($$), or a date-time stamp (see: date), or a combination of the two, to ensure uniqueness. the only issue is cleanup; you just need to make sure the tmp log files get deleted at some point, or they would just clutter-up your directory and consume all free space.

hth.

-RN.
 
Old 07-19-2010, 11:23 PM   #11
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by rn_ View Post
another option is to use some variables to make the log file-names unique. for my scripts i usually use the pid of the process ($$), or a date-time stamp (see: date), or a combination of the two, to ensure uniqueness. the only issue is cleanup; you just need to make sure the tmp log files get deleted at some point, or they would just clutter-up your directory and consume all free space.

hth.

-RN.
thats a good idea to use process id as filename .Is it possible for you to share the script?
 
Old 07-20-2010, 02:47 AM   #12
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
I don't think exec'ing free (or any other program) every second is a good idea from performance point of view.
Don't top has all you need - i.e
Code:
top -b -d <delay>  -n <number_of_iteration>
 
Old 07-20-2010, 10:36 PM   #13
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by Valery Reznic View Post
I don't think exec'ing free (or any other program) every second is a good idea from performance point of view.
Don't top has all you need - i.e
Code:
top -b -d <delay>  -n <number_of_iteration>
yep that can be possible.I m searching for a better answer for not to use free -m . Is there any known issue. as i didnt find any performance issue in executing free -m per second.
I guess it should be equal to top utilization.
 
Old 07-23-2010, 08:24 AM   #14
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
Quote:
Originally Posted by pinga123 View Post
thats a good idea to use process id as filename .Is it possible for you to share the script?
just one way of doing it:

Code:
free -m -s 2 | head -$(( ${1:-5} * 5 )) >> /tmp/free_report.$$.txt
this boils it down to a single line script that produces an output every 2 seconds and uses an optional command-line arg for the number of iterations. if you don't specify the arg, it uses 5 as default. the # of iterations is multiplied by 5 because the output of free produces 5 lines, and head will only take the # of iterations times 5 lines and place them in the temp file. The temp file name has a $$ in it which will be replaced by the process id.

save this line to a script; say free_report.sh, and to execute use: './free_report.sh 10' for eg.

another way to do it would be to use 'top' as suggested by Valery.

and yet another way to do it would be to cat the /proc/meminfo file.

good luck.

HTH.

-RN.
 
Old 07-25-2010, 11:20 PM   #15
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Thank you for the useful information u guys shared with me.
Now i have got all the necessary logic for my script.
But as mentioned in above posts by various users there are 3 ways of doing the same thing.
i m listing them one by one.
1)Using output of top.
2)Using output of free -m.
3)Using /proc/meminfo.
Since my script is going to monitor the performance i dont want it to add extra load on system.Hence want to evaluate best among the above described methods.
 
  


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
is it possible to limit time for users? glenn69 Linux - General 1 09-27-2007 09:02 PM
SquidGuard time limit problem scjvsTP Linux - Networking 2 02-25-2006 11:11 PM
Bootloader Time Limit KissDaFeetOfSean Linux - Newbie 3 06-15-2005 11:54 PM
time limit for select jhon Linux - Networking 1 09-08-2004 09:03 AM
killing process at time limit hfawzy Linux - General 0 05-31-2003 03:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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