0

I have the following script and I need this to run every minute on my windows server. The file path is d/TFTP/script.sh so I was wondering if I can insert a while loop or some other method so I can have this running in the background every minute

#!/bin/bash

declare -A arr_map

arr_map=([AR]=Switches [SW]=Switches [LR]=Switches [AP]=Default [GV]=Default [DR]=Default [GV]=Default [VN]=Default [MGMT]=Default [GW]=Routers)

# Iterate through indexes of array
for keyword in "${!arr_map[@]}"; do
    # Search files containing the "-$keyword" pattern in the name
    # like "-GW" or "-AR". This pattern can be tuned to the better matching.
    for filename in *-"$keyword"*; do
        # if file exists and it is regular file
        if [ -f "$filename" ]; then
            destination=${arr_map["$keyword"]}/"$filename"
            # Remove these echo commands, after checking resulting commands.
            echo mkdir -p "$destination"
            echo mv -f "$filename" "$destination"
            mkdir -p "$destination"
            mv -f "$filename" "$destination"
            #echo in front of mkidr and move
        fi
    done
done
7
  • Which did you want- the script to run atomically every minute, or for the script to run once ever and loop infinitely, once per minute?
    – DopeGhoti
    Commented Jan 9, 2018 at 17:12
  • Hi, either works. I just need this to run every minute either way infinitely
    – ksuzy31
    Commented Jan 9, 2018 at 17:16
  • Use Task Scheduler to invoke Cygwin's bash.exe with arguments -c and 'cd /path/to/script; ./script.sh', and schedule it as desired.
    – DopeGhoti
    Commented Jan 9, 2018 at 17:18
  • Can you help me understand? I'm not familiar with task scheduler
    – ksuzy31
    Commented Jan 9, 2018 at 17:31
  • 1
    Hmmm....is there some problem with the cygwin cron package?
    – mdpc
    Commented Jan 9, 2018 at 23:55

2 Answers 2

0

Per comment thread on the question:

#!/bin/bash
while [[ 1 -eq 1 ]]; do
    everything_in_the_original_script
    sleep 60
done
1
  • Of course this exposes an ambiguity in the question: if ‘‘everything_in_the_original_script’’ takes 18 seconds, this will wait 60 seconds before running it again, rather than the 42 second delay that would be necessary to get the commands to run once every minute. Commented Jan 9, 2018 at 20:11
0

Alternatively, if you want each iteration to take 60 seconds total this Bash script will time your code and only sleep for whatever time remains in the 60 seconds (if your code took longer that 60 seconds it doesn't sleep at all)...

sleep_time=60

while true; do
    start_secs=$(date +'%s')
    everything_in_the_original_script
    end_secs=$(date +'%s')

    elapsed=$((end_secs - start_secs))
    [[ $elapsed -le $sleep_time ]] || elapsed=$sleep_time
    sleep $((sleep_time - elapsed))
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .