LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script to find a file size (http://www.linuxquestions.org/questions/linux-newbie-8/shell-script-to-find-a-file-size-821153/)

sharincharles 07-20-2010 10:48 PM

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

murugesan 07-21-2010 12:41 AM

You can use the following:
Code:

mylogfileSize=$(stat -c %s mylogfile.log)
or
Code:

s=$(stat -c %s mylogfile.log)
echo $s


sharincharles 07-22-2010 01:23 AM

that works
 
Quote:

Originally Posted by murugesan (Post 4040276)
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

i92guboj 07-22-2010 01:47 AM

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.

Filip_Kv 07-22-2010 01:47 AM

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 ... ;)

murugesan 07-23-2010 01:07 AM

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

Dangel7 06-16-2011 03:42 PM

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.

chrism01 06-16-2011 08:11 PM

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

Dangel7 06-17-2011 06:32 AM

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.

chrism01 06-19-2011 11:23 PM

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/

scott8035 06-20-2011 01:06 PM

@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.


All times are GMT -5. The time now is 01:33 AM.