LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-18-2011, 08:52 AM   #1
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Rep: Reputation: 18
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.
Old 03-18-2011, 08:54 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Why not write a script that has a loop, which includes a sleep 1?

Hope this helps.
 
Old 03-18-2011, 09:03 AM   #3
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271

Original Poster
Blog Entries: 7

Rep: Reputation: 18
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".
 
Old 03-18-2011, 09:10 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
Old 03-18-2011, 09:26 AM   #5
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271

Original Poster
Blog Entries: 7

Rep: Reputation: 18
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.
 
Old 03-18-2011, 09:38 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
Old 03-18-2011, 10:36 AM   #7
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271

Original Poster
Blog Entries: 7

Rep: Reputation: 18
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
 
Old 03-18-2011, 11:08 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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:

Code:
/root/script.sh &
Sorry about that!
 
1 members found this post helpful.
Old 03-18-2011, 08:01 PM   #9
sleddog
Member
 
Registered: Jan 2002
Location: Labrador, Canada
Distribution: CentOS, Debian
Posts: 182

Rep: Reputation: 35
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 08:03 PM.
 
1 members found this post helpful.
Old 03-19-2011, 06:42 AM   #10
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271

Original Poster
Blog Entries: 7

Rep: Reputation: 18
Hi druuna,

Thanks that works.

Thanks to sleddog too for your suggestion.
 
  


Reply



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
run perl script in linux casualzone Programming 7 08-05-2010 07:05 AM
Run linux script without having to be logged in matsko Linux - Software 5 04-05-2009 02:21 AM
how do I run a perl script in linux izquierdista Linux - Software 6 07-07-2007 10:54 PM
on Network Up Script run? On Battery power run script? v2-ncl Linux - General 0 12-08-2003 09:34 AM
how to run a shell script in Linux chandhru Linux - Newbie 2 09-27-2002 01:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:08 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