Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
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.
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.
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.
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.