LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-20-2010, 10:48 PM   #1
sharincharles
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Rep: Reputation: 0
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
 
Old 07-21-2010, 12:41 AM   #2
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
You can use the following:
Code:
mylogfileSize=$(stat -c %s mylogfile.log)
or
Code:
s=$(stat -c %s mylogfile.log)
echo $s
 
1 members found this post helpful.
Old 07-22-2010, 01:23 AM   #3
sharincharles
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Original Poster
Rep: Reputation: 0
that works

Quote:
Originally Posted by murugesan View Post
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
 
Old 07-22-2010, 01:47 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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.
 
Old 07-22-2010, 01:47 AM   #5
Filip_Kv
LQ Newbie
 
Registered: Mar 2010
Location: Thessaloniki, Greece
Distribution: Debian, Ubuntu, CentOS, Fedora
Posts: 11

Rep: Reputation: 2
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 ...
 
Old 07-23-2010, 01:07 AM   #6
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
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
 
Old 06-16-2011, 03:42 PM   #7
Dangel7
LQ Newbie
 
Registered: Jun 2011
Posts: 8

Rep: Reputation: Disabled
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.
 
Old 06-16-2011, 08:11 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
Old 06-17-2011, 06:32 AM   #9
Dangel7
LQ Newbie
 
Registered: Jun 2011
Posts: 8

Rep: Reputation: Disabled
Post

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.
 
Old 06-19-2011, 11:23 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
For anything more frequent than every 5 mins, I'd write a daemon instead.
Remember that every time a cronjob starts, it has to create a whole new process environment. This is going to add to your performance load.


Useful bash links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 06-20-2011, 01:06 PM   #11
scott8035
LQ Newbie
 
Registered: Nov 2010
Posts: 14

Rep: Reputation: 4
@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.
 
  


Reply

Tags
script, shell



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
to find if a file exist using shell script nanxy Programming 8 09-16-2013 10:30 PM
shell script - how to check file size noir911 Programming 3 01-24-2009 09:17 AM
shell script to determine if file size equals zero jonfa Programming 5 12-11-2007 04:48 PM
File Size Shell Script BlackLabel Programming 7 11-27-2007 07:48 PM
Shell script: how to find a file containing a string guarriman General 2 08-27-2007 03:39 AM

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

All times are GMT -5. The time now is 03:02 PM.

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