首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

Linux周期运作一个命令的脚本

2013-03-21 
Linux周期运行一个命令的脚本在做测试的时候,有时候需要不断的运行一个命令或者脚本,下面的这个脚本可以实

Linux周期运行一个命令的脚本

在做测试的时候,有时候需要不断的运行一个命令或者脚本,下面的这个脚本可以实现这个目的。

使用方法:

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/shPROG_NAME=period.shINTERVAL=1ALWAYS_LOOP=1COUNT=-1#------------------------- functions --------------------------------------------------usage() {  cat << ENDusage: $PROG_NAME [options] <cmd> <args>    run command periodically.options:    -c <count> : how many times.    -i <interval>: every <interval> seconds.    -h: show helpEND}run_cmd(){  echo $ $*  eval $*  echo}#---------------------------- main() --------------------------------------------------# get command line argumentswhile getopts "c:i:h" options; do  case "$options" in    c) COUNT=$OPTARG ;;    i) INTERVAL=$OPTARG ;;    h) usage; exit 0;;    \?) usage; exit -1;;  esacdoneshift $((OPTIND - 1))if [ $# -lt 1 ]; then  usage;  exit 0;fiif [ $COUNT -ge 0 ]; then   ALWAYS_LOOP=0else   COUNT=1fiwhile [ $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 $INTERVALdone





热点排行