我有以下脚本,我需要它在我的 Windows 服务器上每分钟运行一次。文件路径是 d/TFTP/script.sh 所以我想知道是否可以插入 while 循环或其他方法,这样我就可以让它每分钟在后台运行
#!/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
答案1
每个问题的评论线程:
#!/bin/bash
while [[ 1 -eq 1 ]]; do
everything_in_the_original_script
sleep 60
done
答案2
或者,如果您希望每次迭代总共花费 60 秒,则此 Bash 脚本将为您的代码计时,并且仅在 60 秒内剩余的时间内休眠(如果您的代码花费的时间超过 60 秒,则它根本不休眠)...
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