日期:2014-05-16 浏览次数:20658 次
在做测试的时候,有时候需要不断的运行一个命令或者脚本,下面的这个脚本可以实现这个目的。
使用方法:
usage: period.sh [options] <cmd> <args>
run command periodically.
options:
-c <count> : how many times.
-i <interval>: every <interval> seconds.
-h: show help
示例:
$ ./period.sh ls -l
$ ./period.sh -i 3 ls -l
#!/bin/sh PROG_NAME=period.sh INTERVAL=1 ALWAYS_LOOP=1 COUNT=-1 #------------------------- functions -------------------------------------------------- usage() { cat << END usage: $PROG_NAME [options] <cmd> <args> run command periodically. options: -c <count> : how many times. -i <interval>: every <interval> seconds. -h: show help END } run_cmd() { echo $ $* eval $* echo } #---------------------------- main() -------------------------------------------------- # get command line arguments while getopts "c:i:h" options; do case "$options" in c) COUNT=$OPTARG ;; i) INTERVAL=$OPTARG ;; h) usage; exit 0;; \?) usage; exit -1;; esac done shift $((OPTIND - 1)) if [ $# -lt 1 ]; then usage; exit 0; fi if [ $COUNT -ge 0 ]; then ALWAYS_LOOP=0 else COUNT=1 fi while [ $ALWAYS_LOOP -eq 1 ] || [ $COUNT -gt 0 ]; do echo "#$INDEX Date: `date +%Y-%m-%d_%H:%M:%S`" run_cmd $* INDEX=$((INDEX + 1)) COUNT=$((COUNT - 1)) sleep $INTERVAL done