Linux - Server This 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.
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.
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.
|
 |
03-18-2011, 09:52 AM
|
#1
|
Member
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Rep:
|
how to run a script every second in linux
Hi all,
I want to run a script every second in linux,but cron allows only to run every minute.
so i googled it and found that it can be run like daemon process.
But i dont know how to do that.Kindly post your suggestions please.
The purpose of my script is to check the space of a folder, and this needs to be run every second.
Thanks in advance,
Dinesh.
|
|
|
Click here to see the post LQ members have rated as the most helpful post in this thread.
|
03-18-2011, 09:54 AM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
Why not write a script that has a loop, which includes a sleep 1?
Hope this helps.
|
|
|
03-18-2011, 10:03 AM
|
#3
|
Member
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Original Poster
Rep:
|
Hi,
can you help me out in this since i dont have much knowledge in this.
i want this command to be executed every second. "/root/script".
|
|
|
03-18-2011, 10:10 AM
|
#4
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
The following is a rough example:
Code:
#!/bin/bash
while [ true ]
do
commands go here
sleep 1
done
The bold part is where your code is placed (probably the content of /root/script).
Once you start this script it will execute the bold part, then wait 1 second (the sleep 1) and start again.
For example: The following will show the size of your home directory, every second
Code:
#!/bin/bash
while [ true ]
do
du -sh ~
sleep 1
done
Without knowing what the content of /root/script is I cannot help you any further at this moment.
Hope this helps.
|
|
3 members found this post helpful.
|
03-18-2011, 10:26 AM
|
#5
|
Member
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Original Poster
Rep:
|
Hi druna,
Thanks for your reply. this is my script.
#!/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.
|
|
|
03-18-2011, 10:38 AM
|
#6
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
This should work:
Code:
#!/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.
Hope this helps.
|
|
2 members found this post helpful.
|
03-18-2011, 11:36 AM
|
#7
|
Member
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Original Poster
Rep:
|
hi,
I have included this in /etc.rc.local file
/root/script.
But system not got booted so i booted through single user mode and remove that entry in /etc/rc.local then my system booted.
Do we have anyother option other than this
|
|
|
03-18-2011, 12:08 PM
|
#8
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
I forgot to mention something important.....
You need to add an ampersand (&) after the script line in /etc/rc.d/rc.local file. I.e:
Sorry about that!
|
|
1 members found this post helpful.
|
03-18-2011, 09:01 PM
|
#9
|
Member
Registered: Jan 2002
Location: Labrador, Canada
Distribution: CentOS, Debian
Posts: 182
Rep:
|
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).
Last edited by sleddog; 03-18-2011 at 09:03 PM.
|
|
1 members found this post helpful.
|
03-19-2011, 07:42 AM
|
#10
|
Member
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Original Poster
Rep:
|
Hi druuna,
Thanks that works.
Thanks to sleddog too for your suggestion.
|
|
|
All times are GMT -5. The time now is 02:59 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
|
|