Find the answer to your Linux question:
Results 1 to 6 of 6
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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.


  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Harrow, UK
    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!"

  3. #3
    There is also inittab but I think it refreshes every 5 seconds. I think you're stuck with using a loop, e.g.:
    while :; do
      echo blah
      sleep 2
    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. $spacer_open
  5. #4
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Either at home or at work or down the pub
    If you are looking at replicating changes across machines, have a look at incron or lsyncd instead.
    Should you be sitting wondering,
    Which Batman is the best,
    There's only one true answer my friend,
    It's Adam Bloody West!

    The Fifth Continent

  6. #5
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    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.

  7. #6
    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:

    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:

    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:

Posting Permissions

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