Skip to main content
Add a fixed loop duration regardless of work length
Source Link
Chris Davies
  • 128k
  • 16
  • 178
  • 323

Use an array to contain the pair of hosts. Initially the array can contain just a single element, and you can add the second host later. Here I've included two sets of credentials so you can see the syntax. For the copying I've chosen to use rsync but you could revert to scp if you preferred.

#!/bin/bash

# Remote credentials
remotes=('user@remote1' 'user@remote2')

# Enable data acquisition service
for remote in "${remotes[@]}"
do
    ssh -n "$remote" 'systemctl start my.service'
done

# Transfer data file everywith an interval of 60 seconds, until SIGINT from host
while sleep 60
do
    for remote in "${remotes[@]}"
    do
        rsync -tz --contimeout=10 "$remote:/data1.txt" /host/
        # scp -pC "$remote:/data1.txt" /host/
    done
done

# Forever

The loop delays 60 seconds between executions, so if the copies take 15 seconds each you'll actually loop every 90 seconds. If you want the loop to aim for a minute cycle regardless of the copying duration you can use bash's built-in clock:

interval=60 start=$SECONDS
while sleep $(( interval - ((SECONDS - start) % interval) ))
do
    …
done

Use an array to contain the pair of hosts. Initially the array can contain just a single element, and you can add the second host later. Here I've included two sets of credentials so you can see the syntax. For the copying I've chosen to use rsync but you could revert to scp if you preferred.

#!/bin/bash

# Remote credentials
remotes=('user@remote1' 'user@remote2')

# Enable data acquisition service
for remote in "${remotes[@]}"
do
    ssh -n "$remote" 'systemctl start my.service'
done

# Transfer data file every 60 seconds, until SIGINT from host
while sleep 60
do
    for remote in "${remotes[@]}"
    do
        rsync -tz --contimeout=10 "$remote:/data1.txt" /host/
        # scp -pC "$remote:/data1.txt" /host/
    done
done

# Forever

Use an array to contain the pair of hosts. Initially the array can contain just a single element, and you can add the second host later. Here I've included two sets of credentials so you can see the syntax. For the copying I've chosen to use rsync but you could revert to scp if you preferred.

#!/bin/bash

# Remote credentials
remotes=('user@remote1' 'user@remote2')

# Enable data acquisition service
for remote in "${remotes[@]}"
do
    ssh -n "$remote" 'systemctl start my.service'
done

# Transfer data file with an interval of 60 seconds, until SIGINT
while sleep 60
do
    for remote in "${remotes[@]}"
    do
        rsync -tz --contimeout=10 "$remote:/data1.txt" /host/
        # scp -pC "$remote:/data1.txt" /host/
    done
done

# Forever

The loop delays 60 seconds between executions, so if the copies take 15 seconds each you'll actually loop every 90 seconds. If you want the loop to aim for a minute cycle regardless of the copying duration you can use bash's built-in clock:

interval=60 start=$SECONDS
while sleep $(( interval - ((SECONDS - start) % interval) ))
do
    …
done
Source Link
Chris Davies
  • 128k
  • 16
  • 178
  • 323

Use an array to contain the pair of hosts. Initially the array can contain just a single element, and you can add the second host later. Here I've included two sets of credentials so you can see the syntax. For the copying I've chosen to use rsync but you could revert to scp if you preferred.

#!/bin/bash

# Remote credentials
remotes=('user@remote1' 'user@remote2')

# Enable data acquisition service
for remote in "${remotes[@]}"
do
    ssh -n "$remote" 'systemctl start my.service'
done

# Transfer data file every 60 seconds, until SIGINT from host
while sleep 60
do
    for remote in "${remotes[@]}"
    do
        rsync -tz --contimeout=10 "$remote:/data1.txt" /host/
        # scp -pC "$remote:/data1.txt" /host/
    done
done

# Forever