Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all 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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    16

    run a bash script in every two second


    Hi all
    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

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,217
    Use the sleep command. Make a separate loop script that sleeps for two seconds and then launches the synchroniser script.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  3. #3
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    There is also inittab but I think it refreshes every 5 seconds. I think you're stuck with using a loop, e.g.:
    Code:
    #!/bin/sh
    while :; do
      echo blah
      sleep 2
    done
    When I need to do this type of thing, I usually do it in Perl, and I detach from the terminal and fork the process to the background and make sure another copy of the process is not already running.

  4. #4
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,565
    If you are looking at replicating changes across machines, have a look at incron or lsyncd instead.
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    The Fifth Continent

  5. #5
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,390
    I would recoomend *not* to (r?)sync every 2 seconds.
    This will very likely lead to race conditions, unless proper locking is used.
    Also it is ressource heavy to traverse a directory structure every 2s and build a filelist. Probably most of the time for no actual filecopy at all.

    The solution to your problem maybe is lsyncd - Lsyncd (Live Syncing Daemon) synchronizes local directories with a remote targets - Google Project Hosting
    It is a inotify based synchronistation tool, so it will only take actions, if actually some files were changed.
    Also, it features delayed synchronisation: It can be configured to wait a bit after the first file change before it syncs.
    The logic behind this is to first gather a bunch of changes and sync them at once, instead of mutliple syncs.

    @elija: I need to type faster
    You must always face the curtain with a bow.

  6. #6
    Just Joined!
    Join Date
    Jun 2011
    Posts
    1
    Hi Shibupthomas,

    A simple method for running a script every two seconds is to use the watch command. By default it runs every two seconds but you can change that with the -n switch. Here's an example:

    Code:
    #!/bin/sh
    watch -n 1 'wpa_cli scan && wpa_cli scan_r'
    The watch command uses wpa_cli to scan for wireless access points and then prints the results every second.
    Make this script executable and run it as root. If you get an error message upon running this script, then try this one:

    Code:
    #!/bin/sh
    wpa_supplicant -B -D wext -i eth1 -C /var/run/wpa_supplicant &    #Replace wext with the driver your wireless card uses(wext should work for most) and replace eth1 with your wireless interface name
    watch -n 1 'wpa_cli scan && wpa_cli scan_r' &&    #Monitor wireless access points in "real time"
    killall wpa_supplicant    #This will restart wpa_supplicant just in case your network management program doesn't play nice
    Feel free to email me if you have any questions:
    losstfeedback@gmail.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •