FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Redhat > Red Hat Install

 
 
LinkBack Thread Tools
 
Old 06-29-2011, 07:08 AM
ShibuThomas
 
Default Run a script automatically in every 2 sec.

Hi ricks,

I wrote a one shell script which contains code for synchronize files
between two machines.I want to execute this script every 2 second.How
can I do this?.When I gone through net found that usually cron job is
used for doing same.But the minimum time frame in cron is 1 minitues.One
method is to keep a loop inside my script and execute it.But I would
like to know is there any other way to do the same.
So how can execute my script every 2 second.Please provide some example
code.


Regards,
ShibuThomas



_______________________________________________
Redhat-install-list mailing list
Redhat-install-list@redhat.com
https://www.redhat.com/mailman/listinfo/redhat-install-list
To Unsubscribe Go To ABOVE URL or send a message to:
redhat-install-list-request@redhat.com
Subject: unsubscribe
 
Old 06-29-2011, 04:22 PM
Rick Stevens
 
Default Run a script automatically in every 2 sec.

On 06/29/2011 12:08 AM, ShibuThomas wrote:
> Hi ricks,
>
> I wrote a one shell script which contains code for synchronize files
> between two machines.I want to execute this script every 2 second.How
> can I do this?.When I gone through net found that usually cron job is
> used for doing same.But the minimum time frame in cron is 1 minitues.One
> method is to keep a loop inside my script and execute it.But I would
> like to know is there any other way to do the same.
> So how can execute my script every 2 second.Please provide some example
> code.

Good lord! Every 2 seconds? That's a bit extreme.

Yes, the minimum granularity for a cron job is 1 minute. If you need
finer granularity, you need to run a script that does a sleep call:

#!/bin/bash
while /bin/true; do
rsync (or whatever) >/dev/null 2>&1
sleep 2
done

This will do the rsync command (discarding any messages), wait for
two seconds, then repeat.

I really, REALLY recommend you do NOT do this as it can be a massive
resource hog. If the two machines are on the same LAN, the best bet is
to share the target directory between them using NFS rather than doing
some sync job.

Alternately, look at the inotifywait stuff. You can put a watch on a
directory on the source machine and only launch the sync if a file is
written in it. You'd need to "yum install inotify-tools", and here's an
example script:

------------------------------- CUT HERE -----------------------------
#!/bin/bash
# Filename: watchuploads.sh
# Author: Rick Stevens, AllDigital, Inc.
# Last Edit: 29 March 2011
#
# Synopsis:
# This script uses inotifywait to watch for newly uploaded files
# in the /opt/uploads directory. When something is uploaded, it
# runs rsync to copy the file to another machine.
#
# NOTES:
# It's probably best to run this script in a screen(1) session or
# via "nohup /usr/local/bin/watchuploads.sh >/dev/null 2>&1 &".
#

# Set up some variables to make life easier...
WATCHPT="/opt/uploads" # Directory to watch for uploads

# And away we go...
while /bin/true; do # Start infinite loop
FNAME=`inotifywait -e close_write --format "%f" $WATCHPT
2>/dev/null` # Watch for changes and grab
# filename
nohup rsync $WATCHPT/$FNAME remotemachine:$WATCHPT/
>>/dev/null 2>&1 & # Launch rsync
done
------------------------------- CUT HERE -----------------------------

This is FAR more efficient than doing a sleep(1) and won't put as much
of a load on the system. Look at the man pages for inotifywait(1),
inotifywatch(1) and inotify(7).
----------------------------------------------------------------------
- Rick Stevens, Systems Engineer, C2 Hosting ricks@nerd.com -
- AIM/Skype: therps2 ICQ: 22643734 Yahoo: origrps2 -
- -
- Charter Member of the International Sarcasm Society -
- "Yeah, like we need YOUR support!" -
----------------------------------------------------------------------

_______________________________________________
Redhat-install-list mailing list
Redhat-install-list@redhat.com
https://www.redhat.com/mailman/listinfo/redhat-install-list
To Unsubscribe Go To ABOVE URL or send a message to:
redhat-install-list-request@redhat.com
Subject: unsubscribe
 
Old 06-29-2011, 04:22 PM
Rick Stevens
 
Default Run a script automatically in every 2 sec.

On 06/29/2011 12:08 AM, ShibuThomas wrote:
> Hi ricks,
>
> I wrote a one shell script which contains code for synchronize files
> between two machines.I want to execute this script every 2 second.How
> can I do this?.When I gone through net found that usually cron job is
> used for doing same.But the minimum time frame in cron is 1 minitues.One
> method is to keep a loop inside my script and execute it.But I would
> like to know is there any other way to do the same.
> So how can execute my script every 2 second.Please provide some example
> code.

Good lord! Every 2 seconds? That's a bit extreme.

Yes, the minimum granularity for a cron job is 1 minute. If you need
finer granularity, you need to run a script that does a sleep call:

#!/bin/bash
while /bin/true; do
rsync (or whatever) >/dev/null 2>&1
sleep 2
done

This will do the rsync command (discarding any messages), wait for
two seconds, then repeat.

I really, REALLY recommend you do NOT do this as it can be a massive
resource hog. If the two machines are on the same LAN, the best bet is
to share the target directory between them using NFS rather than doing
some sync job.

Alternately, look at the inotifywait stuff. You can put a watch on a
directory on the source machine and only launch the sync if a file is
written in it. You'd need to "yum install inotify-tools", and here's an
example script:

------------------------------- CUT HERE -----------------------------
#!/bin/bash
# Filename: watchuploads.sh
# Author: Rick Stevens, AllDigital, Inc.
# Last Edit: 29 March 2011
#
# Synopsis:
# This script uses inotifywait to watch for newly uploaded files
# in the /opt/uploads directory. When something is uploaded, it
# runs rsync to copy the file to another machine.
#
# NOTES:
# It's probably best to run this script in a screen(1) session or
# via "nohup /usr/local/bin/watchuploads.sh >/dev/null 2>&1 &".
#

# Set up some variables to make life easier...
WATCHPT="/opt/uploads" # Directory to watch for uploads

# And away we go...
while /bin/true; do # Start infinite loop
FNAME=`inotifywait -e close_write --format "%f" $WATCHPT
2>/dev/null` # Watch for changes and grab
# filename
nohup rsync $WATCHPT/$FNAME remotemachine:$WATCHPT/
>>/dev/null 2>&1 & # Launch rsync
done
------------------------------- CUT HERE -----------------------------

This is FAR more efficient than doing a sleep(1) and won't put as much
of a load on the system. Look at the man pages for inotifywait(1),
inotifywatch(1) and inotify(7).
----------------------------------------------------------------------
- Rick Stevens, Systems Engineer, C2 Hosting ricks@nerd.com -
- AIM/Skype: therps2 ICQ: 22643734 Yahoo: origrps2 -
- -
- Charter Member of the International Sarcasm Society -
- "Yeah, like we need YOUR support!" -
----------------------------------------------------------------------

_______________________________________________
Redhat-install-list mailing list
Redhat-install-list@redhat.com
https://www.redhat.com/mailman/listinfo/redhat-install-list
To Unsubscribe Go To ABOVE URL or send a message to:
redhat-install-list-request@redhat.com
Subject: unsubscribe
 
Old 07-01-2011, 03:00 AM
ShibuThomas
 
Default Run a script automatically in every 2 sec.

Hi Ricks,

I tried the following.Its working fine.

But instead of rsync we are using unison.

when i tried to use unison.it is not working

The only change i made is

nohup unison $WATCHPT/$FNAME remotemachine:$WATCHPT/
>>/dev/null 2>&1 &
unison is installed in my pc.and i checked this unison working.that was
fine.but with above code it is not working for me.

can you please help me.



Regrads

shibuthomas

Rick Stevens wrote:

On 06/29/2011 12:08 AM, ShibuThomas wrote:


Hi ricks,

I wrote a one shell script which contains code for synchronize files
between two machines.I want to execute this script every 2 second.How
can I do this?.When I gone through net found that usually cron job is
used for doing same.But the minimum time frame in cron is 1 minitues.One
method is to keep a loop inside my script and execute it.But I would
like to know is there any other way to do the same.
So how can execute my script every 2 second.Please provide some example
code.



Good lord! Every 2 seconds? That's a bit extreme.

Yes, the minimum granularity for a cron job is 1 minute. If you need
finer granularity, you need to run a script that does a sleep call:

#!/bin/bash
while /bin/true; do
rsync (or whatever) >/dev/null 2>&1
sleep 2
done

This will do the rsync command (discarding any messages), wait for
two seconds, then repeat.

I really, REALLY recommend you do NOT do this as it can be a massive
resource hog. If the two machines are on the same LAN, the best bet is
to share the target directory between them using NFS rather than doing
some sync job.

Alternately, look at the inotifywait stuff. You can put a watch on a
directory on the source machine and only launch the sync if a file is
written in it. You'd need to "yum install inotify-tools", and here's an
example script:

------------------------------- CUT HERE -----------------------------
#!/bin/bash
# Filename: watchuploads.sh
# Author: Rick Stevens, AllDigital, Inc.
# Last Edit: 29 March 2011
#
# Synopsis:
# This script uses inotifywait to watch for newly uploaded files
# in the /opt/uploads directory. When something is uploaded, it
# runs rsync to copy the file to another machine.
#
# NOTES:
# It's probably best to run this script in a screen(1) session or
# via "nohup /usr/local/bin/watchuploads.sh >/dev/null 2>&1 &".
#

# Set up some variables to make life easier...
WATCHPT="/opt/uploads" # Directory to watch for uploads

# And away we go...
while /bin/true; do # Start infinite loop
FNAME=`inotifywait -e close_write --format "%f" $WATCHPT
2>/dev/null` # Watch for changes and grab
# filename
nohup rsync $WATCHPT/$FNAME remotemachine:$WATCHPT/
>>/dev/null 2>&1 & # Launch rsync
done
------------------------------- CUT HERE -----------------------------

This is FAR more efficient than doing a sleep(1) and won't put as much
of a load on the system. Look at the man pages for inotifywait(1),
inotifywatch(1) and inotify(7).
----------------------------------------------------------------------
- Rick Stevens, Systems Engineer, C2 Hosting ricks@nerd.com -
- AIM/Skype: therps2 ICQ: 22643734 Yahoo: origrps2 -
- -
- Charter Member of the International Sarcasm Society -
- "Yeah, like we need YOUR support!" -
----------------------------------------------------------------------







_______________________________________________
Redhat-install-list mailing list
Redhat-install-list@redhat.com
https://www.redhat.com/mailman/listinfo/redhat-install-list
To Unsubscribe Go To ABOVE URL or send a message to:
redhat-install-list-request@redhat.com
Subject: unsubscribe
 

Thread Tools




All times are GMT. The time now is 09:04 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org