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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-20-2010, 10:48 PM
|
#1
|
|
LQ Newbie
Registered: Jul 2010
Posts: 3
Rep:
|
Shell script to find a file size
Hi there, am new to linux and shell script,am trying to write a shell script to find the size of a particular log file and if the log size grows, script should mail the changes to the administrator or a any user so script should monitor the log file continuously in a time interval, how can i do that?
I tried with these codes to find the file size but it throws me error says command not found
$s=$( stat -c %s mylogfile.log)
echo $s
|
|
|
|
07-21-2010, 12:41 AM
|
#2
|
|
Member
Registered: May 2003
Location: India
Distribution: Linux, SUN, AIX, (HP-UX 11.11 11.00 and 11.23 IPF PA-RISC), CYGWIN
Posts: 145
Rep:
|
You can use the following:
Code:
mylogfileSize=$(stat -c %s mylogfile.log)
or
Code:
s=$(stat -c %s mylogfile.log)
echo $s
|
|
|
|
07-22-2010, 01:23 AM
|
#3
|
|
LQ Newbie
Registered: Jul 2010
Posts: 3
Original Poster
Rep:
|
that works
Quote:
Originally Posted by murugesan
You can use the following:
Code:
mylogfileSize=$(stat -c %s mylogfile.log)
or
Code:
s=$(stat -c %s mylogfile.log)
echo $s
|
Thanku, thats works, but how can i make the script monitor the log file continuously with some time interval
|
|
|
|
07-22-2010, 01:47 AM
|
#4
|
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 3,965
|
Code:
while true; do
<something>
sleep 10
done
However, for proper fs monitoring nowadays I would use inotify. There are tools like inotifywait and inotifywatch that can help on that. That way you don't have to resort to periodic checks.
|
|
|
|
07-22-2010, 01:47 AM
|
#5
|
|
LQ Newbie
Registered: Mar 2010
Location: Thessaloniki, Greece
Distribution: Debian, Ubuntu, CentOS, Fedora
Posts: 11
Rep:
|
You can schedule the script to execute every x minutes, hours, etc using crontab. In order to do that, log in as the user with which you would
like your script to be executed and from there type the following commands (I assume that you are connected as root):
root# crontab -e --in order to open the file that schedules your command
and then enter the parameters for your script to be executed every x mins, hours, etc. For example, if your script is located in
/usr/bin/myscript.sh and you would like it to be executed at 16 and 18 hours every day you should type:
Code:
0 16,18 * * * /usr/bin/myscript.sh
or if you like it to execute every 3 hours every day at 0 minutes in every hour you should type:
Code:
0 */3 * * * /usr/bin/myscript.sh
For more information on cron check this out ... 
|
|
|
|
07-23-2010, 01:07 AM
|
#6
|
|
Member
Registered: May 2003
Location: India
Distribution: Linux, SUN, AIX, (HP-UX 11.11 11.00 and 11.23 IPF PA-RISC), CYGWIN
Posts: 145
Rep:
|
Here goes the other ways for using while loop:
Code:
#!/bin/ksh
# Comment: Script to find the size of a particular log file: "murugesan.log"
# Comment: Other ways
initSec=`date +%s`
currSec=$initSec
while [ `expr $currSec - $initSec` -le 9 ]
do
s=$(stat -c %s murugesan.log)
if [ $s -gt 1024 ]
then
# Comment: You can redirect this to some file.
echo "Size of logfile murugesan.log is $s" | mail username@hostname
# Comment break this script if required.
break;
fi
# Comment: sleep for required time
sleep 1
currSec=`date +%s`
done
# Comment: Other ways
initSec=`date +%s`
currSec=$initSec
while [ 1 ]
do
currSec=`date +%s`
if [ `expr $currSec - $initSec` -gt 9 ]
then
break
fi
s=$(stat -c %s murugesan.log)
# Comment: You can redirect this to some other file or mail this if required
echo OTHERWAY3:$s
# Comment: sleep for required time
sleep 1
done
http://geocities.ws/murugesan/techni...izeofFile.html
|
|
|
|
06-16-2011, 03:42 PM
|
#7
|
|
LQ Newbie
Registered: Jun 2011
Posts: 8
Rep: 
|
I want to do something similar and I'm a complete noob.
I'm trying to setup a cron job that work every 2min and checks if filesize is changed (doesn't equal to 2152 bytes), if it's not equal to 2152 bytes than it must take the original file from location A and copy it to the location B where the changed file is stored and replace it.
I'll appreciate your feedback's on the issue.
Dangel7.
Last edited by Dangel7; 06-16-2011 at 03:44 PM.
|
|
|
|
06-16-2011, 08:11 PM
|
#8
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,985
|
If you need to continuously monitor a log file and perform certain actions, Perl has a module for that http://search.cpan.org/~mgrabnar/Fil...0.99.3/Tail.pm
@Dangel7: show us what you've done so far
|
|
|
|
06-17-2011, 06:32 AM
|
#9
|
|
LQ Newbie
Registered: Jun 2011
Posts: 8
Rep: 
|
Thank you for trying to help me.
I'm thinking of doing this :
First : root# crontab -e
*/1 * * * * location of the script/myscript.sh - (I want the cron to work every minute)
Inside the myscript.sh I wrote :
if [ $ stat -s file.tpl -ne 2152 ]; then
$ cp -f location of the backup file/myscript.sh dir1 location of the changed file.
Thats all I have so far.
Last edited by Dangel7; 06-17-2011 at 07:19 AM.
|
|
|
|
06-20-2011, 01:06 PM
|
#11
|
|
LQ Newbie
Registered: Nov 2010
Posts: 14
Rep:
|
@Dangel7, why is your file changing in the first place? Perhaps what you really need is to change permissions so that only you (or other authorized users) can replace the file.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:45 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|