[SOLVED] how to run a script every second in linux
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
#!/bin/bash
CATALINA_HOME=/usr/local/apache-tomcat-6.0.18/webapps
ls /root/size/200mb > 200limit
for var in $(cut -f1 200limit)
do
#echo $var
cd $CATALINA_HOME
a=`du -sh $var | cut -f1`
echo $a > file
sed -i 's/M//g' file
b=`cut -f1 file`
if [ $b -ge 200 ]
then
cd $CATALINA_HOME/$var
chattr +i ApplicantsAttachments/
chattr +i CallogAttachments/
chattr +i ClientSubmissionAttachments/
chattr +i ImportResumeFolder/
chattr +i ImportResumeTemp/
chattr +i OnBoardedAttachment/
chattr +i RequirementAttachment/
chattr +i TempAttach
else
echo size for $var is lessthan 200M
fi
done
I run this script because our client use to upload documents in our server through our webapplication. But once they reach their 200mb limit they should not upload documents anymore.
I could not use LVM concept since my 90% of server disk size is allotted to root.
so i thought of changing the folder permission once it reaches 200 mb and thats why i need to run my script every second.
But i have one doubt, how to make your script to run in background. shall i run it using cron.
#!/bin/bash
while [ true ]
do
CATALINA_HOME=/usr/local/apache-tomcat-6.0.18/webapps
ls /root/size/200mb > 200limit
for var in $(cut -f1 200limit)
do
#echo $var
cd $CATALINA_HOME
a=`du -sh $var | cut -f1`
echo $a > file
sed -i 's/M//g' file
b=`cut -f1 file`
if [ $b -ge 200 ]
then
cd $CATALINA_HOME/$var
chattr +i ApplicantsAttachments/
chattr +i CallogAttachments/
chattr +i ClientSubmissionAttachments/
chattr +i ImportResumeFolder/
chattr +i ImportResumeTemp/
chattr +i OnBoardedAttachment/
chattr +i RequirementAttachment/
chattr +i TempAttach
else
echo size for $var is lessthan 200M
fi
done
sleep 1
done
Quote:
But i have one doubt, how to make your script to run in background. shall i run it using cron.
I see you use Cent OS, which has an rc.local file (/etc/rc.d/rc.local if I remember correctly). When you add the full path and script name (/full/path/to/script.sh) to this file it will be started when you boot your machine. The while [ true ] part makes sure this script keeps running.
If you're going to run that every second then I'd suggest you work to streamline it. There are several file reads/writes which could be eliminated -- just process the data in memory variables. And you could probably drop the use of sed simply by getting usage in 1M blocks (du -m).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.