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.
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
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.
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:
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
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
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.
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.
@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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.