Search code examples
bashsleepbackground-processcp

Copy every n seconds


I have a sleep and copy function to retrieve daily output of simulations running on a cluster node. It basically looks like this:

sleep_and_copy() {
while true; do
sleep 85600s
copy_data
done
}

where copy_data will perform the copy. The problem is that copy_data takes a lot of time to execute. In other words my copy happens only every 85600s + time to do the copy.

Is there a way to perform the copy exactly every n seconds?


Solution

  • To run your copy script every 24h (=86400s), use a cron job! This also ensures that the task runs after a reboot.

    If you really want to use a script and run it every 85600s, you can simply do the following:

    while true; do
      sleep 85600s
      copy_data &
    done
    

    The & starts whatever copy_data does in a background process and returns immediately.